Page 1 of 2

Import Pyserial Module

Posted: Thu Jun 06, 2019 8:39 pm
by moneyman
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?

Re: Import Pyserial Module

Posted: Fri Jun 07, 2019 5:52 am
by jimmo
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)

Re: Import Pyserial Module

Posted: Sun Jun 09, 2019 3:27 pm
by moneyman
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.

Re: Import Pyserial Module

Posted: Sun Jun 09, 2019 5:42 pm
by pythoncoder
You can use pyserial on the Raspberry Pi and use the Pyboard UART as per the doc references @jimmo gave you.

Re: Import Pyserial Module

Posted: Sun Jun 09, 2019 6:56 pm
by Roberthh
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.

Re: Import Pyserial Module

Posted: Mon Jun 10, 2019 8:35 am
by rhubarbdog
I've connected a microbit to raspberry pi with direct uart to uart connection

Re: Import Pyserial Module

Posted: Mon Jun 10, 2019 9:10 pm
by moneyman
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?

Re: Import Pyserial Module

Posted: Mon Jun 10, 2019 10:23 pm
by dhylands
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')

Re: Import Pyserial Module

Posted: Tue Jun 11, 2019 9:23 pm
by moneyman
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!

Re: Import Pyserial Module

Posted: Sat Jun 15, 2019 7:48 pm
by moneyman
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!