LUA5: Serial communication

In some cases the text-oriented style of the XCON branch of the item tree is insufficient. For that reason powerful LUA functions can be used to cover more complex requirements.

Opening the connection

xcon.CreateCOM("mySerialConnection","COM3",9600,8,0,0,0,0)

This function opens the connection and creates the handle mySerialConnection to access the connection furtheron.

Sending text

 xcon.SendText("mySerialConnection","a message")

Sending binary data

 xcon.SendHex("mySerialConnection","00 01 02 0a 0B 0f ff")

Receving incoming data

Create a LUA function named <handle-name>_OnReceiveEvent(vData, vSize, vIP, vPort). Identified by its name it will be the event handler and be called every time data is received through that connection.

function mySerialConnection_OnReceiveEvent(vData, vSize, vIP, vPort)
    nxa.LogInfo("vData: " .. vData)
    nxa.LogInfo("vSize: " .. vSize)
    nxa.LogInfo("vIP: " .. vIP)
    nxa.LogInfo("vPort: " .. vPort)
end

Closing the connection

 xcon.Close("mySerialConnection")

Article applies to the following products:

  • NETx BMS Platform
  • NETx Multi Protocol Server
  • NETx BMS Server 2.0