Import Pyserial Module

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
moneyman
Posts: 5
Joined: Thu Jun 06, 2019 12:17 am

Import Pyserial Module

Post by moneyman » Thu Jun 06, 2019 8:39 pm

Hi guys, I'm wrote code in main.py of the Pyboard using the pyserial module. Unfortunately, when I run the program, the pyboard does not know what the serial module is. How can I import this module through REPL given that the Pyboard is a bare metal computer and thus does not have access to the terminal?

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

Re: Import Pyserial Module

Post by jimmo » Fri Jun 07, 2019 5:52 am

Hi,

pyserial is a library for "big" Python (CPython).

On an embedded device, you'll likely be achieving the same result using a UART directly. https://docs.micropython.org/en/latest/ ... .UART.html and https://docs.micropython.org/en/latest/ ... serial-bus

The UART IDs (e.g. the `1` in the example in the quick reference) are labeled on the pin diagram at https://store.micropython.org/product/PYBv1.1H#image4 (e.g. UART 1 is X9 / X10)

moneyman
Posts: 5
Joined: Thu Jun 06, 2019 12:17 am

Re: Import Pyserial Module

Post by moneyman » Sun Jun 09, 2019 3:27 pm

Thanks for the response. Is there any way I can do the same with a serial connection over USB using the pyserial module? I'm trying to connect the Pyboard to the Raspberry Pi.

User avatar
pythoncoder
Posts: 3613
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Import Pyserial Module

Post by pythoncoder » Sun Jun 09, 2019 5:42 pm

You can use pyserial on the Raspberry Pi and use the Pyboard UART as per the doc references @jimmo gave you.
Peter Hinch

User avatar
Roberthh
Posts: 1648
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Import Pyserial Module

Post by Roberthh » Sun Jun 09, 2019 6:56 pm

If you want to use the USB on the PyBoard for that communication, you can use the pyb.USB_VCP class for receiving and sending. You can use pyb.USB_CVP.any() to tell, if characters are buffered for reading.

rhubarbdog
Posts: 166
Joined: Tue Nov 07, 2017 11:45 pm

Re: Import Pyserial Module

Post by rhubarbdog » Mon Jun 10, 2019 8:35 am

I've connected a microbit to raspberry pi with direct uart to uart connection

moneyman
Posts: 5
Joined: Thu Jun 06, 2019 12:17 am

Re: Import Pyserial Module

Post by moneyman » Mon Jun 10, 2019 9:10 pm

Thank you all for the responses. I've found using the pyb.UCB_VCP method to be easiest because it still allows me to use the USB connection on both ends. However, a follow up question I have is with the write function of this class. I am attempting to do the following:

USB_VCP.write(("hello\r\n").encode())

However, the error I receive is "argument has wrong type". Does anyone know why?

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

Re: Import Pyserial Module

Post by dhylands » Mon Jun 10, 2019 10:23 pm

You need to instantiate an object. Your code should look like:

Code: Select all

usb = pyb.USB_VCP()
usb.write(b'helllo\r\n')
. Note that b'hello\r\n' 'hello\r\n'.encode() or ('hello\r\n').encode() all produce the same thing (namely b'hello\r\n')

moneyman
Posts: 5
Joined: Thu Jun 06, 2019 12:17 am

Re: Import Pyserial Module

Post by moneyman » Tue Jun 11, 2019 9:23 pm

dhylands wrote:
Mon Jun 10, 2019 10:23 pm
You need to instantiate an object. Your code should look like:

Code: Select all

usb = pyb.USB_VCP()
usb.write(b'helllo\r\n')
. Note that b'hello\r\n' 'hello\r\n'.encode() or ('hello\r\n').encode() all produce the same thing (namely b'hello\r\n')
This worked perfectly. Thank you all so much for the help!

moneyman
Posts: 5
Joined: Thu Jun 06, 2019 12:17 am

Re: Import Pyserial Module

Post by moneyman » Sat Jun 15, 2019 7:48 pm

Hi all,

Another question I have is if there is any way to set an interrupt for every time data reaches USB_VCP. I have an infinite loop running elsewhere in the code but I want to be able to keep reading from this port in case I want to keep sending new data from the RPi. Any suggestions and examples would be great. Thanks!

Post Reply