How send string from PC to RP2040?

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
User avatar
duckt14
Posts: 4
Joined: Wed Jul 21, 2021 9:29 am
Location: Turin, Italy

How send string from PC to RP2040?

Post by duckt14 » Wed Jul 21, 2021 10:03 pm

Hi everyone,

I want to send string from pc to Pico to turn-on and turn-off the led built in, I'm using UART but I'm not having success in that.
To send the string I use two different options: a program called VVVV (that I use for my project) and Window's CMD (echo 'hello' > COM n); in both cases programs send messages (I verify using serial port monitor), but Rasperry Pico doesn't receive nothing! Or rather: the sent strings are displayed in Thonny's shell, but it is as if nothing happens.
Instead, Pico is able to send messages to VVVV or pc in general (verified using PuTTY).

This is the code I wrote, nominated 'main.py':

Code: Select all

from machine import Pin, UART
from utime import sleep

led = Pin(25, Pin.OUT)

vvvv = UART(0, 9600)

while True:
    
    data = vvvv.readline()
    num_char = vvvv.any()
    
    string = str(data)
    
    if (num_char > 0) is True:
        led.toggle()
        sleep(2)
    
    sleep(0.5)
What could be the problem?

Very thanks,
Enrico

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How send string from PC to RP2040?

Post by jimmo » Wed Jul 21, 2021 11:57 pm

duckt14 wrote:
Wed Jul 21, 2021 10:03 pm
I'm using UART but I'm not having success in that.
How is the UART connected to your PC? Do you have a separate UART-USB adaptor (e.g. FTDI) also wired up?

User avatar
duckt14
Posts: 4
Joined: Wed Jul 21, 2021 9:29 am
Location: Turin, Italy

Re: How send string from PC to RP2040?

Post by duckt14 » Thu Jul 22, 2021 9:28 am

I connect the Rpi Pico to pc with the simple USB wire: only one USB connect to PC and Micro-USB to Pico!
Could it be a problem to only have one "output" port (usb)?

From Rpi Pico to Pc communication works, from Pc to Rpi Pico it doesn't. In Thonny I see strings message received in the shell, but I don't know if these strings are used by the code... :?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How send string from PC to RP2040?

Post by Roberthh » Thu Jul 22, 2021 10:35 am

USB is not UART. Since you use the normal USB interface, you could sys.stdout.write() for sending (or just print), and sys.stdin.readline() for receiving.

User avatar
duckt14
Posts: 4
Joined: Wed Jul 21, 2021 9:29 am
Location: Turin, Italy

Re: How send string from PC to RP2040?

Post by duckt14 » Fri Jul 23, 2021 2:52 pm

I'm trying use sys.stdin.readline() but Pico doesn't receive nothing from CMD or from the other program that I use. :(

Am I wrong to write the logic of the code?

Code: Select all

from machine import Pin
import sys

led = Pin(25, Pin.OUT)

while True:
    
    data = sys.stdin.readline()
    
    pippo = data.decode("utf-8")

    if pippo.len() > 0:
        led.on()

    else:
        led.off()

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How send string from PC to RP2040?

Post by Roberthh » Fri Jul 23, 2021 8:06 pm

Readline expects a newline character Ctrl-J for termination. You can also use sys.stdin,read(1) to get a single character.

User avatar
duckt14
Posts: 4
Joined: Wed Jul 21, 2021 9:29 am
Location: Turin, Italy

Re: How send string from PC to RP2040?

Post by duckt14 » Fri Jul 23, 2021 8:52 pm

Thank you so much Roberth, now it works with sys.stdin.read(n) and sys.stdin.readline() adding in the string message the Line Feed character (CTRL-J), and this second is what I need for my project!!

Thanks again for your help! :D :D

Enrico

Post Reply