Last active
January 26, 2017 11:20
-
-
Save jsopenrb/1e40d78d0773f6f2176adcb32a145a67 to your computer and use it in GitHub Desktop.
Denver e-Ribbon Fire Lua example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function send(dev, req) | |
local port, b1, b2, cs, data, res, err | |
-- checksum calculation | |
function getcs(b1, b2) | |
return 0xFF - bit.band(b1 + b2, 0xFF) | |
end | |
require('serial') | |
port = serial.open(dev, { baudrate = 19200 }) | |
-- get two bytes from request | |
b1 = bit.rshift(req, 8) | |
b1 = bit.band(b1, 0xFF) | |
b2 = bit.band(req, 0xFF) | |
-- calculate checksum | |
cs = getcs(b1, b2) | |
-- send data | |
data = string.char(0x01, b1, b2, cs, 0x04) | |
port:flush() | |
port:write(data) | |
-- wait for reply | |
res, err = port:read(#data, 1) | |
port:close() | |
-- valid reply | |
if type(res) == 'string' and #res == #data then | |
b1, b2, cs = res:byte(2, 4) | |
-- verify checksum | |
if cs == getcs(b1, b2) then | |
return b1, b2 | |
-- invalid checksum | |
else | |
return nil, 'invalid checksum' | |
end | |
-- no reply or invalid reply | |
else | |
return nil, err | |
end | |
end | |
function getstatus(port, data) | |
local b1, b2 = send(port, data) | |
if b1 then | |
b1 = bit.band(b1, 0x0F) | |
b1 = bit.lshift(b1, 8) | |
return bit.bor(b1, b2) | |
else | |
return nil, b2 | |
end | |
end | |
-- send flame5 command | |
log(send('/dev/RS232', 0x0005)) | |
-- fuel level status | |
log(getstatus('/dev/RS232', 0x9000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment