Search code examples
python-3.xusbpyserialraspberry-pi4raspberry-pi-pico

Raspberry Pi sending/receiving data over usb


I have a very simple configuration of a Raspberry Pi Pico H (2021) connected to a Raspberry Pi 4 via USB both are on the latest firmware versions. My end goal is to send temp/humidity data over to the Raspberry Pi 4. I am able to read temp/humidity data from my sensor with no issue, but when I try to send data over USB I run into a lot of problems:

Here is a sample of the code I have on the Pico (micropython 1.22.2)

import time

while True:
    print('hello')
    time.sleep(1)

Here is what I have running on my raspberry pi 4 (python 3.11):

import time
import serial

ser = serial.Serial('/dev/ttyACM0', 19600, timeout=30)
ser.flushInput()

while True:
    line = ser.readline()
    if line:
        print(line.decode('utf-8'), end='')

When I run my code on the pico, it prints "hello" with no issues. When I run my code on my Pi 4, it will either fail immediately and say the "device reports readiness to read but returned no data", or it will return fragments of the word "hello" with no discernable pattern in responses and then fail with device reports readiness to read but returned no data. For reference, I did check to make sure that the USB device was registered under /dev/ttyACM* and it was. It never has any connection issues, and only has problems when I run the code on the raspberry pi 4 and the pico at the same time

All I want to do is send temp/humidity data over the USB, which is why I made this simple test case. My question here is what am I missing? I reimaged my Pi and flashed the Pico with latest OS and firmware, no other processes should be running that would interfere with serial communication. The only device attached via usb is the Pi Pico, and I tried over a few different USB ports and cables and the same issue occurs, I even tried with a different Pi Pico and had the same issue so I don't think it's the hardware. The only thing I haven't tried is a new Pi 4 but I only have one. It was also new out of the box.

Any help here is appreciated. I would like to believe that I'm not stressing out the hardware but I'm not sure. If there's a better method to approach this let me know.


Solution

  • So I updated the baud rate back to 115200 and it didn't fix the issue. When I stopped using Thonny and used minicom instead, the code ran with zero issues. So that's all to say that Thonny was preventing data to be sent over serial back to the primary Raspberry Pi 4.