23.03.2023, 08:41
Ok then the script is already more or less correct. Use this to add an alert if 5 timeouts in a row happen.
Code:
if not server then
require('socket')
server = socket.udp()
server:setsockname('127.0.0.1', 5485)
server:settimeout(60)
end
if not port then
require('serial')
port = serial.open('/dev/RS485-1', {
baudrate = 19200,
databits = 8,
stopbits = 1,
parity = 'none',
duplex = 'half'
})
timeouts = 0
end
data = server:receive()
if data then
port:write(data)
os.sleep(0.5)
end
port:flush()
port:write(':RR\r\n')
data, err = port:read(17, 1) -- read 17 bytes for up to 1 second
if data and #data == 17 then
timeouts = 0
-- parse data response here
else
timeouts = timeouts + 1
if timeouts == 5 then
-- send alert
end
end