⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nmea0183.cpp

📁 提取各种NEMA0183格式数据的类编程。
💻 CPP
字号:
/*
** Author: Samuel R. Blackburn
** Internet: wfc@pobox.com
**
** Copyright, 1996-2005, Samuel R. Blackburn
**
** "You can get credit for something or get it done, but not both."
** Dr. Richard Garwin
**
** BSD License follows.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer. Redistributions
** in binary form must reproduce the above copyright notice, this list
** of conditions and the following disclaimer in the documentation and/or
** other materials provided with the distribution. Neither the name of
** the WFC nor the names of its contributors may be used to endorse or
** promote products derived from this software without specific prior
** written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
** $Workfile: nmea0183.cpp $
** $Revision: 5 $
** $Modtime: 10/10/98 4:27p $
*/

#include "nmea0183.h"
#pragma hdrstop

#if defined( _DEBUG ) && defined( _INC_CRTDBG )
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif // _DEBUG

NMEA0183::NMEA0183()
{
   m_Initialize();

   m_ResponseTable.Add( (RESPONSE *) &Aam );
   m_ResponseTable.Add( (RESPONSE *) &Alm );
   m_ResponseTable.Add( (RESPONSE *) &Apb );
   m_ResponseTable.Add( (RESPONSE *) &Asd );
   m_ResponseTable.Add( (RESPONSE *) &Bec );
   m_ResponseTable.Add( (RESPONSE *) &Bod );
   m_ResponseTable.Add( (RESPONSE *) &Bwc );
   m_ResponseTable.Add( (RESPONSE *) &Bwr );
   m_ResponseTable.Add( (RESPONSE *) &Bww );
   m_ResponseTable.Add( (RESPONSE *) &Dbt );
   m_ResponseTable.Add( (RESPONSE *) &Dcn );
   m_ResponseTable.Add( (RESPONSE *) &Dpt );
   m_ResponseTable.Add( (RESPONSE *) &Fsi );
   m_ResponseTable.Add( (RESPONSE *) &Gda ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gdf ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gdp ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gga );
   m_ResponseTable.Add( (RESPONSE *) &Gla ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Glc );
   m_ResponseTable.Add( (RESPONSE *) &Glf ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gll );
   m_ResponseTable.Add( (RESPONSE *) &Glp ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gsa );
   m_ResponseTable.Add( (RESPONSE *) &Gsv );
   m_ResponseTable.Add( (RESPONSE *) &Gtd ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gxa );
   m_ResponseTable.Add( (RESPONSE *) &Gxf ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Gxp ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Hcc ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Hdg );
   m_ResponseTable.Add( (RESPONSE *) &Hdm ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Hdt );
   m_ResponseTable.Add( (RESPONSE *) &Hsc );
   m_ResponseTable.Add( (RESPONSE *) &Hvd ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Hvm ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Ima ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Lcd );
   m_ResponseTable.Add( (RESPONSE *) &Mhu ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Mta ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Mtw );
   m_ResponseTable.Add( (RESPONSE *) &Mwd ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Mwv );
   m_ResponseTable.Add( (RESPONSE *) &Oln );
   m_ResponseTable.Add( (RESPONSE *) &Osd );
   m_ResponseTable.Add( (RESPONSE *) &Proprietary );
   m_ResponseTable.Add( (RESPONSE *) &Rma );
   m_ResponseTable.Add( (RESPONSE *) &Rmb );
   m_ResponseTable.Add( (RESPONSE *) &Rmc );
   m_ResponseTable.Add( (RESPONSE *) &Rmm ); // Garmin Datum as provided by Jeff Bailey
   m_ResponseTable.Add( (RESPONSE *) &Rot );
   m_ResponseTable.Add( (RESPONSE *) &Rpm );
   m_ResponseTable.Add( (RESPONSE *) &Rsa );
   m_ResponseTable.Add( (RESPONSE *) &Rsd );
   m_ResponseTable.Add( (RESPONSE *) &Rte );
   m_ResponseTable.Add( (RESPONSE *) &Sfi );
   m_ResponseTable.Add( (RESPONSE *) &Stn );
   m_ResponseTable.Add( (RESPONSE *) &Tep ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Trf );
   m_ResponseTable.Add( (RESPONSE *) &Ttm );
   m_ResponseTable.Add( (RESPONSE *) &Vbw );
   m_ResponseTable.Add( (RESPONSE *) &Vhw );
   m_ResponseTable.Add( (RESPONSE *) &Vdr );
   m_ResponseTable.Add( (RESPONSE *) &Vlw );
   m_ResponseTable.Add( (RESPONSE *) &Vpw );
   m_ResponseTable.Add( (RESPONSE *) &Vtg );
   m_ResponseTable.Add( (RESPONSE *) &Vwe ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Vwr ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Vwt ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Wcv ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Wdc ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Wdr ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Wnc );
   m_ResponseTable.Add( (RESPONSE *) &Wpl );
   m_ResponseTable.Add( (RESPONSE *) &Xdr );
   m_ResponseTable.Add( (RESPONSE *) &Xte );
   m_ResponseTable.Add( (RESPONSE *) &Xtr );
   m_ResponseTable.Add( (RESPONSE *) &Zda );
   m_ResponseTable.Add( (RESPONSE *) &Zfi ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zfo );
   m_ResponseTable.Add( (RESPONSE *) &Zlz ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zpi ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zta ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zte ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Ztg );
   m_ResponseTable.Add( (RESPONSE *) &Zti ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zwp ); // Sentence Not Recommended For New Designs
   m_ResponseTable.Add( (RESPONSE *) &Zzu ); // Sentence Not Recommended For New Designs

   m_SortResponseTable();
   m_SetContainerPointers();
}

