How to Send/Receive via Pico USB

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rikun99
 
Posts: 3
Joined: Mon Oct 11, 2021 10:57 am

How to Send/Receive via Pico USB

Post by rikun99 »

Hi all, i'm a beginner just joined now for support. i have latest circuitpython on the Raspberry Pico.
In a simple project i need the Pico, attached via USB to the PC, to receive a string, calculate an hash, and send back the result 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 with circuitpython?
I tried:

Code: Select all

import sys
string = sys.stdin.buffer.read()
but error is

Code: Select all

AttributeError: 'FileIO' object has no attribute 'buffer'
the hashing part is simple using hashlib.sha256()

but for output to PC? should i just use print() or again something like: sys.stdout.write()

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

Thanks in advance for any suggestions!

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to Send/Receive via Pico USB

Post by tannewt »

You can either use print() and input() in CircuitPython for the shared cdc channel or turn on the second user one. There is a guide for it here: https://learn.adafruit.com/customizing- ... 3096590-12

User avatar
rikun99
 
Posts: 3
Joined: Mon Oct 11, 2021 10:57 am

Re: How to Send/Receive via Pico USB

Post by rikun99 »

Thanks, so i can use both console device and data device to send/receive data.
Let's say i want to use data device, i will enable it with usb_cdc.enable(console=True, data=True) and then?
can you write a simple example to see how the receive and the sending work?
Also will i need to send/receive binary data?
sys.stdin and sys.stdout are for text based streams instead?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to Send/Receive via Pico USB

Post by tannewt »

There is a bit more info here: https://circuitpython.readthedocs.io/en ... index.html

Once you enable it, `usb_cdc.data` is a Serial object that you can call read and write on. There is no encoding/decoding so it'd be binary data. That's best to allow you to encode data any way you like.

User avatar
rikun99
 
Posts: 3
Joined: Mon Oct 11, 2021 10:57 am

Re: How to Send/Receive via Pico USB

Post by rikun99 »

so far i can receive on the pico with usb_cdc.data.read() or usb_cdc.data.readline()
but i got error when i try to send to pc via the serial with:
usb_cdc.data.write(bytes("test"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: wrong number of arguments

why?

also:
>>> usb_cdc.data.write(bytes('test').encode('utf-8'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: wrong number of arguments

any hints?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to Send/Receive via Pico USB

Post by tannewt »

I think bytes may be expecting a second argument (like bytes("test", "utf-8)). You can try this instead: usb_cdc.data.write(b"test")

This will make it a byte string by default and not need any additional encoding.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”