New: the AI firewall watches the whole platform and explains each incident in plain words. See how it works

Reply To: USB-RS232 scale indicator on Pi: permission error, ttyUSB0/ttyUSB1 changing, readline hanging

Translated from Türkçe by AI ·

I experienced both, in a weighing scale project.Port name: the ttyUSB number is assigned based on enumeration order, so don’t trust it. Check with ls -l /dev/serial/by-id/, there is a permanent name containing the converter’s serial number, use that. If you have two of the same chip and no serial numbers, you can name them by USB port with a udev rule, but most of the time by-id is enough.readline: by default it waits for
. Most indicators end lines with , so it never returns. Also, if you don’t set a timeout, it waits forever when the cable is unplugged. What I use:

PORT = '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A10K3XYZ-if00-port0'
ser = serial.Serial(PORT, 9600, bytesize=8, parity='N', stopbits=1, timeout=2)
buf = b''
while True:
    chunk = ser.read(64)
    if not chunk:
        continue          # no data for 2 s, log here
    buf += chunk
    while b'' in buf:
        line, buf = buf.split(b'', 1)
        w = parse_weight(line)   # e.g. b'ST,GS,+0012.45kg'
        if w is not None:
            client.publish('v1/telemetry', json.dumps({'weight_kg': w}), qos=1)

If the indicator prints several times a second, don’t send every line. Send once per second, or only the ST (stable) lines. Write a systemd unit for the service, put your own user in the User= line, and use Restart=always.

Ağ geçidi yine bağlanıyor…
Accepted solution♥ 5