Jump to content
The simFlight Network Forums

Serial read/write inside the same LUA script


Recommended Posts

Hello,

I'm trying to make my first panel, starting from basic implementation, Arduino, 1 rotary encoder, 1 led.

I associated this LUA script to one of my Joystick button, that I press only one time, once "ready to fly" (FS2020).

COM port correctly open and function Arduino_Data correctly receive values and autopilot altitude increase or decrease correctly.

What I want to try, is, changing the taxi lights directly from inside the simulator, send the status to the Arduino and switch on or off a led.

To do this I used the offset and the function sendLightsStatus, now only for practice I always send the TAXION string to arduino, it's only an exercise to understand.

When I switch the taxi lights inside FS2020, the offset is correctly taken and the function is executed, but no bytes are sent, due the COM port seems to be busy by the event.com needed to monitor the inputs arriving from arduino.

How can I solve?

I'm a bit new to LUA and FSUIPC scripting, but not in programming in general.

Thank you

Arduino_Com_Port = com.open("COM6", 115200, 0)

if Arduino_Com_Port ~= 0 then
	ipc.display("Arduino Com Port Open",5)
else
	ipc.display("Could not open ARDUINO Com Port")
	ipc.exit()
end 

function Arduino_Data(Arduino_Com_Port, datastring, length)
 	if (string.find(datastring, "INC1000")) then  -- Altitude up
		ipc.control(65892,0) 
	end
 	if (string.find(datastring, "DEC1000")) then  -- Altitude dw
		ipc.control(65893,0)  
	end
end 

function sendLightsStatus(offset, value, Arduino_Com_Port)
  n = com.write(Arduino_Com_Port, "TAXION")
  ipc.display("Sent: "..n)
end


event.com(Arduino_Com_Port, 50,1, "Arduino_Data") -- Monitor the serial
event.offset("0D0C", "UB", "sendLightsStatus") -- Change of taxi lights

 

Edited by Federico
Link to comment
Share on other sites

Hello John,

I changed all the ipc.display to ipc.log, but the problem is the com.write does not work when the event.com is enabled.

For example if I comment the event.com line, then when I change thaxi lights inside the simulator the string is correctly passed to arduino.

Thank you.

Link to comment
Share on other sites

3 minutes ago, Federico said:

For example if I comment the event.com line, then when I change thaxi lights inside the simulator the string is correctly passed to arduino.

What is sending the string then when you uncomment that line?

Does it work if you use two scripts, one for each event function? You will need to open the com port in each script. Maybe try that.
In the mean-time, I'll see if I can check why it isn't working with both event calls in the same lua file...

John
 

Link to comment
Share on other sites

4 minutes ago, John Dowson said:

Does it work if you use two scripts, one for each event function? You will need to open the com port in each script. Maybe try that.

Unlike USB devices via DirectInput, you can't open COM serial ports more than once. You'd need instead to pass the handle from one to the other via ipc.set and ipc.get.

The event.com will merely be waiting for data to arrive into a buffer. That's a separate always-running thread within FSUIPC. The same functions within FSUIPC are used by PFCcom64.DLL and my GA28R drivers, with potentially furious rates of input and output. Neither routines are blocking at all or the serial PFC and GA28 gear would not be operable.

The most important information missing at present is the value of 'n', returned by the com.write. That gives the number of bytes placed in the send buffer, which is subject to a continuous loop in a separate thread.

Pete

 

 

Link to comment
Share on other sites

1 minute ago, Pete Dowson said:

Unlike USB devices via DirectInput, you can't open COM serial ports more than once. You'd need instead to pass the handle from one to the other via ipc.set and ipc.get.

Ah, ok. But the documentation for com.open states:

Quote
If the port is already opened by FSUIPC for use in its handling of VRInsight devices, the com.open call will succeed and be granted access to the same port.

So thats just for VRInsight devices but not others?

 

Link to comment
Share on other sites

3 minutes ago, John Dowson said:

So thats just for VRInsight devices but not others?

Yes. It's related to the definition in the INI file stating the VRI com ports. I think it was a provision for some other VRI-related needs, but I don't remember anything specific.

