Import Pyserial Module
Import Pyserial Module
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
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)
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
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Import Pyserial Module
You can use pyserial on the Raspberry Pi and use the Pyboard UART as per the doc references @jimmo gave you.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Import Pyserial Module
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.
-
- Posts: 168
- Joined: Tue Nov 07, 2017 11:45 pm
Re: Import Pyserial Module
I've connected a microbit to raspberry pi with direct uart to uart connection
Re: Import Pyserial Module
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?
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
You need to instantiate an object. Your code should look like:. 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')
Code: Select all
usb = pyb.USB_VCP()
usb.write(b'helllo\r\n')
Re: Import Pyserial Module
This worked perfectly. Thank you all so much for the help!dhylands wrote: ↑Mon Jun 10, 2019 10:23 pmYou need to instantiate an object. Your code should look like:. 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')Code: Select all
usb = pyb.USB_VCP() usb.write(b'helllo\r\n')
Re: Import Pyserial Module
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!
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!