NMEA GPS

Aus NeoWiki

Wechseln zu: Navigation, Suche

Infos

Skripte

Python Skript zum direkten NMEA decodieren/loggen von einem seriellen Anschluss

Kann direkt auf der Konsole als Live-Viewer verwendet werden.

#!/usr/bin/env python
import serial, time, sys, re

# Open the port. Specify baudrate, handshake, and timeout. We plan
# to use the pyserial readline method which will 'hang' if a timeout is
# not specified.
#
ser = serial.Serial('/dev/ttyUSB0',baudrate=4800,xonxoff=0, timeout=1)

#ser.setRTS(0)
#ser.setDTR(0)

# Wait approximately 500 milliseconds.
#
#time.sleep(0.5)

# Dump everything from the ARMmite to stdout.
# Terminate when the program finishes.
#

interval = 10;
csvfile = "gpslog.csv";

# do not edit below

lastCSV = 0;

timestamp = "";
lat = "";
lng = "";
sats = "";
height1 = "";
height2 = "";
pdop= "";
hdop= "";
vdop= "";
speed= "";
course= "";
declination= "";
quality= "";
fix= "";
sat = [0,0,0,0,0,0,0,0,0,0,0,0];
satinfo = ["","","","","","","","","","","",""];
satinfotemp = satinfo;

if csvfile<>"":
    out = open(csvfile,"a");
    out.write("timestamp,lat,lng,course,speed,height,sattelites,pdop,fix\n");

while(True):
    if ser.inWaiting() > 0:
        # Either wait for and return with a newline, or timeout.
        #
        s = ser.readline()

        # clean the line and split to array
        res = re.match("\$(\S+)\*(\S+)",s);
        if res:
            s = res.group(1);
            print s;
            s += ",,,,,,,,,,,,,,";
            a = s.split(',');
            if a[0] == "GPGSA":
                fix = a[2];
                for x in range(12):
                    sat[x] = a[3+x];
                pdop = a[15];
                hdop = a[16];
                vdop = a[17];

            elif a[0] == "GPGSV":
                if a[2] == "1":
                    # reset the satinfo array
                    satinfotemp = ["","","","","","","","","","","",""];

                for x in range(4):
                    i = ((int(a[2])-1)*4)+x;
                    ai = 4+(x*4);
                    satinfotemp[i] = a[ai]+","+a[ai+1]+","+a[ai+2]+","+a[ai+3]+",";

                if a[2] == a[1]:
                    # set the satinfo array
                    satinfo = satinfotemp;


            elif a[0] == "GPRMC":
                timestamp = ''+a[9]+a[1];
                receiverwarning = a[2];
                lat = a[3];
                lng = a[5];
                speed = a[7];
                course = a[8];
                declination = a[10];
                mode = a[12];

            elif a[0] == "GPGGA":
                #timestamp = a[1];
                lat = a[2];
                lng = a[4];
                quality = a[6];
                sats = a[7];
                hdop = a[8];
                height1 = a[9];
                height2 = a[11];
        else:
            print "UNKNOWN: "+s;


        # display the data in clear text
        print "timestamp    : "+ timestamp;
        print "lat, lng     : "+ lat+ " / "+ lng;
        print "sattellites  : "+ sats;
        print "height       : "+ height1+ " / "+ height2;
        print "precision h/v: "+ pdop+ " "+ hdop+ "/"+vdop;
        print "quality      : "+ quality;
        print "speed, course: "+ speed+ " " + course;
        print "declination  : "+ declination;
        print "fix          : "+ fix+"D";
        print "";
        print "Sattelites in View:";
        for x in range(12):
            lock = " ";
            s = satinfo[x]+",,,,,";
            a = s.split(',');
            z = a[0];
            for y in range(12):
                if sat[y] == z:
                    lock = "*";
                    break;

            print "     "+lock+" "+z+"  elev="+a[1]+"°  azim="+a[2]+"°  SNR="+a[3]+" dB";

        # check timestamp
        t = time.time();
        if (lastCSV < (t-interval)) & (timestamp<>"") & (fix<>""):
            lastCSV = t;
            if csvfile<>"":
                out.write(timestamp+","+lat+","+lng+","+course+","+speed+","+height1+","+sats+","+pdop+","+fix+"\n");

    else:
        time.sleep(0.1);


# Close the serial port.
#
if csvfile<>"":
    out.close()
ser.close()