I am using a USB-to-serial adapter to let the Raspberry Pi talk with some motor controller through the serial bus.
I made the following Python script:
#!/usr/bin/ipython import serial import time ser =serial.Serial( port='/dev/ttyUSB0', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, writeTimeout = 0, timeout = 10, rtscts=False, dsrdtr=False, xonxoff=False) print("Port " + ser.portstr + " opened : " + str(ser.isOpen())) time.sleep(5) bytesToRead = ser.inWaiting() print ser.read(bytesToRead) ser.close()
The first time the code is executed everything works and gives the following expected answer:
>>> pi@raspberrypi ~ $ sudo ./Work/Tests/Test_Serial_1.py >>> Port /dev/ttyUSB0 opened : True >>> >>> ReceiveMessage: CanInID = 2, CanControllerID = 1, CanBroadcastID = 0 >>> >>> ReceiveMessage: CanInID = 2, CanControllerID = 1, CanBroadcastID = 0
The problem is now that if I directly re-run the script, I get the following incoming messages:
>>> pi@raspberrypi ~ $ sudo ./Work/Tests/Test_Serial_1.py >>> Port /dev/ttyUSB0 opened : True >>> wĸ7�a)� �7ę
The only solution is then to reboot the Raspberry Pi. I tried unplugging/replug the USB-to-serial adapter, but nothing changed.
It is like if the baud rate were wrong the second time, but the connected devices are exactly the same.
My setup is Raspberry Pi 2, Linux raspberrypi 4.1.10-v7+, and the USB-to-serial adapter is there.
How can I fix this problem?