1
0
Fork 0

Switch over to using SGIOChannel so we can try to read data live from

the serial port at some future time.
This commit is contained in:
curt 2005-09-23 21:23:13 +00:00
parent 4f72904235
commit 2f98caeba8
3 changed files with 30 additions and 26 deletions

View file

@ -3,6 +3,7 @@
#include <plib/ul.h> #include <plib/ul.h>
#include <simgear/constants.h> #include <simgear/constants.h>
#include <simgear/io/sg_file.hxx>
#include <simgear/math/sg_geodesy.hxx> #include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sgstream.hxx> #include <simgear/misc/sgstream.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
@ -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 // load the specified file, return the number of records loaded
bool MIDGTrack::load( const string &file ) { bool MIDGTrack::load( const string &file ) {
int count = 0;
MIDGpos pos; MIDGpos pos;
MIDGatt att; MIDGatt att;
@ -329,16 +331,17 @@ bool MIDGTrack::load( const string &file ) {
pos_data.clear(); pos_data.clear();
att_data.clear(); att_data.clear();
// openg the file // open the file
fd = fopen( file.c_str(), "r" ); SGFile input( file );
if ( !input.open( SG_IO_IN ) ) {
if ( fd == NULL ) {
cout << "Cannot open file: " << file << endl; cout << "Cannot open file: " << file << endl;
return false; return false;
} }
while ( ! feof( fd ) ) { while ( ! input.eof() ) {
int id = next_message( fd, &pos, &att ); // cout << "looking for next message ..." << endl;
int id = next_message( &input, &pos, &att );
count++;
if ( id == 10 ) { if ( id == 10 ) {
if ( att.get_msec() > att_time ) { if ( att.get_msec() > att_time ) {
@ -357,38 +360,41 @@ bool MIDGTrack::load( const string &file ) {
} }
} }
cout << "processed " << count << " messages" << endl;
return true; return true;
} }
// load the next message of a real time data stream // 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 // scan for sync characters
int sync0, sync1; uint8_t sync0, sync1;
sync0 = fgetc( fd ); ch->read( tmpbuf, 1 ); sync0 = (unsigned char)tmpbuf[0];
sync1 = fgetc( fd ); ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
while ( (sync0 != 129 || sync1 != 161) && !feof(fd) ) { while ( (sync0 != 129 || sync1 != 161) && !ch->eof() ) {
sync0 = sync1; sync0 = sync1;
sync1 = fgetc( fd ); ch->read( tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
} }
// cout << "start of message ..." << endl; // cout << "start of message ..." << endl;
// read message id and size // read message id and size
int id = fgetc( fd ); ch->read( tmpbuf, 1 ); uint8_t id = (unsigned char)tmpbuf[0];
int size = fgetc( fd ); ch->read( tmpbuf, 1 ); uint8_t size = (unsigned char)tmpbuf[0];
// cout << "message = " << id << " size = " << size << endl; // cout << "message = " << (int)id << " size = " << (int)size << endl;
// load message // load message
char buf[256]; ch->read( savebuf, size );
fread( buf, size, 1, fd );
// read checksum // read checksum
int cksum0 = fgetc( fd ); ch->read( tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0];
int cksum1 = fgetc( fd ); ch->read( tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0];
if ( validate_cksum( id, size, buf, cksum0, cksum1 ) ) { if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
parse_msg( id, buf, pos, att ); parse_msg( id, savebuf, pos, att );
return id; return id;
} }

View file

@ -8,8 +8,6 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <stdio.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -25,6 +23,7 @@ typedef unsigned int uint32_t;
# error "Port me! Platforms that don't have <stdint.h> need to define int8_t, et. al." # error "Port me! Platforms that don't have <stdint.h> need to define int8_t, et. al."
#endif #endif
#include <simgear/io/iochannel.hxx>
SG_USING_STD(cout); SG_USING_STD(cout);
SG_USING_STD(endl); SG_USING_STD(endl);
@ -130,7 +129,6 @@ private:
vector <MIDGpos> pos_data; vector <MIDGpos> pos_data;
vector <MIDGatt> att_data; vector <MIDGatt> att_data;
FILE *fd;
// parse message and put current data into vector if message has a // parse message and put current data into vector if message has a
// newer time stamp than existing data. // newer time stamp than existing data.
@ -143,7 +141,7 @@ public:
// read/parse the next message from the specified data stream, // read/parse the next message from the specified data stream,
// returns id # if a valid message found. // 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 // load the named file into internal buffers
bool load( const string &file ); bool load( const string &file );

View file

@ -13,7 +13,7 @@ MIDGsmooth_SOURCES = \
MIDG_main.cxx MIDG_main.cxx
MIDGsmooth_LDADD = \ MIDGsmooth_LDADD = \
-lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \ -lsgio -lsgtiming -lsgmath -lsgmisc -lsgdebug -lplibnet -lplibul \
$(joystick_LIBS) $(network_LIBS) $(base_LIBS) -lz $(joystick_LIBS) $(network_LIBS) $(base_LIBS) -lz
INCLUDES = -I$(top_srcdir)/src INCLUDES = -I$(top_srcdir)/src