Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    The received data is not complete

    Scheduled Pinned Locked Moved MicroPython
    6 Posts 3 Posters 62 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      Andy Smith
      last edited by

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

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @Andy Smith
        last edited by

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

        A 1 Reply Last reply Reply Quote 1
        • N Offline
          neil jepsen
          last edited by livius

          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.

          A 1 Reply Last reply Reply Quote 0
          • A Offline
            Andy Smith @neil jepsen
            last edited by

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

            N 1 Reply Last reply Reply Quote 0
            • A Offline
              Andy Smith @jcaron
              last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • N Offline
                neil jepsen @Andy Smith
                last edited by

                @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.

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom