ttyAMA0, ttyS0, serial0: ¿cuál debería usar mi HAT Modbus en una Pi 3B+?
Traducido del English por IA ·
Pi 3B+, Raspberry Pi OS Bookworm, un HAT RS485 en el header GPIO. Un script en Python con minimalmodbus consulta un medidor de energía trifásico a 9600 8N1, dirección 5, y publica kW y kWh en Hermetry cada minuto.Problema: aproximadamente una de cada cinco consultas falla. Puede ser un timeout o un error CRC. El mismo medidor, conectado a mi portátil mediante un adaptador USB-RS485, responde siempre. Mi script abre /dev/ttyAMA0 porque eso es lo que usaba el ejemplo del fabricante del HAT. Luego leí un foro que recomendaba ttyS0, otro que recomendaba serial0, y sinceramente ya no sé qué es ttyAMA0.
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
minimalmodbus.InvalidResponseError: Checksum error in rtu mode: 'x8ax1f' instead of 'x0exc4' .
¿Cuál es la diferencia y cuál debo usar?
Pi 3B+, Raspberry Pi OS Bookworm, an RS485 HAT on the GPIO header. Python with minimalmodbus polls a three-phase energy meter at 9600 8N1, address 5, and publishes kW and kWh to Hermetry every minute.
Problem: about one poll in five fails. Either a timeout or a CRC error. The same meter on a USB-RS485 stick on my laptop answers every time. My script opens /dev/ttyAMA0 because that’s what the HAT vendor’s example used. Then I read a forum post saying to use ttyS0, and another saying serial0, and now I honestly don’t know what ttyAMA0 even is.
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
minimalmodbus.InvalidResponseError: Checksum error in rtu mode: 'x8ax1f' instead of 'x0exc4' .
What’s the difference and which one do I want?
Traducido del English por IA ·
¿ttyAMA0 es algo USB? Siempre pensé que los nombres ACM/USB eran para USB.
Is ttyAMA0 a USB thing? I always thought ACM/USB names were for USB.
Traducido del English por IA ·
No es USB. Una vez tuve Modbus inestable en una Pi 3 y parte de la solución fue quitar console=serial0,115200 de cmdline.txt, porque la consola de inicio de sesión compartía el puerto. No estoy seguro de que sea todo tu problema.
Not USB. I had flaky Modbus on a Pi 3 once and part of the fix was taking console=serial0,115200 out of cmdline.txt, the login console was sharing the port. Not sure that’s your whole problem though.
Traducido del English por IA ·
Lo que dice Emily es parte del asunto. El resto es el lío de UART de la Pi 3, que atrapa a todo el mundo.La Pi 3/4/Zero W tiene dos UART. ttyAMA0 es la PL011, la buena, con su propio reloj. ttyS0 es la mini UART, cuya velocidad en baudios se deriva del reloj del núcleo VPU; cuando el reloj del núcleo cambia, la velocidad se desvía y obtienes exactamente tus errores CRC de uno de cada cinco. En esos modelos, la PL011 está conectada por defecto al Bluetooth integrado y el conector GPIO recibe la mini UART. /dev/serial0 es un alias que siempre apunta a la que está en GPIO14/15.Así que, según tu configuración, estabas hablando con el chip Bluetooth o con la mini UART. Devuelve la PL011 al conector:
# /boot/firmware/config.txt (Bookworm)
enable_uart=1
dtoverlay=disable-bt
sudo systemctl disable hciuart
# y elimina console=serial0,115200 de /boot/firmware/cmdline.txt
sudo reboot
Después, abre /dev/serial0 en tu script, para que siga funcionando si cambias a otro modelo. Pierdes el Bluetooth integrado, que en una caja de lectura de contadores no vas a echar de menos.Si necesitas Bluetooth, la otra opción es core_freq=250 para fijar el reloj de la mini UART, pero prefiero la PL011.
Emily’s bit is part of it. The rest is the Pi 3 UART shuffle, which catches everyone.
The Pi 3/4/Zero W have two UARTs. ttyAMA0 is the PL011, the good one with its own clock. ttyS0 is the mini UART, whose baud rate is derived from the VPU core clock, so when the core clock moves the baud drifts and you get exactly your one-in-five CRC errors. On those models the PL011 is wired to the onboard Bluetooth by default and the GPIO header gets the mini UART. /dev/serial0 is an alias that always points at whichever one is on GPIO14/15.
So depending on your config you were either talking to the Bluetooth chip or to the mini UART. Give the PL011 back to the header:
# /boot/firmware/config.txt (Bookworm)
enable_uart=1
dtoverlay=disable-bt
sudo systemctl disable hciuart
# and remove console=serial0,115200 from /boot/firmware/cmdline.txt
sudo reboot
Then open /dev/serial0 in your script, so it keeps working if you move to another model. You lose onboard Bluetooth, which on a meter-reading box you don’t miss.
If you need Bluetooth, the other option is core_freq=250 to pin the clock for the mini UART, but I’d rather have the PL011.
Traducido del English por IA ·
disable-bt más quitar la consola lo solucionó. 600 consultas durante la noche, cero errores CRC, cero timeouts. Estaba usando ttyAMA0 y aparentemente hablaba con… nada útil, y el script recurría a reintentos que a veces funcionaban. Lo cambié a serial0. Kevin, ttyAMA es la nomenclatura ARM PL011, no USB.
disable-bt plus removing the console did it. 600 polls overnight, zero CRC errors, zero timeouts. I was on ttyAMA0 talking to… nothing useful apparently, and the script fell back to retries that sometimes hit. Changed it to serial0. Kevin, ttyAMA is the ARM PL011 naming, not USB.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

