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

📄 sentence.cpp

📁 提取各种NEMA0183格式数据的类编程。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
** 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: sentence.cpp $
** $Revision: 5 $
** $Modtime: 10/10/98 2:43p $
*/

#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

SENTENCE::SENTENCE()
{
   Sentence.Empty();
}

SENTENCE::~SENTENCE()
{
   Sentence.Empty();
}

NMEA0183_BOOLEAN SENTENCE::Boolean( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "A" ) )
   {
      return( True );
   }
   else if ( field_data == TEXT( "V" ) )
   {
      return( False );
   }
   else
   {
      return( NMEA_Unknown );
   }
}

COMMUNICATIONS_MODE SENTENCE::CommunicationsMode( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "d" ) )
   {		 
      return( F3E_G3E_SimplexTelephone );
   }
   else if ( field_data == TEXT( "e" ) )
   {
      return( F3E_G3E_DuplexTelephone );
   }
   else if ( field_data == TEXT( "m" ) )
   {
      return( J3E_Telephone );
   }
   else if ( field_data == TEXT( "o" ) )
   {
      return( H3E_Telephone );
   }
   else if ( field_data == TEXT( "q" ) )
   {
      return( F1B_J2B_FEC_NBDP_TelexTeleprinter );
   }
   else if ( field_data == TEXT( "s" ) )
   {
      return( F1B_J2B_ARQ_NBDP_TelexTeleprinter );
   }
   else if ( field_data == TEXT( "w" ) )
   {
      return( F1B_J2B_ReceiveOnlyTeleprinterDSC );
   }
   else if ( field_data == TEXT( "x" ) )
   {
      return( A1A_MorseTapeRecorder );
   }
   else if ( field_data == TEXT( "{" ) )
   {
      return( A1A_MorseKeyHeadset );
   }
   else if ( field_data == TEXT( "|" ) )
   {
      return( F1C_F2C_F3C_FaxMachine );
   }
   else
   {
      return( CommunicationsModeUnknown );
   }
}

BYTE SENTENCE::ComputeChecksum( void ) const
{
   BYTE checksum_value = 0;

   int string_length = Sentence.GetLength();
   int index = 1; // Skip over the $ at the begining of the sentence

   while( index < string_length    && 
          Sentence[ index ] != '*' && 
          Sentence[ index ] != CARRIAGE_RETURN && 
          Sentence[ index ] != LINE_FEED )
   {
      checksum_value ^= Sentence[ index ];
      index++;
   }

   return( checksum_value );
}

double SENTENCE::Double( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   return( _tstof( field_data ) );
}

EASTWEST SENTENCE::EastOrWest( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "E" ) )
   {
      return( East );
   }
   else if ( field_data == TEXT( "W" ) )
   {
      return( West );
   }
   else
   {
      return( EW_Unknown );
   }
}

const CString SENTENCE::Field( int desired_field_number ) const
{
   // Thanks to Vilhelm Persson (vilhelm.persson@st.se) for finding a 
   // bug that lived here.

   CString return_string;

   return_string.Empty();

   int index                = 1; // Skip over the $ at the begining of the sentence
   int current_field_number = 0;
   int string_length        = 0;

   string_length = Sentence.GetLength();

   while( current_field_number < desired_field_number && index < string_length )
   {
      if ( Sentence[ index ] == ',' || Sentence[ index ] == '*' )
      {
         current_field_number++;
      }

      index++;
   }

   if ( current_field_number == desired_field_number )
   {
      while( index < string_length    &&
             Sentence[ index ] != ',' &&
             Sentence[ index ] != '*' &&
             Sentence[ index ] != 0x00 )
      {
         return_string += Sentence[ index ];
         index++;
      }
   }

   return( return_string );
}

WORD SENTENCE::GetNumberOfDataFields( void ) const
{
   int index                = 1; // Skip over the $ at the begining of the sentence
   int current_field_number = 0;
   int string_length        = 0;

   string_length = Sentence.GetLength();

   while( index < string_length )
   {
      if ( Sentence[ index ] == '*' )
      {
         return( (WORD) current_field_number );
      }

      if ( Sentence[ index ] == ',' )
      {
         current_field_number++;
      }

      index++;
   }

   return( (WORD) current_field_number );
}

int SENTENCE::ChecksumFieldNumber( void ) const
{
   int index                = 1; // Skip over the $ at the begining of the sentence
   int current_field_number = 0;
   int string_length        = 0;

   string_length = Sentence.GetLength();

   while( index < string_length )
   {
      if ( Sentence[ index ] == '*' )
      {
         return( current_field_number + 1 );
      }

      if ( Sentence[ index ] == ',' )
      {
         current_field_number++;
      }

      index++;
   }

   return( -1 );
}

void SENTENCE::Finish( void )
{
   BYTE checksum = ComputeChecksum();

   CString temp_string;

   temp_string.Format( TEXT( "*%02X%c%c" ), (int) checksum, CARRIAGE_RETURN, LINE_FEED );

   Sentence += temp_string;
}

int SENTENCE::Integer( int field_number ) const
{
   CString integer_string;

   integer_string = Field( field_number );

   return( ::_ttoi( integer_string ) );
}

NMEA0183_BOOLEAN SENTENCE::IsChecksumBad( void ) const
{
   const int checksum_field_number = ChecksumFieldNumber();

   if ( checksum_field_number == (-1) )
   {
      return( NMEA_Unknown );
   }

   /*
   ** Checksums are optional, return TRUE if an existing checksum is known to be bad
   */

   CString checksum_in_sentence = Field( checksum_field_number );

   if ( checksum_in_sentence.GetLength() == 0 )
   {
      return( NMEA_Unknown );
   }

   if ( ComputeChecksum() != HexValue( checksum_in_sentence ) )
   {
      return( True );
   } 

   return( False );
}

LEFTRIGHT SENTENCE::LeftOrRight( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "L" ) )
   {
      return( Left );
   }
   else if ( field_data == TEXT( "R" ) )
   {
      return( Right );
   }
   else
   {
      return( LR_Unknown );
   }
}

NORTHSOUTH SENTENCE::NorthOrSouth( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "N" ) )
   {
      return( North );
   }
   else if ( field_data == TEXT( "S" ) )
   {
      return( South );
   }
   else
   {
      return( NS_Unknown );
   }
}

REFERENCE SENTENCE::Reference( int field_number ) const
{
   CString field_data;

   field_data = Field( field_number );

   if ( field_data == TEXT( "B" ) )
   {
      return( BottomTrackingLog );
   }
   else if ( field_data == TEXT( "M" ) )
   {
      return( ManuallyEntered );
   }
   else if ( field_data == TEXT( "W" ) )
   {
      return( WaterReferenced );
   }
   else if ( field_data == TEXT( "R" ) )
   {
      return( RadarTrackingOfFixedTarget );
   }
   else if ( field_data == TEXT( "P" ) )
   {
      return( PositioningSystemGroundReference );
   }
   else
   {
      return( ReferenceUnknown );
   }
}

const CTime SENTENCE::Time( int field_number ) const
{
   CTime return_value;

   return_value = CTime::GetCurrentTime();

   CString temp_string = Field( field_number );

   if ( temp_string.GetLength() >= 6 )
   {
      TCHAR temp_number[ 3 ];

⌨️ 快捷键说明

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