micropython - Bidirectional communication over USB between host PC and Raspberry Pi Pico - Stack Overflow
I'm trying to achieve bidirectional communication over my Raspberry Pi Pico's built-in micro-USB port. More specifically, I'm trying to make my computer send a ping to the Pico, and the Pico receives the ping and responds with a pong.
The code
This is the code that I run on my computer:
# This must not run on Pico, but on the host computer!
import serial
import time
import sys
# open a serial connection
s = serial.Serial("/dev/cu.usbmodem1101", 115200) # on my mac, this is how pico shows up
# blink the led
while True:
s.write(b"ping\n")
print("sent 'ping'")
time.sleep(1)
response = s.readline()
print(f"received '{response}'")
time.sleep(1)
And this is the code I put on my Pico (using the Thonny Python IDE):
import time
import sys
while True:
# read a command from the host
v = sys.stdin.readline().strip()
print(f"received '{v}'")
time.sleep(1)
sys.stdout.write(b"pong\n")
print("sent 'pong'")
I first click the green Run button in Thonny to put code on the Pico. Then, I run the first script on my computer with the following command:
$ python data_transfer_host.py
The problem
My Pico successfully receives the ping from the computer. But I don't know how to send the pong back to my computer. With the code I have now, the text pong
is written to Thonny's console, like this:
I also tried using sys.stdout.print(b"pong\n")
instead of sys.stdout.write
but it results in an error: AttributeError: 'TextIOWrapper' object has no attribute 'print'
.
- 苹果WWDC2014:iOS 8新功能都有哪些?
- 微软推Surface平板 成硬件合作伙伴竞争对手
- 抓住“苹果”的尾巴,国产平板的突围之路
- python - Adding quotes to list objects to format as a dictionary pyspark - Stack Overflow
- python - Chromadb: Why do results of collection.query() and collection.get() differ? - Stack Overflow
- windows 10 - Gamemaker Mobile device Inconsistent Drag Speed Across Different Operating Systems (Win7 vs. Win10) - Stack Overflo
- Banning telegram channels from groups as a bot - Stack Overflow
- mip sdk - Can you use the mip sdk to apply a mpip sensitivity label to a .json? - Stack Overflow
- flutter - Error handshake exception: handshake error in client(os error: certificate _verify_failed: unable to get local issuer
- xamarin - How to add static library(.a) in to .Net Maui iOS application - Stack Overflow
- python - Bullets shooting from wrong position after player moved in small "Space Invaders" game - Stack Overfl
- Java Zxing Datamatrix Code Not Scanning Datamatrixes - Stack Overflow
- python - Ipywidgets : How to get the list of comm opened in a jupyter notebook? - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- c# - Why is OntriggerEnter2D not working on my Flappy Bird game?Or if not what is the problem and can you solve it? - Stack Over
- php - Laravel Defer on API requests - Stack Overflow
- Clicking on Android Studio on Mac makes Chrome window disaapear - Stack Overflow