Really hoping someone can help, trying to create a plugin for the Rain8net. I have the skeleton working but I am having trouble getting my head around how to buffer incoming packets using the raw format.
Could someone point me to Lua code snippets that I could use to build the buffer.
What I need to do is buffer 3 bytes read and take action then flush. I am a complete newbie to Lua and Luup and have spent a lot of time looking on the web for codes snippets but no luck.
I faced a similar challenge with the protocol for the Caddx NX-584 alarm panel. A chunk of the Lua code handles the incoming bytes, builds the buffer (in my case a string of bytes), decides if it’s finished, and then dispatches off to code to handle the message in the buffer.
In particular, see the readByte() function, which implements a state machine, builds up a message in the MESSAGE variable, and calls handleReadByteResult() when the message is complete. It’s a little convoluted because of specifics of the Caddx protocol, also because I need the code to run synchronously during setup and asynchronously during normal operation.
Code like this might be overkill for your needs, so you might do better with something simple like @Ap15e’s.
More help required, below is my code and the result. I am missing something but cannot for the life of me work out what it is
[code] function rain8netStartup(lul_device)
ipAddress = luup.devices[lul_device].ip
if (ipAddress ~= "") then
luup.log("Running Network Attached I_Rain8net1.xml on " .. ipAddress)
if( luup.io.is_connected(lul_device)==false ) then
luup.io.open(lul_device, ipAddress, ipPort)
end
else
luup.log("Running Serial Attached Rain8net.xml")
end
luup.io.intercept()
command = luup.io.write(string.char(0x40,0x01,0xF0));
while true do
incoming_data = tonumber(string.byte(luup.io.read(2, lul_device)))
luup.log( "Rain8Net: Received " .. incoming_data)
end
end[/code]
It does loop through and give me the data
50 11/05/10 9:39:57.295 luup_log:27: Rain8Net: Received 64 <0x402>
50 11/05/10 9:39:57.301 luup_log:27: Rain8Net: Received 1 <0x402>
50 11/05/10 9:39:57.311 luup_log:27: Rain8Net: Received 0 <0x402>
But it then dies with the following: -
01 11/05/10 9:40:24.321 LuaInterface::CallFunction_Startup-1 device 27 function rain8netStartup failed [string "..."]:67: bad argument #1 to 'byte' (string expected, got no value) <0x402>
01 11/05/10 9:40:24.322 LuImplementation::StartLua running startup code for 27 failed <0x402>
@zoot1612,
Consider what happens if [tt]luup.io.read(2, …)[/tt] times out? You’ve specified a 2-second timeout, but the code below assumes that it’ll always return a value.