Devon Custard Posted January 6, 2014 Posted January 6, 2014 Anyone out there willing to share some of there C# LUA interface code? Im looking to interface some stepper motors and 7 segment LEDs to values being exported from DCS:World A10C. Im stuck at first principles, i.e. how the hell do i start, and would really appreciate browsing over someones experiences to get me started. Thanks in advance
Joe Kurr Posted January 7, 2014 Posted January 7, 2014 The easiest way is to write a TCP listener in C#, and connect to that from lua. Here's a snippet from my export.lua: function InitExport() package.path = package.path .. ";.\\LuaSocket\\?.lua" package.cpath = package.cpath .. ";.\\LuaSocket\\?.dll" socket = require("socket") host = "localhost" port = 10309 connection = socket.try(socket.connect(host, port)) connection:setoption("tcp-nodelay", true) socket.try(connection:send("start")) end My C# application listens on port 10309, and initializes all output signals when it receives "start". Since my lua code was originally written for FC2, the below code might need changes in order to work for you. IIRC LoGetMechInfo() only works for FC2 / FC3 aircraft, and not the DCS modules. function ExportCockpitData() if LoGetMechInfo() then local RawGearStatus = LoGetMechInfo().gear.value local RawFlapStatus = LoGetMechInfo().flaps.value local RawBrakeStatus = LoGetMechInfo().speedbrakes.value --[[ RawGearStatus == 1 --> Gear Down RawGearStatus == 0 --> Gear Up RawGearStatus > 0 < 1 --> Gear Retracting / Extending --]] local GearWarning = (RawGearStatus > 0 and RawGearStatus < 1) and 1 or 0 local GearStatus = (RawGearStatus > 0.5 and GearWarning == 0) and 1 or 0 local FlapStatus = (RawFlapStatus > 0.5) and 1 or 0 local BrakeStatus = (RawBrakeStatus > 0.5) and 1 or 0 local FodStatus = ((LoGetMechInfo().gear.main.left.rod + LoGetMechInfo().gear.main.right.rod) / 2 > 0) and 1 or 0 local MasterWarning = LoGetMCPState().MasterWarning and 1 or 0 local returnString = string.format("leds %d%d%d%d%d%d%d%d\n", 0, 0, MasterWarning, GearWarning, BrakeStatus, FodStatus, FlapStatus, GearStatus) if connection then socket.try(connection:send(returnString)) end end end The important bit for you is the "if connection then ... " block. This will send the data in returnString to the C# application. Finally, I let the application know I'm done flying and close the connection when the mission ends: function EndExport() if connection then socket.try(connection:send("--\n")) connection:close() end end Finally, here's a bit of C# code for the TCP listener. I call this function from a new thread, so it runs in the background and keeps the user interface of the application responsive. void ListenForClients() { TcpListener listener = new TcpListener(IPAddress.Loopback, 10309); listener.Start(); while (keepListening) { Socket socketForClient = listener.AcceptSocket(); if (socketForClient.Connected) { NetworkStream stream = new NetworkStream(socketForClient); StreamWriter writer = new StreamWriter(stream); StreamReader reader = new StreamReader(stream); try { string line = keepListening ? reader.ReadLine() : "--"; while (line != "--") { // Handle your client requests here line = keepListening ? reader.ReadLine() : "--"; } } catch (Exception ex) { Console.Error.WriteLine(" !! -- " + ex.Message); } reader.Close(); writer.Close(); socketForClient.Close(); } } } Hope this helps. 1 Dutch Flanker Display Team | LLTM 2010 Tiger Spirit Award
Devon Custard Posted January 7, 2014 Author Posted January 7, 2014 Joe youre a legend. Thanks! Will have a crack at that today when i get home.
TigersharkBAS Posted May 9, 2014 Posted May 9, 2014 DC...might be worth some exploration in the Warthog Wiki? Under Concepts? [sIGPIC][/sIGPIC] Creator of: F-18C VFA-195 "Dambusters" 1998 CAG Livery https://forums.eagle.ru/showthread.php?t=213788 F-18C VFA-195 "Dambusters" July 2001 CAG Livery https://forums.eagle.ru/showthread.php?t=215950 Pilot avatars for DCS Logbook https://forums.eagle.ru/showthread.php?t=221160 How to make a DCS A-10C Panel http://forums.eagle.ru/showthread.php?t=65998
Recommended Posts