COM devices

Documentation. First post is always kind of manual page, being continuously updated by author. Following posts are discussion about the topic.
Post Reply
admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

COM devices

Post by admin » 19 Sep 2015, 22:57

Some flight simmers build custom hardware devices with displays, switches, rotary encoders etc. They connect those devices usually (with some Arduino inside) through USB port emulating serial (COM) port. I'm not one of them (yet :-)) but I'm happy to support them.

In the script you can register COM device, send some data to it and register some callbacks when data is received.

Register

Code: Select all

-- add COM port (with default config values) as device
-- 1st param: logical name
-- 2nd param: port name
lmc_add_com('C3', 'COM3')
-- add COM port (with full config) as device
-- 1st param: logical name
-- 2nd param: port name
-- 3rd param: baud rate
-- 4th param: data bits (8, 7 , 6, 5)
-- 5th param: parity ('N', 'O', 'E', 'M', 'S')
-- 6th param: stop bits (1,2)
lmc_add_com('C4', 'COM4', 1200, 6, 'E', 2)
The code above register 2 COM devices with logical names C3 and C4. See the comments in code for parameter's description. The question you probably have is What are the default config values? Well, don't ask such question because I don't know :-). This is what I have reused from the serial library. I could find out probably but I suggest you even use full specification or your start with default and switch to full when default doesn't work.

Send
Sending is quite simple, just send string to serial port by command:

Code: Select all

lmc_send_to_com('C3', 'Hello world')
If you need some raw binary data maybe it could be converted to string in LUA first or you can try to ask in this forum that raw method is needed. I'll add it... one day...

Receive
Receive is little more complicated. Wait.... it's not, it has just additional feature.
You can read data from COM port with simple callback as one could expect

Code: Select all

lmc_set_handler('C3',function(comVal)
  print('Have data from COM3: ' .. comVal)
end)
The callback is of course triggered only when data arrives.

So what's the feature? I was told you can have sometimes difficulty to keep serial data in logical groups. Sounds complicated... The callback is triggered for bunch (you can call it packet) of data which arrives to COM port. The length can be influenced by buffers at device or host (PC) side or some timer - whatever. The point is you logical group of data can be separated between 2 or more callbacks. This is the situation when you can set splitter which is character that divides your logical part of data.

Code: Select all

lmc_set_com_splitter('C3', 'a')
With this splitter the callback will happen only when splitter character arrives and any data received before will come as an argument to the callback function. So for example without splitter you would receive (with callbacks) 5 buffers with content "tyuh", "hja4", 879a", "1234", "7a98". With splitter 'a' the callback function will be called with arguments "tyuhhj", "4879", "12347". Remaining "98" is buffered inside LuaMacros waiting for next splitter to arrive.
Petr Medek
LUAmacros author

Post Reply