📄 sentence.cpp
字号:
#include "nmea0183.h"
#pragma hdrstop
/*
** Author: Samuel R. Blackburn
** Internet: sam_blackburn@pobox.com
**
** You can use it any way you like as long as you don't try to sell it.
**
** Copyright, 1996, Samuel R. Blackburn
**
** $Workfile: sentence.cpp $
** $Revision: 5 $
** $Modtime: 10/10/98 2:43p $
*/
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
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 == "A" )
{
return( True );
}
else if ( field_data == "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 == "d" )
{
return( F3E_G3E_SimplexTelephone );
}
else if ( field_data == "e" )
{
return( F3E_G3E_DuplexTelephone );
}
else if ( field_data == "m" )
{
return( J3E_Telephone );
}
else if ( field_data == "o" )
{
return( H3E_Telephone );
}
else if ( field_data == "q" )
{
return( F1B_J2B_FEC_NBDP_TelexTeleprinter );
}
else if ( field_data == "s" )
{
return( F1B_J2B_ARQ_NBDP_TelexTeleprinter );
}
else if ( field_data == "w" )
{
return( F1B_J2B_ReceiveOnlyTeleprinterDSC );
}
else if ( field_data == "x" )
{
return( A1A_MorseTapeRecorder );
}
else if ( field_data == "{" )
{
return( A1A_MorseKeyHeadset );
}
else if ( field_data == "|" )
{
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( ::atof( field_data ) );
}
EASTWEST SENTENCE::EastOrWest( int field_number ) const
{
CString field_data;
field_data = Field( field_number );
if ( field_data == "E" )
{
return( East );
}
else if ( field_data == "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 );
}
void SENTENCE::Finish( void )
{
BYTE checksum = ComputeChecksum();
char temp_string[ 10 ];
::sprintf( temp_string, "*%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( ::atoi( integer_string ) );
}
NMEA0183_BOOLEAN SENTENCE::IsChecksumBad( int checksum_field_number ) const
{
/*
** 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 == "" )
{
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 == "L" )
{
return( Left );
}
else if ( field_data == "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 == "N" )
{
return( North );
}
else if ( field_data == "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 == "B" )
{
return( BottomTrackingLog );
}
else if ( field_data == "M" )
{
return( ManuallyEntered );
}
else if ( field_data == "W" )
{
return( WaterReferenced );
}
else if ( field_data == "R" )
{
return( RadarTrackingOfFixedTarget );
}
else if ( field_data == "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 )
{
char temp_number[ 3 ];
temp_number[ 2 ] = 0x00;
temp_number[ 0 ] = temp_string[ 0 ];
temp_number[ 1 ] = temp_string[ 1 ];
int hours = ::atoi( temp_number );
temp_number[ 0 ] = temp_string[ 2 ];
temp_number[ 1 ] = temp_string[ 3 ];
int minutes = ::atoi( temp_number );
temp_number[ 0 ] = temp_string[ 4 ];
temp_number[ 1 ] = temp_string[ 5 ];
int seconds = ::atoi( temp_number );
int year = return_value.GetYear();
int month = return_value.GetMonth();
int day = return_value.GetDay();
return_value = CTime( year, month, day, hours, minutes, seconds );
}
return( return_value );
}
TRANSDUCER_TYPE SENTENCE::TransducerType( int field_number ) const
{
CString field_data;
field_data = Field( field_number );
if ( field_data == "A" )
{
return( AngularDisplacementTransducer );
}
else if ( field_data == "D" )
{
return( LinearDisplacementTransducer );
}
else if ( field_data == "C" )
{
return( TemperatureTransducer );
}
else if ( field_data == "F" )
{
return( FrequencyTransducer );
}
else if ( field_data == "N" )
{
return( ForceTransducer );
}
else if ( field_data == "P" )
{
return( PressureTransducer );
}
else if ( field_data == "R" )
{
return( FlowRateTransducer );
}
else if ( field_data == "T" )
{
return( TachometerTransducer );
}
else if ( field_data == "H" )
{
return( HumidityTransducer );
}
else if ( field_data == "V" )
{
return( VolumeTransducer );
}
else
{
return( TransducerUnknown );
}
}
/*
** Operators
*/
SENTENCE::operator CString() const
{
return( Sentence );
}
const SENTENCE& SENTENCE::operator = ( const SENTENCE& source )
{
Sentence = source.Sentence;
return( *this );
}
const SENTENCE& SENTENCE::operator = ( const CString& source )
{
Sentence = source;
return( *this );
}
const SENTENCE& SENTENCE::operator += ( const CString& source )
{
Sentence += ",";
Sentence += source;
return( *this );
}
const SENTENCE& SENTENCE::operator += ( double value )
{
char temp_string[ 80 ];
::sprintf( temp_string, "%.3f", value );
Sentence += ",";
Sentence += temp_string;
return( *this );
}
const SENTENCE& SENTENCE::operator += ( COMMUNICATIONS_MODE mode )
{
Sentence += ",";
switch( mode )
{
case F3E_G3E_SimplexTelephone:
Sentence += "d";
break;
case F3E_G3E_DuplexTelephone:
Sentence += "e";
break;
case J3E_Telephone:
Sentence += "m";
break;
case H3E_Telephone:
Sentence += "o";
break;
case F1B_J2B_FEC_NBDP_TelexTeleprinter:
Sentence += "q";
break;
case F1B_J2B_ARQ_NBDP_TelexTeleprinter:
Sentence += "s";
break;
case F1B_J2B_ReceiveOnlyTeleprinterDSC:
Sentence += "w";
break;
case A1A_MorseTapeRecorder:
Sentence += "x";
break;
case A1A_MorseKeyHeadset:
Sentence += "{";
break;
case F1C_F2C_F3C_FaxMachine:
Sentence += "|";
break;
}
return( *this );
}
const SENTENCE& SENTENCE::operator += ( TRANSDUCER_TYPE transducer )
{
Sentence += ",";
switch( transducer )
{
case TemperatureTransducer:
Sentence += "C";
break;
case AngularDisplacementTransducer:
Sentence += "A";
break;
case LinearDisplacementTransducer:
Sentence += "D";
break;
case FrequencyTransducer:
Sentence += "F";
break;
case ForceTransducer:
Sentence += "N";
break;
case PressureTransducer:
Sentence += "P";
break;
case FlowRateTransducer:
Sentence += "R";
break;
case TachometerTransducer:
Sentence += "T";
break;
case HumidityTransducer:
Sentence += "H";
break;
case VolumeTransducer:
Sentence += "V";
break;
}
return( *this );
}
const SENTENCE& SENTENCE::operator += ( NORTHSOUTH northing )
{
Sentence += ",";
if ( northing == North )
{
Sentence += "N";
}
else if ( northing == South )
{
Sentence += "S";
}
return( *this );
}
const SENTENCE& SENTENCE::operator += ( int value )
{
char temp_string[ 80 ];
::sprintf( temp_string, "%d", value );
Sentence += ",";
Sentence += temp_string;
return( *this );
}
const SENTENCE& SENTENCE::operator += ( EASTWEST easting )
{
Sentence += ",";
if ( easting == East )
{
Sentence += "E";
}
else if ( easting == West )
{
Sentence += "W";
}
return( *this );
}
const SENTENCE& SENTENCE::operator += ( NMEA0183_BOOLEAN boolean )
{
Sentence += ",";
if ( boolean == True )
{
Sentence += "A";
}
else if ( boolean == False )
{
Sentence += "V";
}
return( *this );
}
const SENTENCE& SENTENCE::operator += ( LATLONG& source )
{
source.Write( *this );
return( *this );
}
const SENTENCE& SENTENCE::operator += ( const CTime time )
{
CString temp_string = time.Format( "%H%M%S" );
Sentence += ",";
Sentence += temp_string;
return( *this );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -