Jump to content
The simFlight Network Forums

LUA COM library


Recommended Posts

Dear Pete,

I am experimenting with direct communication between LUA and Arduino. If I suceed I would like to publish my results to give a cheap inputs/outputs to anybody who thinks that all those overpriced Opencockpits (and simillar) cards are just useless...

 

I have my arduino programmed but I need some examples in LUA. Could you give me just a simple script which will be sending string "abc" always when it will be running? I mean - is my code acceptable?

handle = com.open(3,9600,0) //COM 3 opened, 9600bps, no handshake
com.write(handle, "abc")

What does the word "handle" mean here? Is it variable name that can be changed, or what?

 

The same for sending:

handle = com.open(3,9600,0) //COM 3 opened, 9600bps, no handshake
n = com.test(handle)
my_received_data = com.read(handle,n)

Is this way correct? I need just listen to COM port and when some string arrives from my arduino I need to store it into a variable...

 

Thank you,

Pavel

Link to comment
Share on other sites

Good work Paja. 

 

During the initial days of my cockpit build, I had used a 'test script' that interfaced my Leonardo board with FSUIPC using LUA. 

But, it wasn't my script. I can't write code if my life depended on it :)

 

May I recommend for you to head over to http://hornetpits.org/index.php where a gentleman called Tripod has done a considerable work with LUA scripts and its interface with FSUIPC. He was also experimenting with Arduino sketches and LUA and I'm certain both of you might many interesting notes to share with each other. 

 

Cheers,

 

Mickey

Link to comment
Share on other sites

Ok, I did some testing with updated code:

handle = com.open("COM3",9600,0) --COM 3 opened, 9600bps, no handshake
while 1 do
com.write(handle, "a")
ipc.sleep(500)
com.write(handle, "g")

ipc.display("overhead driver ok")

end

which does something... the message appears, the script runs, the Receiving LED on Arduino blinks quickly, but the arduino is not receiving what it is expecting to (the "a" should turn on a LED, the "g" should switch the LED off - which works from any Serial terminal app, so the arduino code is correct). Is here any way how to log the outcoming traffic from LUA?

 

Pavel

Link to comment
Share on other sites

Ok, I did some testing with updated code:

handle = com.open("COM3",9600,0) --COM 3 opened, 9600bps, no handshake
while 1 do
com.write(handle, "a")
ipc.sleep(500)
com.write(handle, "g")

ipc.display("overhead driver ok")

end

which does something... the message appears, the script runs, the Receiving LED on Arduino blinks quickly, but the arduino is not receiving what it is expecting to (the "a" should turn on a LED, the "g" should switch the LED off - which works from any Serial terminal app, so the arduino code is correct). Is here any way how to log the outcoming traffic from LUA?

 

 

 

I always debug serial port (COMn) problems using the free debugging tool "PortMon" from http://technet.microsoft.com/en-gb/sysinternals/

 

Pete

  • Upvote 1
Link to comment
Share on other sites

Well if I start the portmon.exe it says Not Connected even if I start it after or before the FSX with transmitting LUA plugin (or any other COM software like the COM terminal from Arduino) - have you experienced it?

 

Or possibly could LUA debug to some file everything it has sent via COM? The arduino is receiving data correctly, it blinks every 500ms with RX LED but the orders dont work so LUA must be sending something else than just a and g there :sad: When I send a and g by COM terminal it works with my board...

 

What does the "handle" word mean? Does my code send "handle a" or "a"?

 

Pavel

Link to comment
Share on other sites

Well if I start the portmon.exe it says Not Connected even if I start it after or before the FSX with transmitting LUA plugin (or any other COM software like the COM terminal from Arduino) - have you experienced it?

 

No -- I think you must start PortMon and select the correct port before any program opens it. it the device is using a virtual port via a USB connection then you probably need to have the device connected, but no program trying to use it.

 

But I'm not sure about that -- it has always worked for me. There are more sophisticated programs which can do more, and may work better for you.  Try "Device monitoring studio" www.hhdsoftware.com/Downloads/device-monitoring-studio

 

What does the "handle" word mean?

 

It's the handle, or identifier, of the device you are using. It relates back to the com.open, as you surely must have noticed:

 

handle = com.open("COM3",9600,0)

 

You can have any number of devices open for reading/writing. the handle determines which one!

 

Does my code send "handle a" or "a"?

 

 

The line com.write(handle, "a") sends the string "a" which is two bytes -- the ASCII character "a" (0x61 in hex) and the zero terminator (0x00) -- i.e two bytes altogether.

 

