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
How to read data from USB port, pyb.USB_VCP ?
Re: How to read data from USB port, pyb.USB_VCP ?
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).
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).
Re: How to read data from USB port, pyb.USB_VCP ?
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.
Other options are to buy a pyboard or to go back to C++ on the Arduino IDE. I’ll probably do the latter.
Re: How to read data from USB port, pyb.USB_VCP ?
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
http://docs.micropython.org/en/latest/l ... e.UART.any
Re: How to read data from USB port, pyb.USB_VCP ?
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 pmThere 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.
uart.read() is non-blocking. In order to use it with UART 0, you have to detach REPL from the UART.Code: Select all
import uselect list = uselect.select([sys.stdin], [], [], 0.01) if list[0]: byte = sys.stdin.read(1) else: byte = None