19.04.2023, 06:47
Check system log in system config. Most likely your USB device reconnected and changed the port name from /dev/ttyUSB0 to /dev/ttyUSB1.
You can use this function to find the port name. It will return nil if no port is found.
You should check for read errors to detect USB disconnect:
You can use this function to find the port name. It will return nil if no port is found.
Code:
function findport()
local path = '/sys/bus/usb-serial/devices'
local ports = io.ls(path) or {}
if type(ports) == 'table' and #ports > 0 then
return '/dev/' .. ports[ 1 ]
end
end
You should check for read errors to detect USB disconnect:
Code:
res, err = port:read(...)
if err == 'read error' then
-- USB disconnected
end