Reworked update() logic and added sentence length limit checking

pull/4/head
Calvin 2014-12-12 19:50:41 -05:00
rodzic a21c1d69d8
commit 3ef80dac72
1 zmienionych plików z 41 dodań i 28 usunięć

Wyświetl plik

@ -29,6 +29,9 @@ class MicropyGPS(object):
"""GPS NMEA Sentence Parser. Creates object that stores all relevant GPS data and statistics. """GPS NMEA Sentence Parser. Creates object that stores all relevant GPS data and statistics.
Parses sentences one character at a time using update(). """ Parses sentences one character at a time using update(). """
# Max Number of Characters a valid sentence can be (based on GGA sentence)
SENTENCE_LIMIT = 76
def __init__(self, local_offset=0): def __init__(self, local_offset=0):
"""Setup GPS Object Status Flags, Internal Data Registers, etc""" """Setup GPS Object Status Flags, Internal Data Registers, etc"""
@ -39,6 +42,7 @@ class MicropyGPS(object):
self.process_crc = False self.process_crc = False
self.gps_segments = [] self.gps_segments = []
self.crc_xor = 0 self.crc_xor = 0
self.char_count = 0
##################### #####################
# Data From Sentences # Data From Sentences
@ -342,6 +346,7 @@ class MicropyGPS(object):
self.crc_xor = 0 self.crc_xor = 0
self.sentence_active = True self.sentence_active = True
self.process_crc = True self.process_crc = True
self.char_count = 0
def update(self, new_char): def update(self, new_char):
"""Process a new input char and updates GPS object if necessary based on special characters ('$', ',', '*') """Process a new input char and updates GPS object if necessary based on special characters ('$', ',', '*')
@ -354,28 +359,30 @@ class MicropyGPS(object):
ascii_char = ord(new_char) ascii_char = ord(new_char)
if 33 <= ascii_char <= 126: if 33 <= ascii_char <= 126:
self.char_count += 1
# Check if a new string is starting ($) # Check if a new string is starting ($)
if new_char == '$': if new_char == '$':
self.new_sentence() self.new_sentence()
return None return None
# Check if sentence is ending (*) elif self.sentence_active:
elif new_char == '*' and self.sentence_active:
self.process_crc = False
self.active_segment += 1
self.gps_segments.append('')
return None
# Check if a section is ended (,), Create a new substring to feed # Check if sentence is ending (*)
# characters to if new_char == '*':
elif new_char == ',' and self.sentence_active: self.process_crc = False
self.active_segment += 1 self.active_segment += 1
self.gps_segments.append('') self.gps_segments.append('')
return None
# Store All Other printable character and check CRC when ready # Check if a section is ended (,), Create a new substring to feed
else: # characters to
if self.sentence_active: elif new_char == ',':
self.active_segment += 1
self.gps_segments.append('')
# Store All Other printable character and check CRC when ready
else:
self.gps_segments[self.active_segment] += new_char self.gps_segments[self.active_segment] += new_char
# When CRC input is disabled, sentence is nearly complete # When CRC input is disabled, sentence is nearly complete
@ -389,23 +396,27 @@ class MicropyGPS(object):
except ValueError: except ValueError:
pass # CRC Value was deformed and could not have been correct pass # CRC Value was deformed and could not have been correct
# Update CRC # Update CRC
if self.process_crc: if self.process_crc:
self.crc_xor ^= ascii_char self.crc_xor ^= ascii_char
# If a Valid Sentence Was received and it's a supported sentence, then # If a Valid Sentence Was received and it's a supported sentence, then parse it!!
# parse it!! if valid_sentence :
if valid_sentence and self.gps_segments[0] in self.supported_sentences:
# Clear Active Processing Flag # Clear Active Processing Flag
self.sentence_active = False self.sentence_active = False
if self.gps_segments[0] in self.supported_sentences:
# parse the Sentence Based on the message type, return True if parse is clean
if self.supported_sentences[self.gps_segments[0]](self):
# Let host know that the GPS object was updated by returning parsed sentence type
return self.gps_segments[0]
# Check that the sentence buffer isn't filling up with Garage waiting for the sentence to complete
if self.char_count > self.SENTENCE_LIMIT:
self.sentence_active = False
# parse the Sentence Based on the message type, return True if
# parse is clean
if self.supported_sentences[self.gps_segments[0]](self):
# Let host know that the GPS object was updated by returning
# parsed sentence type
return self.gps_segments[0]
# Tell Host no new sentence was parsed # Tell Host no new sentence was parsed
return None return None
@ -436,6 +447,8 @@ class MicropyGPS(object):
supported_sentences = {'GPRMC': gprmc, 'GPGGA': gpgga, 'GPVTG': gpvtg, 'GPGSA': gpgsa, 'GPGSV': gpgsv} supported_sentences = {'GPRMC': gprmc, 'GPGGA': gpgga, 'GPVTG': gpvtg, 'GPGSA': gpgsa, 'GPGSV': gpgsv}
if __name__ == "__main__": if __name__ == "__main__":
test_RMC = ['$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62', test_RMC = ['$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62',