ESP32 Devkit V4 reading and writing data over USB to serial using the UART class #11096
-
I am trying to use the UART class on an ESP32 devkit V4 using the built in USB to serial CH340 chip and am having some trouble reading and writing data over serial. This is the code which is running on the ESP32:
I have attempted to write data to the ESP32 over serial using the Arduino IDE's serial monitor as well as this Python class:
I used the print statements in the Python class to confirm both are using the same serial settings. The issues I am running into are:
Any advice is greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Your device uses UART0 for REPL and stdio. As soon as you use UART0 for something else, REPL is blocked. And if you put your code into main.py, you lock yourself out. So you have to use either a different UART and a separate USB/UART bridge. Or you use sys.stdin.read() for reading and sys.stdout.write() for writing. Or sys.stdin.buffer.read()/sys.stdout.buffer.write() for raw I/O. There are more methods than read() and write(). The equivalent for uart.any() would be using select(). |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delayed response, but thank you so much for your help robert-hh. I have written a class which can read in single characters over serial, put those characters into a message, and then handle said message. This code could be easily modified to make the messages larger than a single charter by using the command message += readSerial() in some form of a loop. Of course, logic would then have to be added make sure the message is then handled at the desired size, or when it matches a command, or whatever behavior is desired, but that is a larger project that goes beyond what the first post was about. This code also has the advantage of not needing to disconnect from the REPL in order to read data from the serial port, which may be desirable to some.
|
Beta Was this translation helpful? Give feedback.
Sorry for the delayed response, but thank you so much for your help robert-hh.
I have written a class which can read in single characters over serial, put those characters into a message, and then handle said message.
This code could be easily modified to make the messages larger than a single charter by using the command message += readSerial() in some form of a loop. Of course, logic would then have to be added make sure the message is then handled at the desired size, or when it matches a command, or whatever behavior is desired, but that is a larger project that goes beyond what the first post was about.
This code also has the advantage of not needing to disconnect from the REPL in or…