Simple output channel from Lua
The embedded Lua engine has buit-in TCP/IP socket capabilities; in this example, a TCP client is opened, that connects to a TCP server running on host.
To open a socket client and connect to a host, the following code fragment can be used:
-- requires a text socket server on host-- at address <hostip>, port 2001
hostip = "192.168.0.21";
sh = connect(hostip,2001);
send(sh,"--- Hello from Jenet ---\n");
Note that in this simple example no error handling is present, therefore the unit may occasionally crash if the remote server is not present.
If you want to send some formatted text, you may start from the following example:
txt = format(" variable 1 = 0x%06X variable 20x%06X\n", var1, var2);
send(sh,txt);
On the host you need a TCP socket server running before the script starts.
A very basic server can be implemented on host in various ways; as an example, a simple code snippet in the Tcl scripting language is provided here:
proc svconn {client peer peerport} {
global remsk
puts "Connection from $peer at port $peerport"
set remsk $client
fileevent $remsk readable {
global remsk
# Do we have a disconnect?
if {[eof $remsk]} {
close $remsk
puts "Closed !"
} else {
set in [gets $remsk]
puts ">> $in"
}
}
}
set sv [socket -server svconn 2001]
Of course there are lots of other different ways to start a text server; if you have any nice example and you would like to share it, please send it to jenet AT zpeng.com.