Python Forum

Full Version: MCU reboots after opening Serial port when ran from Raspberry PI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I am running the following via the Raspberry PI :
    serialPort = serial.Serial()
    serialPort.baudrate = 115200
    serialPort.bytesize = serial.EIGHTBITS
    serialPort.timeout = 1
    serialPort.stopbits = serial.STOPBITS_ONE
    serialPort.port = target_port
    serialPort.rts = 0
    serialPort.dtr = 0
    serialPort.open()
    if serialPort.is_open:
        print(f"Port {target_port} is now open.")
    else:
        print(f"Failed to open port {target_port}.")
However, the device that I am connecting to reboots. When I try to do this via my Windows machine, it does not reboot. Perhaps anyone know why this could be the case even though I have set rts and dtr to 0?


Since the device reboots after connecting to the serialport, during the bootup, it prints out a bunch of garbage that I want to filter out. So I decided to sleep for 2 seconds and then flush serial port:

    serialPort = serial.Serial()
    serialPort.baudrate = 115200
    serialPort.bytesize = serial.EIGHTBITS
    serialPort.timeout = 1
    serialPort.stopbits = serial.STOPBITS_ONE
    serialPort.port = target_port
    serialPort.rts = 0
    serialPort.dtr = 0
    serialPort.open()
    if serialPort.is_open:
        print(f"Port {target_port} is now open.")
    else:
        print(f"Failed to open port {target_port}.")
    time.sleep(2)
    serialPort.flush()
but that does not work. Even after flushing serial port, there is still bunch of logs accumulated. How can I ensure the serial port is flushed?


So to summarize, my questions are as following:

1. Why would device reboot when connecting via Raspberry PI
2. How to flush serial port properly?
(Mar-15-2024, 09:25 AM)zazas321 Wrote: [ -> ]However, the device that I am connecting to reboots. When I try to do this via my Windows machine, it does not reboot. Perhaps anyone know why this could be the case even though I have set rts and dtr to 0?

Which Microcontroller are you using and which firmware is on the Microcontroller?
I never used rts or dts directly with ESP32, ESP32C3, ESP32S23 or RP Pico W.

ser = Serial(PORT, baudrate=115200) is enough.

AFIK there are some microcontrollers, which do a reset, if USB is reconnected, even if 5V/3.3V supply feed in through the 3.3V or 5V Pin.

I think the best way is, to use a second UART on the Microcontroller via the GPIOs. Mostly all Microcontrollers work with 3.3V. The RPi too.
So you can use the GPIO-Pins to connect your Microcontroller directly without USB to your RPi. RX, TX and GND is required.

The RPi could also supply the Microcontroller with 5V. The use of the 3.3V supply from the regulator is limited in current. If the Microcontroller takes too much current, the RPi will brown out and reboot.

On the USB-Port you have the Micropython-REPL, if Micropython is used. A second UART can be used to exchange Data between RPi and your Microcontroller. If Micropythonis used, the benefit is, that the REPL is not in your way.
(Mar-15-2024, 10:13 AM)DeaD_EyE Wrote: [ -> ]
(Mar-15-2024, 09:25 AM)zazas321 Wrote: [ -> ]However, the device that I am connecting to reboots. When I try to do this via my Windows machine, it does not reboot. Perhaps anyone know why this could be the case even though I have set rts and dtr to 0?

Which Microcontroller are you using and which firmware is on the Microcontroller?
I never used rts or dts directly with ESP32, ESP32C3, ESP32S23 or RP Pico W.

ser = Serial(PORT, baudrate=115200) is enough.

AFIK there are some microcontrollers, which do a reset, if USB is reconnected, even if 5V/3.3V supply feed in through the 3.3V or 5V Pin.

I think the best way is, to use a second UART on the Microcontroller via the GPIOs. Mostly all Microcontrollers work with 3.3V. The RPi too.
So you can use the GPIO-Pins to connect your Microcontroller directly without USB to your RPi. RX, TX and GND is required.

The RPi could also supply the Microcontroller with 5V. The use of the 3.3V supply from the regulator is limited in current. If the Microcontroller takes too much current, the RPi will brown out and reboot.

On the USB-Port you have the Micropython-REPL, if Micropython is used. A second UART can be used to exchange Data between RPi and your Microcontroller. If Micropythonis used, the benefit is, that the REPL is not in your way.


I am connecting ESP32 development board that is running simple "Hello World" firmware. There must be something that SerialPort or the Raspberry does that causes the device to reboot.

Because I connect to the ESP32 serial port via PC using Termite serial terminal software and the ESP32 will not reboot.
UPDATE

What I found I can use as an alternative:

serialPort = serial.Serial()
serialPort.baudrate = 115200
serialPort.bytesize = serial.EIGHTBITS
serialPort.timeout = 1
serialPort.stopbits = serial.STOPBITS_ONE
serialPort.port = target_port
serialPort.rts = 0
serialPort.dtr = 0
serialPort.open()
if serialPort.is_open:
    print(f"Port {target_port} is now open.")
else:
    print(f"Failed to open port {target_port}.")
time.sleep(2)
serialPort.readall()
As you can see from the code above, instead of flushing I can readall() instead to read and discard all the data from the serial port and that seems to work. However, I would still like to know why the serial flush would not work?