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.
İkisini de yaşadım, bir kantar projesinde.
Port adı: ttyUSB numarası takılma sırasına göre veriliyor, güvenme. ls -l /dev/serial/by-id/ ile bak, orada çeviricinin seri numaralı kalıcı adı var, onu kullan. Aynı çipten iki tane varsa ve seri numaraları yoksa udev kuralıyla USB portuna göre isim verirsin, ama çoğu zaman by-id yetiyor.
readline: varsayılan olarak bekliyor. Çoğu indikatör satırı
ile bitiriyor, o yüzden hiç dönmüyor. Bir de timeout vermezsen kablo çıktığında da sonsuza kadar bekler. Benim kullandığım:
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 # 2 sn veri yok, burada log yaz
buf += chunk
while b'
' in buf:
line, buf = buf.split(b'
', 1)
w = parse_weight(line) # örn. b'ST,GS,+0012.45kg'
if w is not None:
client.publish('v1/telemetry', json.dumps({'weight_kg': w}), qos=1)
İndikatör saniyede birkaç kez basıyorsa her satırı gönderme. Hermetry’de cihaz başına varsayılan limit saniyede 10 mesaj, üstü sessizce düşüyor. Saniyede bir, ya da sadece ST (stabil) satırlarını yolla. Servis için systemd unit yaz, User= satırına kendi kullanıcını koy, Restart=always.

