Krinje Posted August 24, 2016 Posted August 24, 2016 This is my first time attempting an export (or any udp/tcp connection), I think I understand what is going on. I followed the export.lua's example. I'm unable to test at the moment (partly being unfinished) but mostly I do not have a listener written yet. The listener will be part of a Java application. I'm wondering if I'm barking up the correct tree(s) here. If I continue working on what I've got will I get the results I expect? Any sage advice is welcome, Thanks. --Start Socket package.path = package.path..";.\\LuaSocket\\?.lua" package.cpath = package.cpath..";.\\LuaSocket\\?.dll" socket = require("socket") host = "localhost" port = "8080" c = socket.try(socket.connect(host, port)) c:setoption("tcp-nodelay", true) end --function LuaExportNextFrame() --Nothing --end --function LuaExportAfterNextFrame() --Nothing --end function LuaExportActivityNextEvent(t) local tNext = t local gazelleM = --Gazelles ID number local gazelleMin = local gazelleL = local craft = LoGetPlayerPlaneId() if craft == gazelleM or gazelleMin or gazelleL then --GazelleID number local altBar = LoGetAltitudeAboveSeaLevel() local nadirLatLong = --I don't know what these are yet, I need to do some diging. local bearing = local tvBearing = local laseRange = socket.try(c:send(string.format("craft = %d, altBar = %d, nadirLatLong = %d, bearing = %d, tvBearing = %d, laseRange = %d"))) --I don't know what types most of these will be int(%d) place holders. end tNext = tNext + 0.5 --repeat every 1/2 second return tNext end function LuaExportStop() --Close Socket socket.try(c:send("quit")) c:close() end
FSFIan Posted August 24, 2016 Posted August 24, 2016 Your code will only work if the listener application is already running when you enter the cockpit, otherwise the TCP connection attempt will fail. There are three ways around this: retry the TCP connection, put the TCP server inside DCS, or send data using UDP which doesn't care whether someone receives it or not. For an example of the "retry TCP connection" approach, look at the DCS Witchcraft source code. For a TCP server example and a UDP example, you can look at the DCS-BIOS source code (in ProtocolIO.lua). "host", "port" and "c" are global variables. This isn't a problem as long as you are the only user of your export script, but if you ever intend to publish this for other people to use, you should have at most one global table that includes the name of your project and put everything else under that. You can also use file-local variables. Another thing you need to do if you want your export script to play well with others (e.g. Helios, TARS or DCS-BIOS) is to save the previous value of the callback functions and call them from your own callbacks. See BIOS.lua from the DCS-BIOS source code for an example. I am not sure what you are trying to achieve with the "craft == gazelleM or gazelleMin or gazelleL" condition. A short description of what you are trying to achieve would be helpful. (I can't tell whether you'll get the results you expect if I don't know what your expectations are :) ) PS: if you haven't already, you should set up DCS Witchcraft (link in my sig). It includes a Lua Console that lets you quickly try pieces of Lua code inside the running simulation. See the comments at the beginning of WitchcraftExport.lua for instructions on how to set this up for the Export.lua environment instead of the missions scripting one. DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
Krinje Posted August 24, 2016 Author Posted August 24, 2016 (edited) Thank you for the excellent reply. I've written a simple calculator in Java for the calculation of Lat/Long of a target via bearing and distance provided by the gazelle's optics and ranging laser. I'm looking to automate much of the data entry by getting NADIR's (your position) lat/long, hopefully the camera's bearing and lased distances etc... The if than statement is to filter out non-gazelle aircraft and avoid unnecessary work/possessing because I don't know how much or how negligible this is for DCS to run. The gazelle currently has 3 airframes which I assume all have different IDs. Using a program called SocketTest, I confirmed before I saw your post the connection attempt issue. I did create the socket, and close the socket (yay!) but even when commented down to just the PlaneID and subsequent send I did not receive data. (wrong type? %d?) Thank you very much for pointing me to some good source. I think I'm going to take a look at the both examples, and attempt the UDP. Is this a good example of global table? (from Simple Radio) #Edit: Never mind that, I figured out what you meant. Edited August 24, 2016 by Krinje
FSFIan Posted August 24, 2016 Posted August 24, 2016 Take a look at the output of the Lua function 'list_cockpit_params()'. At least in the A-10C, it has some information about the TGP. You might find something useful. Another interesting function is 'list_indication(number)'. If the info you are interested in is displayed anywhere in the cockpit as text, chances are you can grab it with this function. It expects one argument -- an integer ID. Try it with different values (starting at 0 I think) and see what you get. Sometimes it does not return any text if a particular instrument is turned off, so only stop trying new numbers when you didn't get anything a few times in a row. If the information is represented by some kind of animation in the virtual cockpit (an indicator light, a needle on an analog gauge, a drum counter, etc) it will be represented by a cockpit argument. For example, in the A-10C, cockpit argument 404 represents the state of the Master Caution Light. To get the value of a cockpit argument, use GetDevice(0):get_argument_value(number), e.g. local a10cMasterCautionLightValue = GetDevice(0):get_argument_value(404) The best way to tell which cockpit argument number corresponds to a specific value is to read through the Gazelle's clickabledata.lua file. If you are familiar with the airframe, you should be able to make an educated guess about which entries correspond to which gauges in the cockpit. DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
Krinje Posted August 24, 2016 Author Posted August 24, 2016 Excellent. Because the NADIR displays LatLong as a 7 segment style display and the TV has 3 numerical and important pieces of information. I think that second tip will prove very useful. As it stands: --Version 0.2 local host = "localhost" local port = "8080" local socket = require("socket") local c = socket.try(socket.connect(host, port)) local PrevExport = {} PrevExport.LuaExportStart = LuaExportStart PrevExport.LuaExportStop = LuaExportStop PrevExport.LuaExportNextFrame = LuaExportNextFrame PrevExport.LuaExportAfterNextFrame = LuaExportAfterNextFrame PrevExport.LuaExportActivityNextEvent = LuaExportActivityNextEvent function LuaExportStart() --Start tcp Socket package.path = package.path..";.\\LuaSocket\\?.lua" package.cpath = package.cpath..";.\\LuaSocket\\?.dll" --socket = require("socket") --c = socket.try(socket.connect(host, port)) c:setoption("tcp-nodelay", true) socket.try(c:send("Gazelle Calc Export: STARTING, \n")) if PrevExport.LuaExportStart then PrevExport.LuaExportStart() end end function LuaExportNextFrame() --Nothing if PrevExport.LuaExportNextFrame then PrevExport.LuaExportNextFrame() end end function LuaExportAfterNextFrame() --Nothing if PrevExport.LuaExportAfterNextFrame then PrevExport.LuaExportAfterNextFrame() end end function LuaExportActivityNextEvent(tCurrent) local tNext = tCurrent + 0.5 --local gazelleM = --Gazelles ID number --local gazelleMin = --local gazelleL = local craft = LoGetPlayerPlaneId() --[[ if craft == gazelleM or gazelleMin or gazelleL then --GazelleID number local altBar = LoGetAltitudeAboveSeaLevel() local nadirLatLong = --I don't know what these are yet, I need to do some diging. local bearing = local tvBearing = local laseRange = socket.try(c:send(string.format("craft = %d, altBar = %d, nadirLatLong = %d, bearing = %d, tvBearing = %d, laseRange = %d"))) --I don't know what types most of these will be int(%d) place holders. end ]]-- socket.try(c:send(string.format("craft = %d, \n", craft))) if PrevExport.LuaExportActivityNextEvent then PrevExport.LuaExportActivityNextEvent(tCurrent) end return tNext end function LuaExportStop() --Close Socket socket.try(c:send("Gazelle Calc Export: STOPPING, \n")) c:close() if PrevExport.LuaExportStop then PrevExport.LuaExportStop() end end I honestly don't know *why* I'm doing the callbacks, just that they seem to work and you said to do them and I couldn't quite figure out why. I'm mostly done head scratching for the day. I'm trying to bite this off one piece at a time. For now, connecting once is acceptable. figured out my problem with string.format, I miss understood how it functioned from the examples in export.lua and it is now working fine. I am now seeing the KA-50 as 16779520 in SocketTest. Again thank you very much for your help so far.
Recommended Posts