Appendix G: Lua Scripts
X-600M Users Manual
following error codes:
0 – Host did not responded
-1 – Can't call tcp functions from lua expressions
-11 – Error in send
-13 – Bad message length. The message must be under 200 characters.
tcpRecv()
This function will attempt to receive a response from a host that has been previously connected to
using
tcpConnect
, and most likely sent a message using
tcpSend
. It will return the number of bytes
received, or one of the following error codes:
-1 – Can't call tcp functions from lua expressions
-12 – Error in receive
This function is similar to the
httpRequest
function in that it will store the response in an internal
buffer associated with the lua script. Each script (1-5) have their own response buffer that can hold
responses up to 8K in length. Responses bigger than 8K will be truncated. These responses can
then be parsed using the functions
responseIndexOf,
responseSubstr
and
responseGetBytes
as explained previously.
tcpClose()
This function will close the currently open tcp connection if it exists. This function must be called
before attempting to call
tcpConnect
in order to connect to a new host.
The following example demonstrates how to use the tcp functions described above to send and
receive a string over a tcp socket.
-- connect to server and send a message every second
-- keep track of connection state. 0 = not connected, 1 = connected
connected = 0
enableDebug()
while true do
-- try to connect
if connected == 0 then
print("Connecting")
rc = tcpConnect("192.168.1.25", 8000, 3000)
if rc == 1 then
print(" Connected")
connected = 1
end
end
-- if connected send message
if connected == 1 then
print("Send message")
rc = tcpSend("Hello world!\r\n")
-- if the send failed, close the connection
if rc < 0 then
print("tcpSend error: "..rc)
tcpClose()
connected = 0
Page 132
Xytronix Research & Design, Inc.
Summary of Contents for X-600M
Page 1: ...X 600MTitle Page Goes Here...
Page 6: ...X 600M Users Manual Page 4 Xytronix Research Design Inc...
Page 99: ...X 600M Users Manual Setup Pages Xytronix Research Design Inc Page 97...
Page 116: ...Appendix B Installing New Firmware X 600M Users Manual Page 114 Xytronix Research Design Inc...