23.12.2022, 08:30
Example of sending data packets:
Code:
port = require('serial').open('/dev/ttyUSB0', { baudrate = 9600 })
function send(...)
local data = string.char(...)
local len = #data + 1
local hdr = string.char(0xFF, 0xFF, len)
local csum = len
for _, byte in ipairs({ ... }) do
csum = bit.band(csum + byte, 0xFF)
end
local pkt = hdr .. data .. string.char(csum)
port:write(pkt)
end
send(0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x4D, 0x02) -- turn on
send(0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x4D, 0x03) -- turn off
send(0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x4D, 0x01) -- request state