The received data is not complete



  • When programming with uart.readline(), there is a problem that the received data is not complete.
    How do I solve the problem?



  • @Andy-Smith that line reads a GPS response which are coming in at 10 lines or more each second (depending on the gps) and the function falls out the end, only printing the ones that I am interested in decoding.



  • @jcaron Actually it doesn't, the data I receive in this one would be longer, something like ST<{"cmd_code": "set_value", "type": "label", "widget": "label2", "value":1.23, "format":"%.2f"}>ET.



  • @neil-jepsen Thanks for your reply, I would like to know how this uart.readline() determines the end of reception.



  • Hi Andy,
    coincidently I have been playing with reading a GPS and trying to get to the bottom of how the various uart instructions work, which is not entirely clear to me from the docs.
    I have the following code running on a fipy'

    
    import time
    import utime
    import socket
    import machine
    dir(machine)
    import gc
    from time import sleep
    from machine import UART
    from utime import sleep_ms, ticks_ms, ticks_diff,ticks_add
    from machine import Pin
    from machine import RTC
    import pycom
    import math
    
    
    pycom.wifi_on_boot(False)
    pycom.smart_config_on_boot(False)
    
    
    valid = ""
    latitude  = 0
    longitude = 0
    GMT_time= 0
    satellites_used=0
    
    gps=UART(1,baudrate=9600,pins =('P8','P13'),rx_buffer_size = 4096)
    print('uart done')
    
    
    
    def read_gps():
        
      try:
        t = utime.ticks_us()
        global gps,latitude,longitude,GMT_time,satellites_used
        if gps.any():
            print(gps.any())
            index = 0
            sleep(0.1)
            print('chars after 0.1 =',gps.any())
            nmea_str= str(gps.readline())   # reads from buffer not gps. looks like b'$  nema string  ' and is a byte array 
                  
            print('gps said',nmea_str,'len=',len(nmea_str))
           
            if nmea_str.find('$GNGGA') >-1:     
                         
                 index = nmea_str.strip().split(',')           # strip spaces, split on ','  split takes 17ms 
                 valid = index[6]
                 gps_time = index[1]                          #time is battery backed up
                                     
                 
                 if valid =='1':  
                        
                       latitude = index[2]
                       longitude=index[4]
                       GMT_time = index[1]
                       satellites_used = index[7]
    #                    
                 else:
                     print('invalid response',nmea_str,'validity = ',valid)
    #                              
                    
    #            
            if nmea_str.find('$GPGSV') >-1:
                      index = nmea_str.strip().split(',')
                      print()
                      print()
                      print ('last good lat',latitude,'at time',GMT_time, 'from',satellites_used,'satellites')
                      print ('last good long',longitude)
                      print('satellites in view=',index[3], 'at time',GMT_time)
                      print()
                      print()
                      sleep(5)
                      print('chars=',gps.any())
                      gps.read()  #read all - flush buffer
                      print('chars after flush =',gps.any())
                     
      except Exception as e:   
                print("## failed to read GPS",e)
                
    
     
    while True:
       r = read_gps()
      
    

    The code runs OK. Things i have learned are:
    uart.any() returns the number of bytes in the uart buffer.
    If the buffer is too small and overflows, data is lost but the processor doesn't crash and
    as far as I know other code/var/ are not overwritten
    uart.readline() reads a line of code if there is a complete line in the buffer at that point in time.
    Data is not removed from the buffer with uart.readline(), unlike uart.read()
    uart.read() will read verything in the uart buffer and clear the buffer.
    In the above code, whilst the first line of gps nmea data is being read and processed, GPS data is still coming in and being stored in the buffer.



  • @Andy-Smith have you set timeout_chats in the UART constructor?


Log in to reply
 

Pycom on Twitter