18.10.2023, 07:05
Try this, make sure that only one script is accessing the serial port at any time moment.
Code:
require('serial')
port, err = serial.open('/dev/RS232', {
baudrate = 9600,
parity = 'even',
duplex = 'full',
})
port:flush()
addr = 0x01 -- short address
tariff = 0x02 -- selected tariff
data = { 0x53, addr, 0x51, 0x09, 0x7C, 0x01, 0x54, tariff }
len = #data
buf = { 0x68, len, len, 0x68 }
cs = 0
for _, char in ipairs(data) do
buf[ #buf + 1 ] = char
cs = cs + char
end
buf[ #buf + 1 ] = bit.band(cs, 0xFF)
buf[ #buf + 1 ] = 0x16
data = string.char(unpack(buf))
port:write(data)
res = port:read(1, 0.5)
if res then
if res:byte(1) == 0xE5 then
log('write ok')
else
log('reply error')
end
else
log('no reply')
end
port:close()