Raspberry Pi pico will not accept complete data if uart.readline() is used to read serial data

Raspberry Pi pico will not accept complete data if uart.readline() is used to read serial data.
How to solve this problem?

1 Like

Hey Lance,

How did you have everything hooked up and were you using the micropython implementation to program the Pico? PS: if you could send through your code that would be amazing!

I can imagine that function is looking for a delimiter sign such as the new line character so throwing that in once your line of data is ‘over’ that will give the function the data it just read.

2 Likes

Hey Lance,

To add to Liams response you can check out the documentation on the module here: class UART – duplex serial communication bus — MicroPython 1.16 documentation
There are also some examples in the Python SDK on the Raspberry Pi Org website here: https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf

Let us know how you go!

2 Likes

Yes, I use micropython.

from machine import UART,Pin
import time
import _thread

#Defining the serial port
uart1 = UART(1, baudrate = 115200, tx = Pin(4), rx = Pin(5))

#Define five-wire, four-phase motor pins
motor1a = Pin(9,Pin.OUT)
motor1b = Pin(10,Pin.OUT)
motor1c = Pin(11,Pin.OUT)
motor1d = Pin(12,Pin.OUT)

def forward(delay, maxspeed):      #Stepper motor forward function, define the stepper motor 

for double four beat drive mode

    for i in range(0, maxspeed):
        motor1a.high()
        motor1b.high()
        motor1c.low()
        motor1d.low()
        time.sleep(delay)
        motor1a.low()
        motor1b.high()
        motor1c.high()
        motor1d.low()
        time.sleep(delay)
        motor1a.low()
        motor1b.low()
        motor1c.high()
        motor1d.high()
        time.sleep(delay)
        motor1a.high()
        motor1b.low()
        motor1c.low()
        motor1d.high()
        time.sleep(delay)

def backward(delay, maxspeed):     #Stepper motor reversing function

    for i in range(0, maxspeed):
        motor1a.low()
        motor1b.low()
        motor1c.high()
        motor1d.high()
        time.sleep(delay)
        motor1a.low()
        motor1b.high()
        motor1c.high()
        motor1d.low()
        time.sleep(delay)
        motor1a.high()
        motor1b.high()
        motor1c.low()
        motor1d.low()
        time.sleep(delay)
        motor1a.high()
        motor1b.low()
        motor1c.low()
        motor1d.high()
        time.sleep(delay)

def stop():    #Motor stop function
    motor1a.low()
    motor1b.low()
    motor1c.low()
    motor1d.low()
    
def dianji():
    forward(0.005, 512)
    stop()
    time.sleep(3)
    backward(0.005, 512)
    stop()
    time.sleep(3)

def zrun():
    forward(0.005, 512)
    
def frun():
    backward(0.005, 512)
    
def runing(list2):
    while switch == '\x01':
        motor1a.low()
        motor1b.low()
        motor1c.high()
        motor1d.high()
        time.sleep(0.005)
        motor1a.low()
        motor1b.high()
        motor1c.high()
        motor1d.low()
        time.sleep(0.005)
        motor1a.high()
        motor1b.high()
        motor1c.low()
        motor1d.low()
        time.sleep(0.005)
        motor1a.high()
        motor1b.low()
        motor1c.low()
        motor1d.high()
        time.sleep(0.005)
        switch = list2[13]

#counti = 0
#list2 = []
motor_off = ['S', 'T', '<', '\x10', '\x10', '\x00', '\x07', 's', 'w', 'i', 't', 'c', 'h', 

'\x00', '>', 'E', 'T']#Serial screen command, can be modified by yourself
motor_on = ['S', 'T', '<', '\x10', '\x10', '\x00', '\x07', 's', 'w', 'i', 't', 'c', 'h', 

'\x01', '>', 'E', 'T']
time.sleep(1)


def threadone():
    counti = 0
    list2 = []
    try:
        rxdata = uart1.read(40)
        list1 = list(rxdata)
        print(list1)
    except BaseException as e:
        #print('No data received on serial port')
        time.sleep(0.005)
    else:
        #list1 = list(jieshou)
        #list2 = []
        for item in list1:
            list2.append(chr(item))
            counti += 1
            if item == 62:                       #The serial port returns the ASCII decimal 

value of the character with the end flag '>' in the command data

                for item2 in range(2):           #When reading '>' and then read two characters 

back, so that the full instruction is displayed, but there are actually two check digits of 

characters after it
                    counts = list1[counti]
                    list2.append(chr(counts))
                    counti += 1
                #break
            print(list2)
            return list1[13]
            break
        uart1.sendbreak()
            
while True:
    #time.sleep(1)
    sss = threadone()
    if sss == 1:
        while True:
            if threadone() == 0:
                motor1a.low()
                motor1b.low()
                motor1c.low()
                motor1d.low()
                break
                
            else:
                motor1a.low()
                motor1b.low()
                motor1c.high()
                motor1d.high()
                time.sleep(0.005)
                motor1a.low()
                motor1b.high()
                motor1c.high()
                motor1d.low()
                time.sleep(0.005)
                motor1a.high()
                motor1b.high()
                motor1c.low()
                motor1d.low()
                time.sleep(0.005)
                motor1a.high()
                motor1b.low()
                motor1c.low()
                motor1d.high()
                time.sleep(0.005)
    

1 Like

Thanks for the reply, I’ll keep looking for a solution!

1 Like

Hey @Lance167598, are you using the hardware uart (eg. GPIO 0 and 1) or are you trying to scan for user input through the USB uart?

If it’s the latter, I think you’ll need to use stdin.readline() instead

3 Likes

Hey @Lance167598

Reading through the script, it appears that on line 6, uart1 has been defined to use pins 4 and 5, although as Michael said, were you trying to scan for user input via the USB or the pins 0 and 1 for hardware UART?

If I understand the docs that the UART library for MicroPython 1.16 has on those methods, it appears that stating pins 4 and 5 as TX and RX references these two pins as seen on the image I’ve added below the link. It may be worth double-checking the pinout in the project is correct to ensure that they’re the right pins, and maybe try using the standard pins for hardware UART and adjusting the MicroPython appropriately to ensure that nothing else is going on with the Pico unexpectedly. Please let us know how you go with it! The Pico is still quite a young board, so having info available for the community on it for common issues is always helpful and appreciated :grinning_face_with_smiling_eyes:

https://docs.micropython.org/en/latest/library/machine.UART.html#methods

2 Likes

I use the hardware uart, I checked the uart.readline() function, the underlying logic of the uart.readline() function is to determine the carriage return character, but the serial screen I am using has the command ending ET.
So the uart.readline() function doesn’t recognize the end of my instruction so that it can’t read the whole thing.