Serial data from ESP32 cam only shows up in Serial monitor but not by pyserial

I want to transmit data from ESP32 cam to my PC using serial port. I want to use pyserial to capture data on PC. Right now I am having a very simple loop printing "hello world" from ESP32 cam like this:

#include "Arduino.h"
 void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Hello World!\n");
  delay(500);
}

I can see hello world after selecting the port(COM9) at the serial monitor. However, when I am using pyserial in Python like this:

import serial, time
import sys

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("python script serial_port baudrate")
        print("python -m serial.tools.list_ports")
        exit()
    port_name = sys.argv[1]
    baud = sys.argv[2]
    ser = serial.Serial(port_name, baudrate= baud, timeout = 2)
    print("serial connected")
    
    while True:
        value = ser.readline()
        print("serial read")
        line = str(value, encoding="UTF-8")
        print(line)

I can connect to the port(COM9), but nothing is captured(time out and no bytes read). I tried this python program on serial input from other microcontroller(MSP432P401R), and I can capture data properly. I am setting both end to be default like 8N1 and the same baudrate for UART. Does serial monitor have some special setting with UART?

What does pyserial default to, hardware or software handshake, or none?

I believe it's default to no hand shaking but it can be changed by parameters

  • xonxoff (bool) – Enable software flow control.
  • rtscts (bool) – Enable hardware (RTS/CTS) flow control.
  • dsrdtr (bool) – Enable hardware (DSR/DTR) flow control.

Does serial print from Arduino require handshaking? If so which should I use

Nope it doesn't

@elberto I tried your Arduino code with your Python code on a Win 11 desktop.

I loaded the c/c++ code onto the ESP32 (not an ESP32 cam)

I named the Python file "cmd_line.py" and using Command Prompt I navigated to the file location. I then ran the Python file like this python cmd_line.py "COM9" 9600

Do you see any errors or anything different about the way I am doing it?

It did work btw

Thanks for trying it out. Yes, that's pretty much what I did, except I am using ESP32 cam of course. I didn't encounter any error, just no data from the port. Maybe there is something different on the UART pins of the ESP32 cam.

try

# terminal.py - simple terminal emulator - requires pyserial

import serial
import sys
import msvcrt
import time

serialPort = serial.Serial(
    port="COM10", baudrate=115200, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE
)
serialPort.rtscts = False
serialPort.xonxoff = False
serialPort.dsrdtr = False 
sys.stdout.write("Python terminal emulator \n")
serialPort.write('hello terminal\n'.encode())
while 1:
    try:
        # serial data received?  if so read byte and print it
        if serialPort.in_waiting > 0:
            char = serialPort.read()
            #sys.stdout.write("\nreceived ")
            sys.stdout.write(str(char, 'ASCII'))
            #serialPort.write(str(char, 'ASCII'))
        # keyboard hit? if so read key and transmit over serial
        if msvcrt.kbhit(): 
            char = msvcrt.getch()
            if char == u'\x1b':
                sys.stdout.write("Exiting")
                quit()
                exit(0)
            serialPort.write(char)
            sys.stdout.write(char.decode())
    except:
        pass

connected to a ESP32

F:\Programming\python\Serial>python terminal_3.py
Python terminal emulator
hello terminal
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4888
load:0x40078000,len:16516
load:0x40080400,len:4
load:0x40080404,len:3476
entry 0x400805b4


ESP32 serial1  test RXD1 pin 16 TXD1 pin 17
 loopback test connect pin 16 to pin 17
RS232: ESP32 pin 16 RXD1 to TTL/RS232 Rx and pin 17 TXD1 to TTL/RS232 Tx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx
llooooppbbaacckk  tteesstt11

I’ve written a small tutorial on interfacing with Python. See Two ways communication between Python3 and Arduino

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.