21.06.2022, 13:44
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