Communication (Xiao) ESP32S3 & PC over USB-UART with MicroPython

209 Views Asked by At

I am looking for a working snippet of a communication over USB to a Xiao ESP32S3.

What I have tried (well, too many things to post here) as reference:

On the PC:

import serial
import time

# Configuration
COM = "COM4"
BUADRATE = 115200
while True:
    try:
        with serial.Serial(COM, baudrate=BUADRATE, timeout=1) as ser:
            print(f"Listening...")
            time.sleep(1)  # Give time to the ESP32 to send data

            while ser.in_waiting > 0:
                incoming_data = ser.read_all()
                if incoming_data:
                    print(f"Received: {incoming_data}")
    except Exception as e:
        print(f"Error: {e}")
        time.sleep(1)
        if "KeyboardInterrupt" in str(e):
            break

On the Xiao ESP32S3:

import uos
import machine
import time

UART = 0
BAUDRATE = 115200

time.sleep(1.5)

uos.dupterm(None, UART)  # detach UART from REPL

uart = machine.UART(UART, baudrate=BAUDRATE)
count = 0
while count < 10:
    count += 1
    uart.write(b"Hello PC\n")
    print("Sent")
    time.sleep(.5)

Interestingly enough, I get the “Sent” message but not the expected ‘b"Hello PC\n"’.

I tried many combinations of UART different terminals, Rx and Tx Pins with for-loops and try-excepts. I received on the PC the things printed with "print" ... which I do not really understand how, in my understanding uos.dupterm should prevent it. Yet I have not got a single byte written over the Uart. So I am missing something, probably obvious, to a fresh and wiser pair of eyes.

What am I doing wrong? Thanks!

0

There are 0 best solutions below