Traduit de English par IA ·
Je suis nouveau côté matériel, mais j’ai fait du travail sur les protocoles série en logiciel pendant des années, et on peut simuler cela avec la parité. Le 9e bit est envoyé à la place du bit de parité. Linux prend en charge la parité « stick » via le drapeau CMSPAR : avec PARENB|CMSPAR|PARODD, le bit de parité vaut toujours 1 (mark), sans PARODD il vaut toujours 0 (space).Donc : régler la parité mark, écrire l’octet d’adresse, attendre qu’il soit parti (tcdrain), passer en parité space, écrire les octets de données. En Python :
import termios, serial
CMSPAR = 0o10000000000
s = serial.Serial('/dev/ttyAMA0', 9600)
def set_bit9(on):
a = termios.tcgetattr(s.fd)
a[2] |= termios.PARENB | CMSPAR
a[2] = a[2] | termios.PARODD if on else a[2] & ~termios.PARODD
termios.tcsetattr(s.fd, termios.TCSADRAIN, a)
set_bit9(True); s.write(bytes([0x03])); s.flush()
set_bit9(False); s.write(bytes([0x10, 0x00]))
La réception est la partie la plus pénible : activez PARMRK et INPCK, et un octet avec la « mauvaise » parité arrive précédé de 0xFF 0x00, ce qui vous indique son 9e bit. À 9600 bauds, le temps entre l’octet d’adresse et les octets de données présente un petit écart dû au changement de mode, mais la plupart des anciens appareils s’en moquent. Utilisez l’UART PL011 (disable-bt), et non le mini UART, car le mini UART n’a pas de parité.Ensuite, chaque indicateur est envoyé comme un enfant, par exemple {« scale-1 »: {« values »: {« kg »: 12.45}}} sur v1/gw/telemetry.
I’m new to the hardware side but I did serial protocol work in software for years, and you can fake this with parity. The 9th bit is sent where the parity bit would be. Linux supports « stick » parity through the CMSPAR flag: with PARENB|CMSPAR|PARODD the parity bit is always 1 (mark), without PARODD it’s always 0 (space).
So: set mark parity, write the address byte, wait for it to leave (tcdrain), switch to space parity, write the data bytes. In Python:
import termios, serial
CMSPAR = 0o10000000000
s = serial.Serial('/dev/ttyAMA0', 9600)
def set_bit9(on):
a = termios.tcgetattr(s.fd)
a[2] |= termios.PARENB | CMSPAR
a[2] = a[2] | termios.PARODD if on else a[2] & ~termios.PARODD
termios.tcsetattr(s.fd, termios.TCSADRAIN, a)
set_bit9(True); s.write(bytes([0x03])); s.flush()
set_bit9(False); s.write(bytes([0x10, 0x00]))
Receiving is the uglier half: turn on PARMRK and INPCK and a byte with the « wrong » parity arrives prefixed with 0xFF 0x00, which tells you its 9th bit. At 9600 the timing between the address and the data bytes gets a small gap from switching, most old devices don’t care. Use the PL011 UART (disable-bt), not the mini UART, the mini UART has no parity.
Then each indicator goes out as a child, something like {« scale-1 »: {« values »: {« kg »: 12.45}}} on v1/gw/telemetry.

