INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
nmeasentence.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2004-2010 Alex Brooks, Alexei Makarenko, Tobias Kaupp, Duncan Mercer 00005 * 00006 * This distribution is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 00011 #ifndef GBXGPSUTILACFR_NMEA_H 00012 #define GBXGPSUTILACFR_NMEA_H 00013 00014 #include <vector> 00015 #include <string> 00016 00017 /* 00018 00019 for further info: 00020 00021 http://www.kh-gps.de/nmea-faq.htm 00022 http://vancouver-webpages.com/peter/nmeafaq.txt 00023 00024 NMEA-0183 sentence 00025 00026 $aaccc,c--c*hh<CR><LF> 00027 || || || | 00028 || || || \________ <CR><LF> - End of sentence (0xOD 0xOA) 00029 || || |\__________ hh - Checksum field hexadecimal [optional] 00030 || || \___________ * - Checksum delimiter (0x2A) [optional] 00031 || |\_______________ c--c - Data sentence block 00032 || \________________ , - Field delimiter (0x2c) 00033 |\_____________________ aaccc - Address field/Command 00034 \______________________ $ - Start of sentence 00035 00036 The optional checksum field consists of a "*" and two hex digits 00037 representing the exclusive OR of all characters between, but not 00038 including, the "$" and "*". A checksum is required on some 00039 sentences. 00040 00041 */ 00042 00043 namespace gbxgpsutilacfr { 00044 00045 00046 // class SOEXPORT NmeaException : public std::exception 00047 class NmeaException : public std::exception 00048 { 00049 public: 00050 NmeaException(const char *message) 00051 : message_(message) {} 00052 00053 NmeaException(const std::string &message) 00054 : message_(message) {} 00055 00056 virtual ~NmeaException() throw() {} 00057 00058 virtual const char* what() const throw() { return message_.c_str(); } 00059 00060 protected: 00061 std::string message_; 00062 }; 00063 00064 00065 #define MAX_SENTENCE_LEN 256 00066 00067 // When using class to send data, need to add checksum, when reciving data need to test checksum 00068 // Checksums are usually optional 00069 enum NmeaSentenceOptions 00070 { 00071 TestChecksum, 00072 AddChecksum, 00073 DontTestOrAddChecksum 00074 }; 00075 00076 // class SOEXPORT NmeaSentence{ 00077 class NmeaSentence 00078 { 00079 public: 00080 NmeaSentence(); 00081 NmeaSentence(const std::string &sentence, NmeaSentenceOptions addOrTestCheckSum=DontTestOrAddChecksum ); 00082 00083 // Set up the internal data for a sentence. 00084 // May throw NmeaException if TestChecksum is specified. 00085 void setSentence(const std::string &data, NmeaSentenceOptions addOrTestCheckSum=DontTestOrAddChecksum ); 00086 00087 // Do we have the raw string? 00088 bool haveSentence() const { return !sentence_.empty(); }; 00089 00090 // Do we have parsed fields? 00091 bool haveTokens() const { return haveTokens_; }; 00092 00093 // Do we have a valid checksum? 00094 bool haveValidChecksum() const { return checkSumOK_; }; 00095 00096 // Have we checked the checksum? 00097 bool haveTestedChecksum()const { return haveCheckSum_; }; 00098 00099 // calculate the checksum from sentence 00100 // May throw NmeaException. 00101 bool testChecksumOk(); 00102 00103 // Return the raw sentence string 00104 const std::string &sentence() const { return sentence_; }; 00105 00106 // Return a single data token as a string. 00107 // Throws an exception if that token is empty (see 'isDataTokenEmpty()') 00108 const std::string &getDataToken(int i) const; 00109 00110 bool isDataTokenEmpty(int i) const; 00111 00112 // Return the number of fields 00113 int numDataTokens() const { return dataTokens_.size(); }; 00114 00115 // Tokenise the string that we received 00116 void parseTokens(); 00117 00118 private: 00119 void init(); 00120 // May throw NmeaException. 00121 void addCheckSum(); 00122 // Have we parsed data into tokens ? 00123 bool haveTokens_; 00124 // Have we a checksum and is it valid? 00125 bool haveCheckSum_; 00126 bool checkSumOK_; 00127 // The raw sentence, allow for terminator 00128 // char sentence_[MAX_SENTENCE_LEN+1]; 00129 std::string sentence_; 00130 // The tokenised data 00131 std::vector<std::string> dataTokens_; 00132 }; 00133 00134 } 00135 00136 #endif |