Python Forum
MCU reboots after opening Serial port when ran from Raspberry PI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MCU reboots after opening Serial port when ran from Raspberry PI
#1
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?
Reply
#2
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(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.
Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,100 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,161 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Serial Port As Global Prasanjith 2 1,517 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  python serial port barryjo 2 1,662 Dec-27-2021, 11:09 PM
Last Post: barryjo
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,494 Sep-09-2021, 03:15 PM
Last Post: Creepy
  How to Properly Open a Serial Port in a Function bill_z 2 4,494 Jul-22-2021, 12:54 PM
Last Post: bill_z
Question Python3 - serial port reload parovelb 4 5,920 Apr-08-2021, 09:18 AM
Last Post: parovelb
  Unable to read from serial port br0kenpixel 1 2,496 Aug-08-2020, 10:03 PM
Last Post: Larz60+
  Port my python program to Raspberry pi seamlessly Hassibayub 1 1,951 Jun-29-2020, 12:58 PM
Last Post: snippsat
  Read Data from Serial Port PA3040 3 3,201 Feb-16-2020, 04:54 AM
Last Post: PA3040

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020