Parsing GPS receiver output via regex in Python

后端 未结 7 1303
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 11:01

I have a friend who is finishing up his masters degree in aerospace engineering. For his final project, he is on a small team tasked with writing a program for tracking weat

相关标签:
7条回答
  • 2020-12-14 11:50

    You should also first check the checksum of the data. It is calculated by XORing the characters between the $ and the * (not including them) and comparing it to the hex value at the end.

    Your pastebin looks like it has some corrupt lines in it. Here is a simple check, it assumes that the line starts with $ and has no CR/LF at the end. To build a more robust parser you need to search for the '$' and work through the string until hitting the '*'.

    def check_nmea0183(s):
        """
        Check a string to see if it is a valid NMEA 0183 sentence
        """
        if s[0] != '$':
            return False
        if s[-3] != '*':
            return False
    
        checksum = 0
        for c in s[1:-3]:
            checksum ^= ord(c)
    
        if int(s[-2:],16) != checksum:
            return False
    
        return True
    
    0 讨论(0)
提交回复
热议问题