OK, slight delay and re-think on this as the HAI serial protocol is binary rather than ASCII based.
I’ve got the Lua code for handling messages roughed out, and a CRC16 generator tested and working.
Update:
The basic comms routines are now working, the plugin is requesting data from the panel and getting correctly formatted response messages back.
I now have to work through handling all the response types.
The CRC16 routine is listed below, in case it’s of use to others.
It uses the ‘bit manipulation’ library - eg. include
local bit = require(“bit”)
Edit - single byte handler moved before the main string CRC routine as the vera does not like forward references.
---- Short table CRC16 function ----
-- Based on the C code on Jim Mahoneys web pages on marlborough.edu --
-- Uses 0xA001 polynomial with CRC register initialised to 0. --
CRC16_Table = {0x0000,0xcc01,0xd801,0x1400,0xf001,0x3c00,0x2800,0xe401,0xa001,0x6c00,0x7800,0xb401,0x5000,0x9c01,0x8801,0x4400}
function CRC16_Byte(x)
local z = 0
-- Do lower four bits of x --
z = CRC16_Table[bit.band(crc_reg, 0x0f)+1]
crc_reg = bit.band(bit.rshift(crc_reg, 4), 0xffff)
crc_reg = bit.bxor(crc_reg, z, CRC16_Table[bit.band(x, 0x0f)+1])
-- Do upper four bits of x --
z = CRC16_Table[bit.band(crc_reg, 0x0f)+1]
crc_reg = bit.band(bit.rshift(crc_reg, 4), 0xffff)
crc_reg = bit.bxor(crc_reg, z, CRC16_Table[bit.band(bit.rshift(x,4), 0x0f)+1])
end
local function CRC16(buf)
crc_reg = 0
local crc_l = 0
local crc_h = 0
len = string.len(buf)
if len < 1 then return 0, 0 end
local i = 0
local x = 0
for i = 1, len do
x = buf:byte(i)
CRC16_Byte(x)
end
crc_l = bit.band(crc_reg, 0xff)
crc_h = bit.band(bit.rshift(crc_reg,8), 0xff)
return crc_l, crc_h
end