Traducido del Türkçe por IA ·
Viví las dos cosas, en un proyecto de báscula.Nombre del puerto: el número ttyUSB se asigna según el orden de enumeración, así que no te fíes de él. Revisa con ls -l /dev/serial/by-id/, ahí hay un nombre permanente que contiene el número de serie del convertidor, úsalo. Si tienes dos chips iguales sin números de serie, puedes nombrarlos por puerto USB con una regla udev, pero casi siempre basta con by-id.readline: por defecto espera . La mayoría de los indicadores terminan las líneas con
, así que nunca retorna. Además, si no pones timeout, espera para siempre cuando se desconecta el cable. Esto es lo que uso:
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 # sin datos durante 2 s, registrar aquí
buf += chunk
while b'' in buf:
line, buf = buf.split(b'', 1)
w = parse_weight(line) # p. ej. b'ST,GS,+0012.45kg'
if w is not None:
client.publish('v1/telemetry', json.dumps({'weight_kg': w}), qos=1)
Si el indicador imprime varias veces por segundo, no envíes cada línea. Envía una vez por segundo, o solo las líneas ST (estable). Escribe una unit de systemd para el servicio, pon tu propio usuario en la línea User= y usa 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.

