I'm using an Arduino Mega to send data via serial communication to a Raspberry Pi 5. The connection is made through a TTL-232R cable (TTL serial on the Arduino side, USB on the Raspberry Pi side). On the Raspberry Pi, I'm using Python with the PySerial library to handle the communication.
Although I can successfully send data, I receive the following unexpected string immediately after opening the serial port:
\x00\x00267
Interestingly, this issue does not occur when I connect the Arduino to a Windows PC instead of the Raspberry Pi. My suspicion is that the serial communication parameters (parity, stop bits, xon/xoff flow control, etc.) might be configured differently on Windows compared to Raspbian. I have played with the number of stop bits with no success.
These parameters are set in Python using serial module:
import serial import serial.tools.list_ports class Serial_comm: EstadoPuerto = False def __init__(self): self.initialize() def initialize(self): self.port = serial.Serial() self.port.stopbits = 1 self.port.parity = serial.PARITY_NONE self.port.xonxoff = False self.port.rtscts = False def serieOpen(self,port, baud): self.port.port = port self.port.baudrate = baud try: self.port.open() if self.port.isOpen(): msg = "Puerto abierto con Exito" self.EstadoPuerto = True except Exception as e: msg = "Puerto no disponible: " + str(e) return msg # Read the port until the end of the line def rdv(self): try: msg = self.port.readline() except Exception as e: msg = "Error de lectura en el puerto: " + str(e) return msg Regarding the OS installed, I need to check, but I think it is Raspberry Pi OS with desktop version 12 (bookworm). serial module was installed using pip.
I’m wondering: how can I prevent receiving these extra or malformed characters when opening the serial port? Or, in other words, how can I check and compare the serial port configuration between Windows and Raspberry Pi OS to ensure consistency?
pipto install system code useapt.pipcan not install system code it can install user code but it is almost always the best option to install the package provided by the OS developers. Forget the peripheral issues, post some executable code and diagnostics.