How can I correct this error? TypeError: 'NoneType' object is not subscriptable

Asked 1 years ago, Updated 1 years ago, 293 views

I am currently using the gps module and would like to extract the latitude and longitude data. However, when you run the program, it appears OK at first, but at some point it stops and displays this error.

This is my program.

stream=Serial ('com 5', 9600)
t1 = time.time()

while True:
    ubr=UBXReader (stream)
    (raw_data, parsed_data) = ubr.read()

    # extract latitude and longitude data from parsed data
    if str(raw_data[0:6]) == "b'$GNRMC'":
        
        lat = parsed_data.lat
        lon = parsed_data.lon
        
        #declare latitude and longitude into string
        gps1 = str(lat)
        gps2 = str(lon)
    
    #measure finish time
    t2 = time.time()        
    elapsed_time=t2-t1
    print(elapped_time, gps1, gps2)
    
    #measure the time data every 100ms
    time.sleep(0.1)

python python3 gps

2022-09-30 22:01

1 Answers

Welcome to Stack Overflow!
As you may know on which line the error occurred,
As far as the code is concerned, the list cannot be retrieved successfully and
You may have encountered an error while slicing the list.

The main reason is that ubr.read() runs more frequently, and the serial port does not have enough data, so it is likely that the list has not been retrieved.If you add an if statement to confirm that the list has been successfully retrieved immediately after raw_data was retrieved, and if there is anything wrong, skip the subsequent process and sleep.

stream=Serial ('com 5', 9600)
t1 = time.time()

while True:
    ubr=UBXReader (stream)
    # Raw_data could not be retrieved successfully here
    (raw_data, parsed_data) = ubr.read()

    # extract latitude and longitude data from parsed data
    # There is probably an error here.
    if str(raw_data[0:6]) == "b'$GNRMC'":
        
        lat = parsed_data.lat
        lon = parsed_data.lon
        
        #declare latitude and longitude into string
        gps1 = str(lat)
        gps2 = str(lon)
    
    #measure finish time
    t2 = time.time()        
    elapsed_time=t2-t1
    print(elapped_time, gps1, gps2)
    
    #measure the time data every 100ms
    time.sleep(0.1)


2022-09-30 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.