As promised after yesterday’s “rf12cmd” sketch, here’s a Lua script for Linux which picks up characters from the serial port and turns them into Lua data structures:
Ah, if only things were that simple! On Mac OSX, the serial port hangs when opened normally, so we need to play non-blocking tricks and use the lower-level nixio package:
(note also the subtle “-f” vs “-F” flag for stty… welcome to the world of portability!)
Here’s some sample output:
$ ./rf12show.lua (table)  = (string) hi  = (string) rf12cmd  = (number) 1  = (number) 100 (table)  = (string) rx  = (number) 868  = (number) 5  = (number) 19  = (12 bytes) BAC80801E702200000DA5121 (table)  = (string) rx  = (number) 868  = (number) 5  = (number) 3  = (4 bytes) 21DE0F00 (table)  = (string) rx  = (number) 868  = (number) 5  = (number) 19  = (8 bytes) 7331D61AE7C43901
That’s the startup greeting plus three incoming packets.
I’m using a couple of Lua utility scripts – haven’t published them yet, but at least you’ll get an idea of how the decoding process can be implemented:
dbg.lua– this is the vardump script, extended to show binary data in hex format
benstream.lua– a little script I wrote which does “push-parsing” of Bencoded data
Note that this code is far too simplistic for real-world use. The most glaring limitation is that it is blocking, i.e. we wait for each next character from the serial port, while being completely unresponsive to anything else.
Taking things further will require going into processes, threads, events, asynchronous I/O, polling, or some mix thereof – which will have to wait for now. To be honest, I’ve become a bit lazy because the Tcl language solves all that out of the box, but hey… ya’ can’t have everything!