Search code examples
c++parsingserial-portdata-stream

how to parse stream data(string) to different data files


@everyone, I have some problem in reading data form IMU recently. Below is the data I got from My device, it is ASCII, all are chars,and my data size is [122], which is really big, I need convert them to short, and then float, but I dont know why and how.....

unsigned char data[33];
short x,y,z;
float x_fl,y_fl,z_fl,t_fl;
float bias[3]={0,0,0};//array initialization
unsigned char sum_data=0;
int batch=0;
if ( !PurgeComm(file,PURGE_RXCLEAR ))
    cout << "Clearing RX Buffer Error" << endl;//this if two sentence aim to clean the buffer

//---------------- read data from IMU ----------------------
do { ReadFile(file,&data_check,1,&read,NULL);
       //if ((data_check==0x026))
       { ReadFile(file,&data,33,&read,NULL); }

       ///  Wx Values               
       {
          x=(data[8]<<8)+data[9];                               
          x_fl=(float)6.8664e-3*x;                                                  
          bias[0]+=(float)x_fl;                                               
        }

        ///  Wy Values                                      
        {
          y=(data[10]<<8)+data[11];
          y_fl=(float)6.8664e-3*y;
          bias[1]+=(float)y_fl;                                                 
        }

        ///  Wz Values                      
        {
          z=(data[12]<<8)+data[13];
          z_fl=(float)6.8664e-3*z; 
          bias[2]+=(float)z_fl;                                                 
        }

        batch++;

}while(batch<NUM_BATCH_BIAS);

$VNYMR,+049.320,-017.922,-024.946,+00.2829,-00.2734,+00.2735,-02.961,+03.858,-08.325,-00.001267,+00.000213,-00.001214*64

$VNYMR,+049.322,-017.922,-024.948,+00.2829,-00.2714,+00.2735,-02.958,+03.870,-08.323,+00.004923,-00.000783,+00.000290*65

$VNYMR,+049.321,-017.922,-024.949,+00.2821,-00.2655,+00.2724,-02.984,+03.883,-08.321,+00.000648,-00.000391,-00.000485*61

$VNYMR,+049.320,-017.922,-024.947,+00.2830,-00.2665,+00.2756,-02.983,+03.874,-08.347,-00.003416,+00.000437,+00.000252*6C

$VNYMR,+049.323,-017.921,-024.947,+00.2837,-00.2773,+00.2714,-02.955,+03.880,-08.326,+00.002570,-00.001066,+00.000690*67

$VNYMR,+049.325,-017.922,-024.948,+00.2847,-00.2715,+00.2692,-02.944,+03.875,-08.344,-00.002550,+00.000638,+00.000022*6A

$VNYMR,+049.326,-017.921,-024.945,+00.2848,-00.2666,+00.2713,-02.959,+03.876,-08.309,+00.002084,+00.000449,+00.000667*6A

all I want to do is:

  1. extract last 6 numbers separated by commas, btw, I don't need the last 3 chars(like *66).
  2. Save the extracted data to 6 .dat files.

What is the best way to do this?

Since I got this raw data from IMU, and I need the last 6 data, which are accelerations(x,y,z) and gyros(x,y,z).

  1. If someone could tell me how to set a counter to the end of each data stream, that will be perfect, because I need the time stamp of IMU also.

Last word is I am doing data acquisition under windows, c++.

Hope someone could help me, I am freaking out because of so much things to do and that's really annoying!!


Solution

  • There's a whole family of scanf functions (fscanf, sscanf and some "secure" ones). Assuming you have read a line into a string:-

    sscanf( s, "VNYMR,%*f,%*f,%*f,%*f,%*f,%*f,%f,%f,%f,%f,%f,%f", &accX, &accY, &accZ, &gyroX, &gyroY, &gyroZ )
    

    And assuming I have counted correctly! This will verify that the literal $VNYMR is there, followed by about five floats that you don't assign and finally the six that you care about. &accaX, etc are the addresses of your floats. Test the result - the number of assignments made..