If you only want the single character 'a' sent you must specify the length parameter too, i.e.

 

com.write(handle, "a", 1)

 

Pete

Link to comment
Share on other sites

That sounds like a good point however code

handle = com.open("COM3",9600,0) --COM 3 opened, 9600bps, no handshake
while 1 do
com.write(handle,"a",1)
ipc.sleep(500)
com.write(handle,"g",1)

ipc.display("overhead driver ok")

end

gives the same result - just RX led blinking (even if I tried it with spaces inside like (handle, "g" 1). I found out that Arduino in default settings has 8-N-1 mode (http://en.wikipedia.org/wiki/8-N-1) - is it the same for LUA? I can change this setting to any other mode in Arduino if required...

 

Or maybe am I missing something when LUA Library pdf says that I should use n = com.write(    ? What should I in my simple example place instead of n?

 

Pavel

Link to comment
Share on other sites

That sounds like a good point however code

gives the same result - just RX led blinking (even if I tried it with spaces inside like (handle, "g" 1).

 

I'm not exactly sure what "g" 1 would do -- I think it would make a string containing "G1" and the zero terminator, so sending 3 characters. You'd need to refer to Lua documentation.

 

 I found out that Arduino in default settings has 8-N-1 mode 

 

Yes, that's just the standard 8 bit no paraity 1 space bit mode. That's exactly what you get.

 

Or maybe am I missing something when LUA Library pdf says that I should use n = com.write(    ? What should I in my simple example place instead of n?

 

Nothing. "n" is the variable into which the result of the com.write is placed. It's the number of bytes actually sent -- why not refer to the documentation for such information, it is all there, explicitly!?

 

I think you need to check exactly what you see getting sent both from your Lua and your other method, and compare them. I already suggested Portmon and another program. I cannot help you further, you have to help yourself. I've no idea what your Arduino wants.

 

Pete

Link to comment
Share on other sites

Interesting - I have tried real time printing of the n variable to a ipc.display and it returns just 1, regardless if the line is "a" or "g" or if I specify the length parameter or not.

 

COM.PNG

 

I have googled it but the ONLY thing I could find is some VRInsignt MCP driver where autor uses the same name for handle name and for variable which openes the com.open and he does NOT save it to any variable like dev=com.write but he uses just com.write... I have tried it exactly as he did and the result was quite...weird :grin:

 

COM_2.PNG

 

Do you have any tips where to get more info about com.write in LUA? I cant find it on their website... 

 

I have tried lot of COM sniffing programs but all of them are unable to open the COM port while FSX (or Arduino terminal or whichever app) is communicating on that port with Arduino...

 

My only and last hope is to find another computer with COM port, try to wire them together and log on the second computer what is my LUA actually sending to it (LUA wont know it is not arduino but computer, I hope)

 

Pavel

Link to comment
Share on other sites

Interesting - I have tried real time printing of the n variable to a ipc.display and it returns just 1, regardless if the line is "a" or "g" or if I specify the length parameter or not.

 

Ah, sorry, my mistake. Checking the code I see that I eliminate the terminating zero. If you want that you have to set a length of 2.

 

I have googled it but the ONLY thing I could find is some VRInsignt MCP driver where autor uses the same name for handle name and for variable which openes the com.open and he does NOT save it to any variable like dev=com.write but he uses just com.write... I have tried it exactly as he did and the result was quite...weird  :grin:
 
Not sure what you expect to find b Googling. The documentation for all of the FSUIPC Lua libraries is in the FSUIPC Documents folder, nowhere else. If you want to learn more about Lua itself, rather than the libraries, the link to their website is given in the documentation also in the FSUIPC Documents folder There are also lots of examples in there.
 
Do you have any tips where to get more info about com.write in LUA? I cant find it on their website... 
 
Because the com library, like all the other libraries added in FSUIPC and WideFS, are specifically for use with FSUIPC and WideFS. The only documentation is that in the FSUIPC Documents folder. What is wrong with it? The information is all there, and lots of examples. You seem to be very confused. Haven't you read any of the documentation I supply at all?
I have tried lot of COM sniffing programs but all of them are unable to open the COM port while FSX (or Arduino terminal or whichever app) is communicating on that port with Arduino..
 
I don't know about "COM sniffing", but certainly the program I use, the one I pointed you to, the Device Monitoring Studio, is fully capable of monitoring any COM or USB port.  It installs a low level monitor into Windows and needs a re-boot after installation in order to get the privilege to intercept ports.
 
Pete
Link to comment
Share on other sites

  • 3 months later...

Hi Pavel,

 

I don't know whether you finally solved your issue with the COM port.

 

Like you, I am trying to establish direct communication between FSUIPC and my Arduino via Lua Com.

 

Anyway, I spent a whole afternoon with your code, and variations of it, experiencing exactly the same issue.....flashing Rx light but no output to the pins..... :???:......until I finally traced the problem......I removed the ipc.sleep(500) line, and the Arduino duly sent its output signals to the LED. :razz:

 

So try removing the ipc.sleep line and see if it works for you too. I've attached my working test code (the sequences 010 and 011 are of course what my Arduino is looking for).

 

Regards,

 

Chakko.

dev = com.open("COM3", 115200, 0)

function pkgbrake(offset, value)
    if value == 0 then
        com.write(dev, "010\n")
    else
        com.write(dev, "011\n")
    end
end

event.offset(0x0BC8, "UB", "pkgbrake")
Link to comment
Share on other sites

  • 4 weeks later...

Ok, got it working... Attached I am sending the easiest ever example of serial communication with arduino. Just paste the INO into your arduino and the LUA into your FSX/Modules and you will get parking brake LED working on the Arduino's onboard LED.

dev = com.open("COM3", 9600, 0)

function pkgbrake(offset, value)
    if value == 0 then
        com.write(dev, "g")
    else
        com.write(dev, "a")
    end
end

event.offset(0x0BC8, "UB", "pkgbrake")
int strValue = 0;
int light1 = 0;
const int out1 = 13; 

void setup()
{
  Serial.begin(9600);
  pinMode(out1, OUTPUT);
}

void loop()
{ 
  if(Serial.available() > 0) {
      strValue = Serial.read();
  }
  

  if (strValue == 'a')
  {
      digitalWrite(out1,HIGH);
      strValue = 'z';
  }

  if (strValue == 'g')
  {
      digitalWrite(out1,LOW);
      strValue = 'z';
  }
}

Hope that helps.

 

Pavel

Link to comment
Share on other sites

  • 3 years later...

Hello, Pavel Sir,

I am trying to figure out how to work with lua Lvars for past few weeks. I have build few PMDG 737 panels with Mobiflight though, as it already has list of publish offsets. But I am total newbie as far as Lua programming is concerned. I am not a programmer but trying to learn.

I am trying to build caution light panel for Metal2Mesh Mirage 2000C (M2M M2KC). I have somehow managed to control switches by using fsuipc macros. But dont know what to do for outputs.  I tried your code for parking brakes and it worked like a charm for parking brakes  in M2M Mirage 2000C. Now, I have all the list of Lvars for inputs (Obtained using FSUIPC), But I don`t know how to get output from M2M M2000C for lightning LEDs.

I got a LUA file for Mirage 2000 for DCS World. But I dont know how to use it in FSX-SE.

Link to DCS BIOS https://github.com/Exo7/DCS_BIOS-M2000C_Library/releases/tag/v1.20  (Mirage 2000 Lua is in ....\DCS_BIOS-M2000C_Library-1.20\DCS-BIOS\lib\M2000C.lua)

Is there any way to use above with FSX??

Also If I could get any help regarding your code (quoted) that how to use it with some other custom Lvars or/and offsets other than parking brakes?

Any help would be highly appreciated.

Thank You So Much.

 

On 2/8/2015 at 2:03 AM, Paja said:

Ok, got it working... Attached I am sending the easiest ever example of serial communication with arduino. Just paste the INO into your arduino and the LUA into your FSX/Modules and you will get parking brake LED working on the Arduino's onboard LED.


dev = com.open("COM3", 9600, 0)

function pkgbrake(offset, value)
    if value == 0 then
        com.write(dev, "g")
    else
        com.write(dev, "a")
    end
end

event.offset(0x0BC8, "UB", "pkgbrake")

int strValue = 0;
int light1 = 0;
const int out1 = 13; 

void setup()
{
  Serial.begin(9600);
  pinMode(out1, OUTPUT);
}

void loop()
{ 
  if(Serial.available() > 0) {
      strValue = Serial.read();
  }
  

  if (strValue == 'a')
  {
      digitalWrite(out1,HIGH);
      strValue = 'z';
  }

  if (strValue == 'g')
  {
      digitalWrite(out1,LOW);
      strValue = 'z';
  }
}

Hope that helps.

 

Pavel

 

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.