From 2f98caeba8da29c93a2cac921f36fc8616ea683b Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 23 Sep 2005 21:23:13 +0000 Subject: [PATCH] Switch over to using SGIOChannel so we can try to read data live from the serial port at some future time. --- utils/GPSsmooth/MIDG-II.cxx | 48 +++++++++++++++++++++---------------- utils/GPSsmooth/MIDG-II.hxx | 6 ++--- utils/GPSsmooth/Makefile.am | 2 +- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/utils/GPSsmooth/MIDG-II.cxx b/utils/GPSsmooth/MIDG-II.cxx index bdcef9350..f950a1139 100644 --- a/utils/GPSsmooth/MIDG-II.cxx +++ b/utils/GPSsmooth/MIDG-II.cxx @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -319,6 +320,7 @@ void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att ) // load the specified file, return the number of records loaded bool MIDGTrack::load( const string &file ) { + int count = 0; MIDGpos pos; MIDGatt att; @@ -329,16 +331,17 @@ bool MIDGTrack::load( const string &file ) { pos_data.clear(); att_data.clear(); - // openg the file - fd = fopen( file.c_str(), "r" ); - - if ( fd == NULL ) { + // open the file + SGFile input( file ); + if ( !input.open( SG_IO_IN ) ) { cout << "Cannot open file: " << file << endl; return false; } - while ( ! feof( fd ) ) { - int id = next_message( fd, &pos, &att ); + while ( ! input.eof() ) { + // cout << "looking for next message ..." << endl; + int id = next_message( &input, &pos, &att ); + count++; if ( id == 10 ) { if ( att.get_msec() > att_time ) { @@ -357,38 +360,41 @@ bool MIDGTrack::load( const string &file ) { } } + cout << "processed " << count << " messages" << endl; return true; } // load the next message of a real time data stream -int MIDGTrack::next_message( FILE *fd, MIDGpos *pos, MIDGatt *att ) { +int MIDGTrack::next_message( SGIOChannel *ch, MIDGpos *pos, MIDGatt *att ) { + char tmpbuf[256]; + char savebuf[256]; + // scan for sync characters - int sync0, sync1; - sync0 = fgetc( fd ); - sync1 = fgetc( fd ); - while ( (sync0 != 129 || sync1 != 161) && !feof(fd) ) { + uint8_t sync0, sync1; + ch->read( tmpbuf, 1 ); sync0 = (unsigned char)tmpbuf[0]; + ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0]; + while ( (sync0 != 129 || sync1 != 161) && !ch->eof() ) { sync0 = sync1; - sync1 = fgetc( fd ); + ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0]; } // cout << "start of message ..." << endl; // read message id and size - int id = fgetc( fd ); - int size = fgetc( fd ); - // cout << "message = " << id << " size = " << size << endl; + ch->read( tmpbuf, 1 ); uint8_t id = (unsigned char)tmpbuf[0]; + ch->read( tmpbuf, 1 ); uint8_t size = (unsigned char)tmpbuf[0]; + // cout << "message = " << (int)id << " size = " << (int)size << endl; // load message - char buf[256]; - fread( buf, size, 1, fd ); + ch->read( savebuf, size ); // read checksum - int cksum0 = fgetc( fd ); - int cksum1 = fgetc( fd ); + ch->read( tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0]; + ch->read( tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0]; - if ( validate_cksum( id, size, buf, cksum0, cksum1 ) ) { - parse_msg( id, buf, pos, att ); + if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) { + parse_msg( id, savebuf, pos, att ); return id; } diff --git a/utils/GPSsmooth/MIDG-II.hxx b/utils/GPSsmooth/MIDG-II.hxx index b1f216c75..c9ebcfdac 100644 --- a/utils/GPSsmooth/MIDG-II.hxx +++ b/utils/GPSsmooth/MIDG-II.hxx @@ -8,8 +8,6 @@ #include -#include - #include #include #include @@ -25,6 +23,7 @@ typedef unsigned int uint32_t; # error "Port me! Platforms that don't have need to define int8_t, et. al." #endif +#include SG_USING_STD(cout); SG_USING_STD(endl); @@ -130,7 +129,6 @@ private: vector pos_data; vector att_data; - FILE *fd; // parse message and put current data into vector if message has a // newer time stamp than existing data. @@ -143,7 +141,7 @@ public: // read/parse the next message from the specified data stream, // returns id # if a valid message found. - int next_message( FILE *fd, MIDGpos *pos, MIDGatt *att ); + int next_message( SGIOChannel *ch, MIDGpos *pos, MIDGatt *att ); // load the named file into internal buffers bool load( const string &file ); diff --git a/utils/GPSsmooth/Makefile.am b/utils/GPSsmooth/Makefile.am index 396245e94..ae795de9e 100644 --- a/utils/GPSsmooth/Makefile.am +++ b/utils/GPSsmooth/Makefile.am @@ -13,7 +13,7 @@ MIDGsmooth_SOURCES = \ MIDG_main.cxx MIDGsmooth_LDADD = \ - -lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \ + -lsgio -lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \ $(joystick_LIBS) $(network_LIBS) $(base_LIBS) -lz INCLUDES = -I$(top_srcdir)/src