top of page

Software

Public·144 members

Fixed the issue on Github: New serial code doesn't work with RPi hat #16

We have optimized the serialMaster on GitHub recently, there is a new issue we fixed just now:

New serial code doesn't work with RPi hat #16(https://github.com/PetoiCamp/OpenCat/issues/16)

We encountered a problem: pyserial (version: 3.5) cannot recognize the Raspberry Pi 3B+ serial port "/dev/ttyS0"

When using the Raspberry Pi 3B + its own serial port (ttyS0) to connect to the product, use the following command to query the serial device on the Raspberry Pi terminal to find the port numbers of the two serial devices:

ls -l /dev

ttyAMA0 ttyS0

But only one of the port numbers can be found by calling the pyserial module of python3:

python3 -m serial.tools.list_ports -v

/dev/ttyAMA0

desc: ttyAMA0

hwid: 3f201000.serial

1 ports found

What's more tragic is that we actually use /dev/ttyS0

There are two possibilities to analyze the root cause:

1. There is a bug in the pyserial module

2. The system parameters of the Raspberry Pi 3B+ device are incorrectly configured

However, after searching on the Internet for a long time, no relevant authoritative explanation was found. In desperation, combined the characteristics of the product itself and made a roundabout solution:

The idea is as follows:

1. If you only use the Raspberry Pi 3B+ own serial port (ttyS0) to connect to the product, at this time the pyserial module can only find one of the ports, and it is not what we need, so directly use "/dev/ttyS0" as a parameter to open it Serial port function:


if platform.uname()[1] == 'raspberrypi':

    serialPort = '/dev/ttyS0'  # needed when plug in RaspberryPi

    ser = Communication(serialPort, 115200, 0.5)

    logger.info(f"Connect to usb serial port: {serialPort}.")


2. If you use Raspberry Pi 3B+ as the host computer (use Raspberry Pi 3B+'s USB port, data cable, and on the Bittle side uses USB adapter to connect to the product, in this case, you should use the serial port number "/dev/ttyUSB0" to connect to the product), or While using the Raspberry Pi 3B+'s own serial port (ttyS0) to connect to the Bittle, the Raspberry Pi 3B+'s USB port is also connected to other serial devices. At this time, the pyserial module will find multiple ports, and also consider compatibility with other operating system platforms. Therefore, the following solution is adopted-If you are using Raspberry Pi 3B+, connect directly to the serial port "/dev/ttyS0", send the serial port command, and determine whether to continue to use the "/dev/ttyS0" connection product by checking the feedback of the serial port command If the serial port feedback is an empty string b'', use the first port (port[serialPortIndex]) ("/dev/ttyUSB0") in the serial port number list port_list_number that the pyserial module queried.


Communication.Print_Used_Com()

port = port_list_number

total = len(port)

index = 0

for index in range(total):

logger.info(f"port[{index}] is {port[index]} ")


if platform.uname()[1] == 'raspberrypi':

serialPort = '/dev/ttyS0' # needed when plug in RaspberryPi

ser = Communication(serialPort, 115200, 0.5)

logger.info(f"Connect to usb serial port: {serialPort}.")

serialWriteByte(["d"])

time.sleep(0.1)

response = ser.main_engine.read_all()

logger.info(f"Response is: {response}")

if response == b'':

ser.Close_Engine()

logger.info(f"close the serial port: {serialPort}.")

serialPortIndex = 0 # 0 means connect to port[0]; -1 means connect to the last port in the list

ser = Communication(port[serialPortIndex], 115200, 0.5)

logger.info(f"Connect to serial port: {port[serialPortIndex]}.")


Reference:

https://forums.raspberrypi.com/viewtopic.php?t=188233

https://pyserial.readthedocs.io/en/latest/tools.html





52 Views
bottom of page