📄 nmea0183.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: nmea0183.cpp $
** $Revision: 5 $
** $Modtime: 10/10/98 4:27p $
*/
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
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 *) &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 *) &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 *) &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 );
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 index = 0;
int comparison = 0;
int drop_dead = 0;
int exit_loop = 0;
int lower_limit = 0;
int upper_limit = 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 += " is an unknown type of sentence";
LastSentenceIDReceived = mnemonic;
/*
** Do a Binary Search to call the appropriate function
*/
lower_limit = 0;
upper_limit = m_ResponseTable.GetSize();
index = upper_limit / 2;
drop_dead = ( index < 10 ) ? 10 : index + 2;
/*
** The drop_dead is here as an insurance policy that we will never get stuck in an endless loop.
** I have encountered situations where the inaccuracy of the division leaves the loop stuck when
** it can't find something.
*/
while( exit_loop == 0 )
{
response_p = (RESPONSE *) m_ResponseTable[ 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 = "No Error";
LastSentenceIDParsed = response_p->Mnemonic;
TalkerID = talker_id( m_Sentence );
ExpandedTalkerID = expand_talker_id( TalkerID );
PlainText = response_p->PlainEnglish();
}
else
{
ErrorMessage = response_p->ErrorMessage;
}
exit_loop = 1;
}
else
{
if ( comparison < 0 )
{
upper_limit = index;
}
else
{
lower_limit = index;
}
if ( lower_limit == upper_limit )
{
exit_loop = 1;
return_value = FALSE;
}
else
{
index = ( lower_limit + upper_limit ) / 2;
}
}
drop_dead--;
if ( exit_loop != 1 && drop_dead < 0 )
{
exit_loop = 1;
return_value = FALSE;
}
}
}
else
{
return_value = FALSE;
}
return( return_value );
}
#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 + -