NMEA0183::~NMEA0183()
{
   m_Initialize();
}

void NMEA0183::m_Initialize( void )
{
   ErrorMessage.Empty();
}

void NMEA0183::m_SetContainerPointers( void )
{
   int index = 0;
   int number_of_entries_in_table = m_ResponseTable.GetSize();

   RESPONSE *this_response = (RESPONSE *) NULL;

   index = 0;

   while( index < number_of_entries_in_table )
   {
      this_response = (RESPONSE *) m_ResponseTable[ index ];

      this_response->SetContainer( this );

      index++;
   }
}

void NMEA0183::m_SortResponseTable( void )
{
   int index = 0;
   int number_of_entries_in_table = m_ResponseTable.GetSize() - 1;

   RESPONSE *this_response = (RESPONSE *) NULL;
   RESPONSE *that_response = (RESPONSE *) NULL;

   BOOL sorted = FALSE;

   while( sorted == FALSE )
   {
      sorted = TRUE;

      index = 0;

      while( index < number_of_entries_in_table )
      {
         this_response = (RESPONSE *) m_ResponseTable[ index     ];
         that_response = (RESPONSE *) m_ResponseTable[ index + 1 ];

         if ( this_response->Mnemonic.Compare( that_response->Mnemonic ) > 0 )
         {
            m_ResponseTable[ index     ] = that_response;
            m_ResponseTable[ index + 1 ] = this_response;

            sorted = FALSE;
         }

         index++;
      }
   }
}

/*
** Public Interface
*/

BOOL NMEA0183::IsGood( void ) const
{
   /*
   ** NMEA 0183 sentences begin with $ and and with CR LF
   */

   if ( m_Sentence.Sentence[ 0 ] != '$' )
   {
      return( FALSE );
   }

   /*
   ** Next to last character must be a CR
   */

   if ( m_Sentence.Sentence.Mid( m_Sentence.Sentence.GetLength() - 2, 1 ) != CARRIAGE_RETURN )
   {
      return( FALSE );
   }

   if ( m_Sentence.Sentence.Right( 1 ) != LINE_FEED )
   {
      return( FALSE );
   }

   return( TRUE );
}

BOOL NMEA0183::Parse( void )
{
   BOOL return_value = FALSE;

   if ( IsGood() )
   {
      int table_index = 0;
      int comparison  = 0;
      int exit_loop   = 0;

      CString mnemonic;

      RESPONSE * response_p = (RESPONSE *) NULL;

      mnemonic = m_Sentence.Field( 0 );

      /*
      ** See if this is a proprietary field
      */

      if ( mnemonic.Left( 1 ) == 'P' )
      {
         mnemonic = "P";
      }
      else
      {
         mnemonic = mnemonic.Right( 3 );
      }

      /*
      ** Set up our default error message
      */

      ErrorMessage  = mnemonic;
      ErrorMessage += TEXT( " is an unknown type of sentence" );

      LastSentenceIDReceived = mnemonic;

      while( exit_loop == 0 )
      {
         response_p = (RESPONSE *) m_ResponseTable.GetAt( table_index );

         comparison = mnemonic.Compare( response_p->Mnemonic );

         if ( comparison == 0 )
         {
            return_value = response_p->Parse( m_Sentence );

            /*
            ** Set your ErrorMessage
            */

            if ( return_value == TRUE )
            {
               /*
               ** Now that we sucessfully parsed a sentence, record stuff *about* the transaction
               */

               ErrorMessage         = TEXT( "No Error" );
               LastSentenceIDParsed = response_p->Mnemonic;
               TalkerID             = talker_id( m_Sentence );
               ExpandedTalkerID     = expand_talker_id( TalkerID );
               PlainText            = response_p->ToText();
            }
            else
            {
               ErrorMessage = response_p->ErrorMessage;
            }

            exit_loop = 1;
         }
         else
         {
            if ( comparison < 0 )
            {
               return( FALSE ); // Sentence not supported
            }
            else
            {
               // We have not yet found the desired mnemonic
               table_index++;
            }
         }
      }
   }
   else
   {
      return_value = FALSE;
   }

   return( return_value );
}

size_t NMEA0183::NumberOfSupportedSentences( void ) const
{
   return( m_ResponseTable.GetSize() );
}

BOOL NMEA0183::GetSentenceMnemonic( long table_index, CString& mnemonic ) const
{
   if ( table_index < 0 || table_index > m_ResponseTable.GetUpperBound() )
   {
      mnemonic.Empty();
      return( FALSE );
   }

   RESPONSE * response_p = (RESPONSE *) m_ResponseTable.GetAt( table_index );

   if ( response_p == NULL )
   {
      mnemonic.Empty();
      return( FALSE );
   }

   mnemonic = response_p->Mnemonic;

   return( TRUE );
}

#if 0

void NMEA0183::Serialize( CArchive& archive )
{
   CObject::Serialize( archive );

   if ( archive.IsStoring() )
   {
      archive << m_Sentence;
      archive << ErrorMessage;
      archive << LastSentenceIDParsed;
      archive << LastSentenceIDReceived;
      archive << PlainText;
      archive << TalkerID;
      archive << ExpandedTalkerID;
   }
   else
   {
      //archive >> m_Sentence;
      archive >> ErrorMessage;
      archive >> LastSentenceIDParsed;
      archive >> LastSentenceIDReceived;
      archive >> PlainText;
      archive >> TalkerID;
      archive >> ExpandedTalkerID;
   }
}

#endif // 0

NMEA0183& NMEA0183::operator << ( const char *source )
{
   m_Sentence = source;

   return( *this );
}

NMEA0183& NMEA0183::operator >> ( CString& destination )
{
   destination = m_Sentence;

   return( *this );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -