Logic Machine Forum
RS485 alarm if the other device does not respond in certain time - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: RS485 alarm if the other device does not respond in certain time (/showthread.php?tid=4097)



RS485 alarm if the other device does not respond in certain time - Hadeel - 17.06.2022

Hello  Smile

We are going to control a device with LM RS485.
LM is the master and the other device is the slave.

When sending command from LM to the other device, the other device is suppose to give LM a respond in at most 3 seconds.
However if the device does not give any responce in 5 min, I need to consider that as an error and indicate an alert.

Is it possible to do such thing (keep observing the signal for 5 min) in resident script or event script?

Thank you for your help in advance!


RE: RS485 alarm if the other device does not respond in certain time - admin - 17.06.2022

If using Modbus profiles there's a built-in device status object functionality. Otherwise in your script you can use os.time() to store the last device response time and compare it with current time. Or just count the number of failed poll requests. This has to be done inside the script that is doing the RS485 communication.


RE: RS485 alarm if the other device does not respond in certain time - Hadeel - 21.06.2022

Thank you admin! This time the system is not Modbus, it is RS485 ASCII. so I will use os.time() to store the last device responce and compare it with current time in resident script .

I would also like to have your suggestion about what timeout I should use in the
port:read(bytes, timeout) method.

The condition is, as I wrote in the first thread,
>When sending command from LM to the other device, the other device is suppose to give LM a respond in at most 3 seconds.
> However if the device does not give any responce in 5 min, I need to consider that as an error and indicate an alert.

I am sorry, maybe it's a basic questions but I appreciate if you could help (:

This is the code I have.
```
if not port then
require('serial')
port = serial.open('/dev/RS485-1', {
baudrate = 19200,
databits = 8,
stopbits = 1,
parity = 'none',
duplex = 'half'
})
end

port:flush()
res, err = port:read(27, 3)

--example res = ":00010001130077770010AB"
t = split(res, ":")

for k,v in pairs(t) do
~~
end
```


RE: RS485 alarm if the other device does not respond in certain time - admin - 21.06.2022

You don't even need to store the time, you can simply count the number of times the device did not reply. Check the res variable contents because it will be nil on error and trying to manipulate res as a string will result in a script error.

Code:
res, err = port:read(27, 3)
if res then
  errors = 0
  -- parse res contents
else
  errors = (errors or 0) + 1
  if errors == 100 then
    alert('device read timeout')
  end
end



RE: RS485 alarm if the other device does not respond in certain time - Hadeel - 22.06.2022

Thank you !
Yes as you saild, my script will result in an error if there's no response from the port ....(:

If I put your code in a resident script with interval 0, how often does the errors count up?

The count starts every time I try to write to the port?


RE: RS485 alarm if the other device does not respond in certain time - admin - 22.06.2022

Is the device constantly sending data or do you need to poll for it? For polling you should not use 0 sleep time. If you increase the sleep time then the number of errors for the alert should be adjusted accordingly. If the device doesn't respond then each cycle time = sleep time + timeout value. If you poll every 2 seconds with 3 second timeout then the whole cycle is 5 seconds. Then the counter value of 60 equals 5 minutes.