Rain8net plugin

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.

Here is the link to the hardware.

[url=http://www.wgldesigns.com/rain8pc.html]http://www.wgldesigns.com/rain8pc.html[/url]

Not very elegant, but should do the trick:

local pointer = 1
local action  = 0

for every "incoming byte":

  action = action * 256 + "incoming byte"
  pointer = pointer + 1
  if pointer == 4
   then 
    call_action( action )
    action  = 0
    pointer = 1
   end

For high performance use bitwise operators instead of the multiplication.

Please note my remarks at http://forum.micasaverde.com/index.php?topic=3855.msg20588#msg20588

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.

Browse the code here: I_CaddxNX584Security.xml in trunk – GE Caddx Networx NX-584 NX-8E Alarm Plugin

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.

All
Thanks for your replies, am playing with Ap15e’s code and hopefully at some stage have something to show for it :slight_smile:

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 :frowning:

[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>

Any help appreciated.

@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.

:-[ I am a newbie.

if( incoming_data == nil ) then

Well, we were all there once :). Good luck on your endevor.