How to read data from USB port, pyb.USB_VCP ?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
acemason
Posts: 3
Joined: Tue Feb 11, 2020 10:30 pm

How to read data from USB port, pyb.USB_VCP ?

Post by acemason » Tue Feb 11, 2020 10:46 pm

I have considerable experience passing data over USB between Arduino boards and a Python app using pyserial.

Having acquired a ESP32 dev board I decided to try Micropython and I can't see how to get Micropython to read data from the board's USB port. It appears that if I had a PyBoard I could use pyb.USB_VCP, but I don't have a PyBoard and I can see no way to get the pyb module to put on to my board, nor can I find USB_VCP anywhere else.

I realise I could add hardware and put a second USB port on a different UART, but is there an easier way?

My board is Zerynth DevKit v1
My Micropython is idf3-20191220-v1.12.bin
IDEs that I have successfully used are Thonny and uPyCraft, on Windows 10

User avatar
jimmo
Posts: 1204
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to read data from USB port, pyb.USB_VCP ?

Post by jimmo » Wed Feb 12, 2020 1:40 am

The ESP32 has no hardware USB, but most boards connect UART0 to an external FTDI or equivalent. (If you're used to Arduino, it's identical to the Uno, except for unlike the atmega328, you have multiple hardware serial peripherals).

On the STM32 port (i.e. pyboard) the main chip has hardware USB so that's why they have pyb.USB_VCP.

So your options are to put a second usb/uart adaptor on a different UART, or to use stdin/stdout which will go via uart0 to the board's usb/uart adaptor. Unfortunately on the ESP32, you can't open machine.UART(0) directly. (Have a search on the forum this has been discussed a bit).

acemason
Posts: 3
Joined: Tue Feb 11, 2020 10:30 pm

Re: How to read data from USB port, pyb.USB_VCP ?

Post by acemason » Wed Feb 12, 2020 9:11 pm

Thanks for the clarification. Most of the references I found were a year or two ago and I wondered if a MicroPython solution had been provided. After all Python itself doesn’t have a simple non-blocking input function (how I miss Basic’s input$) so we have to do something like poll the contents of a config file or add a tkinter GUI - neither being embedded options.
Other options are to buy a pyboard or to go back to C++ on the Arduino IDE. I’ll probably do the latter.

User avatar
dhylands
Posts: 3268
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How to read data from USB port, pyb.USB_VCP ?

Post by dhylands » Wed Feb 12, 2020 11:32 pm

You can use the UART.any() method to determine how many characters can be read without blocking:
http://docs.micropython.org/en/latest/l ... e.UART.any

acemason
Posts: 3
Joined: Tue Feb 11, 2020 10:30 pm

Re: How to read data from USB port, pyb.USB_VCP ?

Post by acemason » Fri Feb 14, 2020 12:41 pm

jimmo wrote:
Wed Feb 12, 2020 1:40 am
So your options are ... use stdin/stdout which will go via uart0 to the board's usb/uart adaptor.
I did get some success, testing with keystrokes at the Thonny terminal, based on the code provided below. One day I will test with serial data sent from Python/COM port.
Roberthh wrote:
Fri Nov 29, 2019 1:51 pm
There is still a simple function missing, which tells, whether and how many characters are available at sys.stdin. You can however use uselect.select() with a short timeout, e.g.

Code: Select all

import uselect
list = uselect.select([sys.stdin], [], [], 0.01)
if list[0]:
    byte = sys.stdin.read(1)
else:
    byte = None
uart.read() is non-blocking. In order to use it with UART 0, you have to detach REPL from the UART.

Post Reply