Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

...

...

...

Description

This example shows the how to write or read to any XCON(TCP/UDP) 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 function should handle the incoming values and convert them to their HEX values. 
This tutorial has two functions: 

  • Send_TcpHex(): used to send HEX values of an Item in this example : ('NETx\\VAR\\String\\Item001') to the Server which has IP address : 127.0.0.1 and Port : 23. More info about xcon.SendHex
  • MyTCP_OnReceiveEvent(data, size, sourceip, sourceport)This function is invoked when an XCON connection has been established. Create a function for each connection to monitor and name it accordingly, e.g. for connection "MyTCP"create the functionMyTCP_OnReceiveEvent.


Code Block
languageactionscript3
titleLUA
function Send_TcpHex()
local var = nxa.GetValue('NETx\\VAR\\String\\Item001')
xcon.CreateTCP("MyTCP","0",0,"127.0.0.1",23)
xcon.SendHex("MyTCP", var)
end




function MyTCP_OnReceiveEvent(data, size, sourceip, sourceport)
local val= string.tohex(data)
nxa.SetValue('NETx\\VAR\\String\\Item002',val)
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 define a server task that is connected to the :('NETx\\VAR\\String\\Item001') item – this means when you want to send data, your LUA Function is invoked and you can parse and send data for the server. 

Image Added


Info
titleNote:

MyTCP_OnReceiveEvent function is invoked when an XCON connection has been established, connection will be established in  Send_TcpHex() function. This means to receive first you will need to send any init value.


Image Added