Description
This example shows the how to write to and or read to any from an XCON (TCP/, UDP, etc.) connection. The connection can be handled with LUA function. The write value can be written from any selected item, and the read value also be written to an output item.
The Read read function should handle handles the incoming values and convert them to their HEX valuesconverts them into a hex dump string.
This tutorial has two functions:
...
Code Block | ||||
---|---|---|---|---|
| ||||
function OnStartEvent() -- this is a predefined callback function, add only the following line to the function. xcon.CreateTCP("MyTCP","0",0,"127.0.0.1",23) --Remote Server:127.0.0.1, Remote Port:23 end function Send_TcpHex() local var = nxa.GetValue('NETx\\VAR\\String\\Item001') --Send value NETx\VAR\String\Item001 xcon.SendHex("MyTCP", var) end function MyTCP_OnReceiveEvent(data, size, sourceip, sourceport) local val= string.tohex(data) nxa.SetValue('NETx\\VAR\\String\\Item002',val) --Received value NETx\VAR\String\Item002 end function string.tohex(str) return (str:gsub('.', function (c) return string.format('%02X', string.byte(c)) end)) end |
Now o use this in LUA, you can Next, define a server task that is connected to the :('triggerd upon changes of the source item, NETx\\VAR\\String\\Item001') item – this means when you want – That is, to send data , your the LUA Function is invoked and you can parse and send data to the server. function is invoked: the source item value is treated as a hex dump string, converted into binary data and sent to the connection.
Info | ||
---|---|---|
| ||
The MyTCP_OnReceiveEvent() callback function is invoked when an XCON connection has been established, connection will be established when the serve starts in data is received on the established XCON connection named MyTCP. The actual setup of the connection is done in the OnStartEvent() callback function. |