2

i have the information as shown below. I have to parse the DATA PART that i will get from a machine and fill the values like Rinse mode,status of machine.... I will get data on Com Port,i have to do it in c++. I have written the code to open the com port and send Data demand(command).

Data format
・ All data is ASCII character code without check sum and CR.
・ Range of Machine Number start from 0000 to 9999
・ If there is no top digit at data part, it will be zero suppression and put space to fill it.
・ Calculate sum total from top till before check sum (1bite unit), then recognize this
complement of 1 as check sum.
・ Finish of format is CR (Hexadecimal: OD)


Data demand(command)
   CM”      “00”         Checksum       CR
(2 bite)   (2 bite)



Monitor data(response)
“RE”              “00”          MachineNumber    Data part
(2 bite)         (2 bite)         4bite            128 byte




Parameter              Data          byte               Unit              Status
Status of machine       #             1
Rinsemode               #             1
Bypass                  #             1
Dialysate flow         ###            3                      
Treated blood volume  ##.##           5  
   .                   ...           ...
   .                   ...           ... 
   .                   ...           ... 
W+B conductivity       ##.#           4  

This is the solution i made:-

char data[6]="CM00";
struct parse_data{


char Response[2];
char oo[2];
char mc_no[4];
char data_part[128];
char checksum;
char cr;
};
struct stored_parse_data{
char    Status_ofmachine;
char    Rinsemode;
char    Bypass;
int Dialysate_flow;
int Blood_pump_flow;
float  Treated_blood_volume;
float   W_B_conductivity;
//HERE More fields are also there but for making code less i am showing only these fields

};

int main()
{
int i, n,
      cport_nr=0,        /* /dev/ttyS0 (COM1 on windows) */
      bdrate=9600;       /* 9600 baud */


  unsigned char buf[4096];
  int m;
  for(int k=0;k<=10;k++)
  {
  if(OpenComport(cport_nr, bdrate))
  {
      char buf[30];
      //sprintf(buf,"Can not open comport %d\n",cport_nr);
    //  wLog->WriteDebugLog(buf);
    printf("Can not open comport\n");
    cport_nr++;
    continue;
    return(0);
  }

char *b;
 char buf_temp[10];
 int chkvalue=calculatechecksum();
 b = itoa(chkvalue,buf_temp,10);


    data[4]= ~(*b);
 data[5]=0x0D;

 if(SendBuf1(cport_nr,data,6) > -1)
 {
     //wLog->WriteDebugLog("Data Sent Successfully\n");
     printf("Send succesfuly\n");
 }
 else
 {
     printf("\nn\failed\n\n");
     //wLog->WriteErrorLog("failed in sending data\n");

 }
 while(1)
  {
    n = PollComport(cport_nr, buf, 4095);


    if(n > 0)
    {
      buf[n] = 0;   /* always put a "null" at the end of a string! */


      for(i=0; i < n; i++)
      {
        if(buf[i] < 32)  /* replace unreadable control-codes by dots */
        {
          buf[i] = '.';
        }
      }
      char buf11[70];


      printf("received %i bytes: %s\n", n, (char *)buf);
      sprintf(buf11,"received %i bytes: %s\n", n, (char *)buf);
      wLog->WriteDebugLog(buf11);
      parse_data curretnt_data;


      if(n==138 || n >138)
        int i=0;

        {
            curretnt_data.Response[0] = buf[i++];
            curretnt_data.Response[1] = buf[i++];
            curretnt_data.oo[0] = buf[i++];
            curretnt_data.oo[1] = buf[i++];

            for(int j =0; j<3; j++)
                curretnt_data.mc_no[j]=buf[i++];

            for(int k =0; k<128; k++)
                curretnt_data.data_part[k] = buf[i++];


            curretnt_data.checksum = buf[i++];
            curretnt_data.cr = buf[i++];
        }

        stored_parse_data fill;

        fill.Status_ofmachine = curretnt_data.data_part[0];
        fill.Rinsemode = curretnt_data.data_part[1];
        fill.Bypass = curretnt_data.data_part[2];


        char temp[3],j=3;
        for(int i =0; i < 3;i++)
            temp[i] = curretnt_data.data_part[j++];


        fill.Dialysate_flow =(int)atoi(temp);
        printf("Dialysate_flow is =%d\n",fill.Dialysate_flow); 
        char buf_d12[500];
        sprintf(buf_d12,"Dialysate_flow is =%d\n",fill.Dialysate_flow); 
        WriteToDataFile(buf_d12);


        j--;
        for(int i =0; i < 3;i++)
            temp[i] = curretnt_data.data_part[j++];


        fill.Blood_pump_flow =(int)atoi(temp);
        printf("Blood_pump_flow is  =%d\n",fill.Blood_pump_flow); 
        char buf_d13[500];
        sprintf(buf_d13,"Blood_pump_flow is  =%d\n",fill.Blood_pump_flow);
        WriteToDataFile(buf_d13);




        char temp_5[5];
        j--;
        for(int i =0; i < 5;i++)
            temp_5[i] = curretnt_data.data_part[j++];
        fill.Treated_blood_volume = (float)atof(temp_5);
        printf("Treated_blood_volume is =%f\n",fill.Treated_blood_volume); 
        char buf_d14[500];
        sprintf(buf_d14,"Treated_blood_volume is =%f\n",fill.Treated_blood_volume); 
        WriteToDataFile(buf_d14);
        j--;




        char temp_4[4];
        for(int i=0;i<4;i++)
            temp_4[i]=curretnt_data.data_part[j++];
        fill.W_B_conductivity=(float)atof(temp_4);
        printf("W_B_conductivity is =%f\n",fill.W_B_conductivity); 
        char buf_d15[500];
        sprintf(buf_d15,"W_B_conductivity is =%f\n",fill.W_B_conductivity);  
        WriteToDataFile(buf_d15);
        j--;

AS I have not done this work before i am new to C and C++,i am putting this code here because i will get only 2 chance to run the code on machine so its better that it may be checked by experience guys. Thanks

user1402643
  • 419
  • 3
  • 14
  • Read the data lines, figure out when you hit the data column. Convert to numerical value. Done. It is all rudimentary stuff you should have done in your computer science courses. – anio Aug 30 '12 at 12:53
  • @LucaMartini Can u now check the solution that i did... – user1402643 Sep 03 '12 at 07:17
  • It's *current*, not *curretnt*. Besides, `atoi` and `printf` is C, not C++. You can use `std::stringstream` and `std::cout` instead. – Bartek Banachewicz Sep 03 '12 at 07:29
  • @BartekBanachewicz ok i will look at that beside that is the code correct? – user1402643 Sep 03 '12 at 07:42
  • It's written in C subset of C++, so I can't really be sure without either careful debugging or thorough review. I would suggest to rewrite as much as possible to C++ standard library, which is much easier to use, and usually much harded to break. – Bartek Banachewicz Sep 03 '12 at 07:49
  • @user1402643 Unfortunately I have no time for a code review at this moment. I warmly suggest to isolate the parsing of the packet into a function that you can test with mock packets. – Luca Martini Sep 03 '12 at 09:55
  • You could try this http://stackoverflow.com/questions/1120140/csv-parser-in-c – axon Sep 24 '12 at 04:59
  • 1
    just beeing curious… why can you run this code only twice? – rhavin Nov 26 '12 at 02:24

0 Answers0