x
Revision 0:a6e68073c162, committed 2018-07-23
- Comitter:
- JackB
- Date:
- Mon Jul 23 12:23:55 2018 +0000
- Commit message:
- NMEA
Changed in this revision
NMEA0183.cpp | Show annotated file Show diff for this revision Revisions of this file |
NMEA0183.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r a6e68073c162 NMEA0183.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NMEA0183.cpp Mon Jul 23 12:23:55 2018 +0000 @@ -0,0 +1,45 @@ +#include "NMEA0183.h" + +NMEA0183::NMEA0183() +{ +} + +// CheckParity +// Returns 1 if valid +int NMEA0183::CheckParity(char *nmea0183_string) +{ + // Calculate parity + char parity_calculated = 0x00; + for (int i = 1; i < strlen(nmea0183_string)-3; i++) { + parity_calculated ^= nmea0183_string[i]; + } + + // Print parity_calculated to string +// char parity_calculated_str[3]; +// snprintf(parity_calculated_str, 3, "%02x", parity_calculated); + + uint8_t len = strlen(nmea0183_string); + char parity_given = hex2dec(nmea0183_string[len-2]) * 16 + hex2dec(nmea0183_string[len-1]); + + // Compare values + if (parity_calculated == parity_given) { + return 1; + } + return 0; +} + +void NMEA0183::SubString(char *s, char *d, int pos, int len) //usage: substring(Src, Dst, Pos, Len); +{ +//usage: substring(Source, Destination, pos, len); + char *t; + + s=s+pos; + t=s+len; + while (s!=t) { + *d=*s; + s++; + d++; + } + *d='\0'; +} +
diff -r 000000000000 -r a6e68073c162 NMEA0183.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NMEA0183.h Mon Jul 23 12:23:55 2018 +0000 @@ -0,0 +1,27 @@ +#ifndef __NMEA0183_H__ +#define __NMEA0183_H__ + +#include "mbed.h" + +#define NMEA0183_BUF_SIZE 240 + +class NMEA0183 { +public: + NMEA0183(); + int CheckParity(char *nmea0183_string); + +private: + void SubString(char *s, char *d, int pos, int len); //usage: SubString(Src, Dst, Pos, Len); + +protected: + char buf_[NMEA0183_BUF_SIZE]; + inline int hex2dec(char c) { + if (c >= '0' && c <='9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return 0; + }; + +}; + +#endif