2

我有如下信息。我必须解析我将从机器获得的数据部分并填充诸如冲洗模式、机器状态等值。我将在 Com 端口上获取数据,我必须在 C++ 中完成。我已经编写了打开 com 端口并发送数据需求(命令)的代码。

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  

这是我提出的解决方案:-

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--;

因为在我不熟悉 C 和 C++ 之前我还没有完成这项工作,所以我把这段代码放在这里是因为我只有 2 次机会在机器上运行代码,所以最好让有经验的人检查一下。谢谢

4

0 回答 0