NMEA Parser
The NMEA parser will decode any NMEA protocol message (official or not) and will allow you to create custom NMEA messages.
Note
The NMEA_parser
class supports NMEA messages up to 120 characters
Methods
- class NMEA_parser(cb=None, split_payload=False, verbose=0)
The
NMEA_parser
class needs to be initialized with the following parameters:Parameter name
Value
Description
cb
Any function.
Default value is None.Callback function to be called when a
NMEA message is parsed.
Callback function argument is a variable
containing the NMEA message fields in an array.split_payload
True, False.
Default value is False.If True, splits the message payload and returns
a list where each element is a NMEA field.verbose
0, 1, default value is 0
If set to 1 parser will print in the terminal a
CRC failed message if the parsed message
is not valid- Class Attributes:
payload
: NMEA message payload from which the CRC is calculated.
This is the complete NMEA message contained between the $ and * characters (excluded).
- parse(buf)
Parse NMEA message
- Args:
buf
: Variable containing one or many NMEA messages. This variable may contain messages in other procotols, which will be ignored by the NMEA parser.- Return:
Array of NMEA messages contained in
buf
.- Example to parse a string with mixed NMEA and non-NMEA messages:
>>> import sbc >>> nmea = sbc.NMEA_parser() >>> buf = b'$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F' >>> buf = buf + b'This is not a NMEA message' >>> buf = buf + b'$GPGST,172814.0,0.006,0.023,0.020,273.6,0.023,0.020,0.031*6A' >>> msg = nmea.parse(buf) >>> print(msg) ['GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031', 'GPGST,172814.0,0.006,0.023,0.020,273.6,0.023,0.020,0.031'] >>> print(msg[0]) GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031 >>> nmea = sbc.NMEA_parser(split_payload = True) >>> msg = nmea.parse(buf) >>> print(msg) [['GPGGA', '172814.0', '3723.46587704', 'N', '12202.26957864', 'W', '2', '6', '1.2', '18.893', 'M', '-25.669', 'M', '2.0', '0031'], ['GPGST', '172814.0', '0.006', '0.023', '0.020', '273.6', '0.023', '0.020', '0.031']] >>> print(msg[0][0],msg[1][0]) GPGGA GPGST
- Example to use a callback function that prints GPGGA message received if a GPGGA message is received:
>>> import sbc >>> def callback_nmea(msg): >>> if msg[0] == 'GPGGA': >>> print('GPGGA message received') >>> nmea = sbc.NMEA_parser(cb = callback_nmea, split_payload = True) >>> buf = b'$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F' >>> buf = buf + b'This is not a NMEA message' >>> buf = buf + b'$GPGST,172814.0,0.006,0.023,0.020,273.6,0.023,0.020,0.031*6A' >>> msg = nmea.parse(buf) GPGGA message received
- chksum_nmea()
Calculates the two checksum bytes of the NMEA message payload.
- Return:
NMEA message checksum.
- Example to calculate the NMEA checksum of a payload and build a NMEA message from it:
>>> import sbc >>> nmea = sbc.NMEA_parser() >>> nmea.payload = b'GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031' >>> nmea.payload_len = len(nmea.payload) >>> print(nmea.chksum_nmea()) b'4F' >>> msg = b'$' + nmea.payload + b'*' + nmea.chksum_nmea() >>> print(msg.decode()) $GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F