Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - Parsing the gps coordinates from SIM868 module

Hi I used this is the code to print GPS data

import serial
import time
ser = serial.Serial("/dev/ttyUSB0",115200)
W_buff = ["AT+CGNSPWR=1
", "AT+CGNSSEQ="RMC"
", "AT+CGNSINF
", "AT+CGNSURC=2
","AT+CGNSTST=1
"]
ser.write(W_buff[0])
ser.flushInput()
data = ""
num = 0
try:
    while True:
        #print ser.inWaiting()
        while ser.inWaiting() > 0:
            data += ser.read(ser.inWaiting())
        if data != "":
            print data
            if  num < 4:    # the string have ok
                print num
                time.sleep(0.5)
                ser.write(W_buff[num+1])
                num =num +1
            if num == 4:
                time.sleep(0.5)
                ser.write(W_buff[4])
            data = ""
except keyboardInterrupt:
    if ser != None:
        ser.close()

[enter image description here][1]

Anyway it gives me the above data, You can see I have highlighted in red, I'm trying to just get only 2 of these then I use the following code where I could extract coordinates, but it gives me the same output as the attached image instead of coordinates

from gps import *
import time, inspect
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
try:
    while True:
        report = gpsd.next()
        if report['class'] == 'TPV':
            GPStime =  str(getattr(report,'time',''))
            lat = str(getattr(report,'lat',0.0))
            lon = str(getattr(report,'lon',0.0))
            speed =  str(getattr(report,'speed','nan'))
            sats = str(len(gpsd.satellites))
            print  GPStime,"",
            print  lat,"",
            print  lon,"",
            print  speed,"",
            print  sats,""
            time.sleep(2)
except (KeyboardInterrupt, SystemExit): 
    print "Done.
Exiting."
    f.close() 

When this didn't work I tried the following code trying to convert them into a python array But 'm getting the error UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 0: invalid start byte

import io
import pynmea2
import serial
#ser.write("AT+CGNSPWR=1")
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2.0)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser), newline=None)
ser.write("AT+CGNSPWR=1")
while 1:
    try:
        line = sio.readline()
        msg = pynmea2.parse(line)
        print(repr(msg))
    except serial.SerialException as e:
        print('Device error: {}'.format(e))
        break
    except pynmea2.ParseError as e:
        print('Parse error: {}'.format(e))
        continue

how can I just extract the coordinates here? [1]: https://i.stack.imgur.com/FDGQO.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...