Page 1 of 1

How send string from PC to RP2040?

Posted: Wed Jul 21, 2021 10:03 pm
by duckt14
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

Re: How send string from PC to RP2040?

Posted: Wed Jul 21, 2021 11:57 pm
by jimmo
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?

Re: How send string from PC to RP2040?

Posted: Thu Jul 22, 2021 9:28 am
by duckt14
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... :?

Re: How send string from PC to RP2040?

Posted: Thu Jul 22, 2021 10:35 am
by Roberthh
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.

Re: How send string from PC to RP2040?

Posted: Fri Jul 23, 2021 2:52 pm
by duckt14
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()

Re: How send string from PC to RP2040?

Posted: Fri Jul 23, 2021 8:06 pm
by Roberthh
Readline expects a newline character Ctrl-J for termination. You can also use sys.stdin,read(1) to get a single character.

Re: How send string from PC to RP2040?

Posted: Fri Jul 23, 2021 8:52 pm
by duckt14
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