Page 1 of 1

How to Send/Receive via Pico USB

Posted: Thu Oct 21, 2021 9:54 am
by rikun99
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!

Re: How to Send/Receive via Pico USB

Posted: Fri Oct 22, 2021 11:50 am
by rikun99
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?

Re: How to Send/Receive via Pico USB

Posted: Fri Oct 22, 2021 1:00 pm
by NameOfTheRose
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.