Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob question: Noob question: how to send and receive bytes over USB?
#1
I'm trying to send bytes to and receive bytes from an m5stack using PySerial. My goal is to create a command line script on my laptop that can send messages (bytes) over USB to the m5stack, which will do some work and send a message (bytes) back. Much like this (on Arduino).

On the m5stack side I've tried

Code:
from machine import UART
import utime

uart = UART(1, tx=22, rx=21)

def main():
    while True:
        msg = uart.readline()
        print(msg)
        utime.sleep(1)

if __name__ == '__main__':
    main()

On the PySerial side I tried

Code:
ser = serial.Serial('/dev/ttyUSB0')
ser.write(b"hello, world!")

Interestingly, when I connect to MicroPython REPL using screen, I do see the "hello, world!" prints to the input line in my MicroPython REPL as if I typed it but didn't hit enter!

The m5stack just prints empty strings. Any hints on how to read the bytes PySerial is sending?

I've seen people recommend pyb.USB_VCP but it seems that's not available on esp32 ...

I also tried machine.stdin_get(bytes, timeout) but that only returns "bytes"-many \x00 bytes whenever I try to send it a bytestring from PySerial.

Would appreciate any direction I can get. Thanks!

(Also posted on MicroPython forum)

Do I need something like this?

Do I need to install the Silicon Labs CP210x driver?
Reply
#2
First of all are you trying to recieve via USB on a M5stack or send from the M5stack via USB??

Pyboard uses a STM32 processor that has a USB peripheral but M5stack uses a ESP32 that doesn't have a USB peripheral. Most ESP32 dev board have a UART to USB converter (CP210x being the most common) that will convert anything coming or going out on UART to USB. In order for your PC to be able to read these converters they will need to have to correct driver installed.
Reply
#3
(01-05-2019, 05:41 AM)OutoftheBOTS Wrote: First of all are you trying to recieve via USB on a M5stack or send from the M5stack via USB??

Pyboard uses a STM32 processor that has a USB peripheral but M5stack uses a ESP32 that doesn't have a USB peripheral. Most ESP32 dev board have a UART to USB converter (CP210x being the most common) that will convert anything coming or going out on UART to USB. In order for your PC   to be able to read these converters they will need to have to correct driver installed.

I'm trying to do both.

I believe that I have the driver correctly installed because the m5stack shows up as /dev/ttyUSB0 on my Linux desktop and I understand that it wouldn't if the driver wasn't working? Though I never installed it directly ...

I got a very basic prototype working that uses machine.stdin_get and machine.stdout_put.

micropython main.py:


Code:
import utime
import machine
import m5stack

def demo():
    while True:
        msg = machine.stdin_get(3, 1000)
        if msg:
            tft.clearwin()
            tft.text(tft.CENTER, 35, msg)
            machine.stdout_put(b"xyz")
            utime.sleep(3)

if __name__ == '__main__':
    demo()

PySerial code running on desktop:

Code:
import sys
import serial

def demo(port):
   with serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS,
           parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE) as ser:
       ser.write(b"abc")
       res = ser.read(3)
       print("response: ", res)

if __name__ == "__main__":
   port = sys.argv[1]
   demo(port)

It's a little weird how machine.stdin_get returns a string -- would prefer bytes but oh well.

Is there a better way to do this, or am I on the right track?

Thanks again for you help Smile
Reply
#4
OK if your using Linux then the CP210x driver is already installed as part of the OS

You should be able to write/read bytes through UART but make sure your using the correct UART and correct speed
Reply
#5
(01-05-2019, 09:35 AM)OutoftheBOTS Wrote: OK if your using Linux then the CP210x driver is already installed as part of the OS

You should be able to write/read bytes through UART but make sure your using the correct UART and correct speed

Thanks. Where can I look those up for the m5stack? I think I also need "tx" and "rx" values, too. Loboris's firmware throws an error if they're not included in the UART constructor.
Reply
#6
(01-06-2019, 08:22 PM)justinmoon Wrote:
(01-05-2019, 09:35 AM)OutoftheBOTS Wrote: OK if your using Linux then the CP210x driver is already installed as part of the OS

You should be able to write/read bytes through UART but make sure your using the correct UART and correct speed

Thanks. Where can I look those up for the m5stack? I think I also need "tx" and "rx" values, too. Loboris's firmware throws an error if they're not included in the UART constructor.