Looking briefly at the PFC drivers they appear to use their own comms code, not the functions in FSUIPC (which aren't exported in any case). So I mis-remembered that bit.

Nevertheless the I/O functions in the PFC drivers are pretty much the same as those in FSUIPC. They were probably the source of the FSUIPC ones as the latter would have been added later.

I certainly can't see that event.com can prevent other events from being triggered, which is what the user's report implies.

A Lua trace log would be useful to see the sequences in the user's script. Maybe the problem is that the event.com is being continuously triggered, which would imply that the problem is in the Arduino programming.

Pete

 

 

Link to comment
Share on other sites

Hello,

first of all thank you very much Pete & John.

I divided the script in 2 separate files and used the ipc.set/get to pass the com handle between them.

This is the first script associated to button1 of the joystick, in the future I'd want to be executed automatically once the flight is loaded, have to understand how. For the moment I start it pressing the button only one time, it is responsible to OPEN the COM and monitoring the event.com in order to catch the rotary encoder changes of the autopilot altitude:

Arduino_Com_Port = com.open("COM6", 115200, 0)

if Arduino_Com_Port ~= 0 then
	ipc.log("Arduino Com Port Open: "..Arduino_Com_Port)
  ipc.set("arduinoCom", Arduino_Com_Port)
else
	ipc.log("Could not open ARDUINO Com Port")
	ipc.exit()
end 

function Arduino_Data(Arduino_Com_Port, datastring, length)
 	if (string.find(datastring, "INC1000")) then  -- Altitude up
		ipc.control(65892,0) 
	end
 	if (string.find(datastring, "DEC1000")) then  -- Altitude dw
		ipc.control(65893,0)  
	end
end 

event.com(Arduino_Com_Port, 50,1, "Arduino_Data") -- Monitor the serial

This is the second script, used to switch on the led on my arduino panel, associated to button2 of the joystick. It catch the event.offset of the taxi lights of A320 that has 3 position OFF/TAXI/T.O.


function sendLightsStatus(offset,value)
  ipc.log("Taxi value: "..value)
  
 Arduino_Com_Port = ipc.get("arduinoCom")
 ipc.log("Arduino COM id: "..Arduino_Com_Port)
 if (value==32) then
  n = com.write(Arduino_Com_Port, "TAXIOFF")
 end
 if (value==40) then
  n = com.write(Arduino_Com_Port, "TAXION")
 end
 if (value==36) then
  n = com.write(Arduino_Com_Port, "TAXITO")
 end

 ipc.log("sent "..n.." bytes")

end

event.offset("0D0C", "UB", "sendLightsStatus")

I confirm with this configuration all is working.

I tried to merge all the in one script and this time works!!!!

It seems the problem was the 2 event.com consecutive lines:

-- Function block
..........
--

event.com(Arduino_Com_Port, 50,1, "Arduino_Data") -- Monitor the serial
event.offset("0D0C", "UB", "sendLightsStatus")

Putting the "senLightsStatus" function just after event.com and before the event.offset, works for me.

Thanks a lot for your help.

Link to comment
Share on other sites

19 minutes ago, Federico said:

in the future I'd want to be executed automatically once the flight is loaded, have to understand how.

You can start it from a [Auto.xxx] section (where xxx is the profile name for the aircraft). See the Advanced User Guide, section Automatic running of Macros and Lua plugins

20 minutes ago, Federico said:

It seems the problem was the 2 event.com consecutive lines:

Hmm, strange, Not sure why that is, but glad its now working.

John

Link to comment
Share on other sites

39 minutes ago, John Dowson said:

You can start it from a [Auto.xxx] section (where xxx is the profile name for the aircraft). See the Advanced User Guide, section Automatic running of Macros and Lua plugins

Thanks John.

It's not clear to me the profile code.

For example, if I want to assign an auto section to my Asobo A320 in FS2020, [AUTO.xxx] which value xxx should have?

 

Link to comment
Share on other sites

2 minutes ago, Federico said:

For example, if I want to assign an auto section to my Asobo A320 in FS2020, [AUTO.xxx] which value xxx should have?

xxx is to be the name of the Profile in which your Asobo A320 is configured.

If you are not using Profiles you can auto start the plug-in using the [Auto] section. Then which aircraft is irrelevant. For more information on this, and Profiles, please do refer to the documentation provided.

Pete

 

Link to comment
Share on other sites

Hello,

I found the problem. It was in the function declaration. I suppose to put another variable containing the COM handle inside the header, besides offset and value, but this was never read inside the function. After managed the com handle with ipc.put/get that worked for me. It seems function called by event cannot have parameters besides offset and value.

So, event can be put all together at the end of the functions section.

Thank you very much for your kindly effort, I'm going to open another topic for other doubts.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.