How to Send/Receive via Pico USB

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
rikun99
Posts: 5
Joined: Wed Oct 06, 2021 11:19 am

How to Send/Receive via Pico USB

Post by rikun99 » Thu Oct 21, 2021 9:54 am

Hi all, i'm a beginner playing with MP on the Raspberry Pico.
In a simple project i need the Pico, attached via USB to the PC, to receive a string, modify the string, and send back the modified string to PC.
On the PC i wrote the Python script that sends the string via serial.write to /dev/ttyACM0
On the Pico what is best way to wait for data and send back result?

I'd like to do something similar to the circuitpython way, where you import the usb_cdc module (it allows access to USB CDC serial communications) and then you can enable even 2 serial objects that can be used to send and receive binary data to and from the host, tipically usb_cdc.console (REPL) and usb_cdc.data, and on pc you will see two new /dev/ttyxxx attached.
Then you can use usb_cdc.data.write() to send data, and usb_cdc.data.read() to receive.
So how can this be done with MP?

the script on PC is using serial.readline() to retrieve result.

Thanks in advance for any suggestions!

rikun99
Posts: 5
Joined: Wed Oct 06, 2021 11:19 am

Re: How to Send/Receive via Pico USB

Post by rikun99 » Fri Oct 22, 2021 11:50 am

so far i only found this way:
import sys, select
mystring = sys.stdin.buffer.read()
#code to modify the string
select.select([sys.stdout],[],[],0)[0]
sys.stdout.write(modifiedstring)

will the REPL interfere with this? Is it possible to use other interface like the usb_cdc.data in circuitpython?

NameOfTheRose
Posts: 7
Joined: Tue Jun 29, 2021 7:12 am

Re: How to Send/Receive via Pico USB

Post by NameOfTheRose » Fri Oct 22, 2021 1:00 pm

rikun99 wrote:
Fri Oct 22, 2021 11:50 am
so far i only found this way:
import sys, select
mystring = sys.stdin.buffer.read()
#code to modify the string
select.select([sys.stdout],[],[],0)[0]
sys.stdout.write(modifiedstring)

will the REPL interfere with this? Is it possible to use other interface like the usb_cdc.data in circuitpython?
Yes, this is what I do. It does not seem to interfere with REPL. But why the select.select on stdout? In my experience it never reports anything. And you do not seem to use select's return value any way.
I use select on stdin to test if there are data pending.

Post Reply