I do believe that GPIO1 is TXD0 and GPIO3 is RXD0 but can't conform that for M5stack as haven't used it.
Reply
#7
(01-06-2019, 08:47 PM)OutoftheBOTS Wrote:
(01-06-2019, 08:22 PM)justinmoon Wrote:
(01-05-2019, 09:35 AM)OutoftheBOTS Wrote: OK if your using Linux then the CP210x driver is already installed as part of the OS

You should be able to write/read bytes through UART but make sure your using the correct UART and correct speed

Thanks. Where can I look those up for the m5stack? I think I also need "tx" and "rx" values, too. Loboris's firmware throws an error if they're not included in the UART constructor.

I do believe that GPIO1 is TXD0 and GPIO3 is RXD0 but can't conform that for M5stack as haven't used it.

Does this mean I need to physically connect pins to my laptop in some manner for this to work? Can I access these pins via USB at all?

I'm completely stumped. I don't know what to do with this information. Complete hardware noob.

I tried this and many other attempts with no luck:

Code:
uart = machine.UART(1, pins=('GPIO1', 'GPIO3'))

Here is some Arduino code that just counts numbers over the serial port. How can I do this in MicroPython?

Code:
// Arduino Code

int count = 0;

void setup() {  
  Serial.begin(115200);
}
 
void loop() {
  while(Serial.available()){
    Serial.write(count);
    count = count + 1;
    delay(1000);
  }
}

Code:
// PySerial code to run on host machine

import serial

with serial.Serial(baudrate=115200, port='/dev/ttyUSB0') as ser:
    while True:
        res = ser.read()
        print("received: ", res)

It seems like loboris' port turns off the `UART(0)` which is what Arduino's `Serial` is using??? I can't figure out how to get this code coding with `Serial1` or `Serial2` on Arduino -- which seem to be the equivalents of `machine.UART(1)` and `machine.UART(2)` on MicroPython.

Thanks for helping! Going to have to go back to Arduino if I can't figure this out  Undecided
Reply
#8
OPk ESP32 doesn't have a USB serial peripheral so most makers of ESP32 dev boards have a UART to USB converter builtin on the dev board. So when you send data to the UART that is connected to the UART to USB converter it will be converted then sent to the USB.
Code:
Serial(baudrate=115200, port='/dev/ttyUSB0')

This will only work on a MCU that has a USB peripheral like the STM32 used on PyBoard

I have just read close the Lobo wiki and it says the UART0 which the UART to USB converter is attached to is unavailable quote"UART0 is used for serial REPL and, at the moment, is not available in this class."

If you just want to print on a serial console you can just use the print() function and it will be sent to USB. Just open a serail console on your computer and you will see it printed out
Reply
#9
(01-19-2019, 06:24 AM)OutoftheBOTS Wrote: OPk ESP32 doesn't have a USB serial peripheral so most makers of ESP32 dev boards have a UART to USB converter builtin on the dev board. So when you send data to the UART that is connected to the UART to USB converter it will be converted then sent to the USB.
Code:
Serial(baudrate=115200, port='/dev/ttyUSB0')

This will only work on a MCU that has a USB peripheral like the STM32 used on PyBoard

I have just read close the Lobo wiki and it says the UART0 which the UART to USB converter is attached to is unavailable quote"UART0 is used for serial REPL and, at the moment, is not available in this class."

If you just want to print on a serial console you can just use the print() function and it will be sent to USB. Just open a serail console on your computer and you will see it printed out


Understood. Disabling UART0 is unfortunate. Maybe I should move away from Loboris' fork and just use vanilla MicroPython firmware ...

This are basically the Loboris' machine.stdin_get/machine.stdin_put which I mentioned earlier. These make debugging impossible at times because you can no longer drop print statements into your code because they are interpreted as serial messages ... feels very hacky.
Reply
#10
(01-19-2019, 07:20 AM)justinmoon Wrote: Understood. Disabling UART0 is unfortunate. Maybe I should move away from Loboris' fork and just use vanilla MicroPython firmware ...

This are basically the Loboris' machine.stdin_get/machine.stdin_put which I mentioned earlier. These make debugging impossible at times because you can no longer drop print statements into your code because they are interpreted as serial messages ... feels very hacky.

The situation with UART0 in 'vanila' MicroPython is exactly the same.

I've added the possibility to use UART0 the same way as other uarts, it will be available after the next update.
It will be possible to use that feature while running the script (for example in main.py), while it is enabled, the REPL does not interpret the commands over uart.

If you need some kind of debuging over uart, you can do it over another uart connected to the separate usb2uart adapter.
You can drop print statements in your code and catch them  on pc.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)