Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


nmea_validator

python 3 - NMEA Validator

Print only valid NMEA from file.

#!/usr/bin/python3
 
import re
 
 
# Example string
# $GPGGA,054917.000,5100.0000,N,00400.0000,E,1,06,1.23,-24.7,M,47.1,M,,*71
 
# Match only lines starting with $ and ending with *<checksum>
pattern = re.compile("^\$(.*)\*[0-9a-fA-F]{2}")
 
with open("nmea.log") as nmea:
    for line in nmea:
        if pattern.match(line):
            # NMEA without $ and *<checksum>
            result = re.search('\$(.*)\*', line)
            # Only the checksum
            nmeasum = re.search('\*(.*)', line)
 
            # Calculate XOR sum, print only valid lines
            sum = 0
            for char in result.group(1):
                sum = sum ^ ord(char)
 
            if '{:02X}'.format(sum) == nmeasum.group(1):
                print(line, end='')
nmea_validator.txt · Last modified: 2021/10/09 15:14 by 127.0.0.1