Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a validate option to skip checksum validation entirely. #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pynmea2/nmea.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ def parse(line, check=False):
sentence_type = match.group('sentence_type').upper()
data = data_str.split(',')

if checksum:
cs1 = int(checksum, 16)
cs2 = NMEASentence.checksum(nmea_str)
if cs1 != cs2:
if check:
if checksum:
cs1 = int(checksum, 16)
cs2 = NMEASentence.checksum(nmea_str)
if cs1 != cs2:
raise ChecksumError(
'checksum does not match: %02X != %02X' % (cs1, cs2), data)
else:
raise ChecksumError(
'checksum does not match: %02X != %02X' % (cs1, cs2), data)
elif check:
raise ChecksumError(
'strict checking requested but checksum missing', data)
'strict checking requested but checksum missing', data)

talker_match = NMEASentence.talker_re.match(sentence_type)
if talker_match:
Expand Down
7 changes: 5 additions & 2 deletions pynmea2/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NMEAStreamReader(object):
'''
Reads NMEA sentences from a stream.
'''
def __init__(self, stream=None, errors='raise'):
def __init__(self, stream=None, errors='raise', validate=True):
'''
Create NMEAStreamReader object.

Expand All @@ -23,13 +23,16 @@ def __init__(self, stream=None, errors='raise'):
stream, and continue reading at the next line
`'ignore'` completely ignore and suppress the error, and
continue reading at the next line

`validate`: If `False`, do not perform checksum validation on the incoming data.
'''

if errors not in ERRORS:
raise ValueError('errors must be one of {!r} (was: {!r})'
.format(ERRORS, errors))

self.errors = errors
self.validate = validate
self.stream = stream
self.buffer = ''

Expand All @@ -50,7 +53,7 @@ def next(self, data=None):

for line in lines:
try:
msg = nmea.NMEASentence.parse(line)
msg = nmea.NMEASentence.parse(line, check=self.validate)
yield msg
except nmea.ParseError as e:
if self.errors == 'raise':
Expand Down