通信数据格式解析

最近做的一点工作。

基本原理是客户端与服务器一对一通信。客户端作为接收方解析数据,服务器端作为发送端,封包数据

基本封包格式如下:

通信格式说明

101,1,6.8,3, 4494.99,0.264894,0.753365,0.798609,0.395870,0.612474,0.703561,2

101起始位

1设备正常工作与否 (0不正常 1正常)

6.8每段代表的长度

3总的段数

4494.99 实际开始的位置

0.264894,0.753365第1段的特征值1和特征值2

0.798609,0.395870第2段的特征值1和特征值2

0.612474,0.703561第3段的特征值1和特征值2

2当前第2段报警0无报警,非0报警段数

 

解析Demo:

 

 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 /************************************************************************/
 6 /* 
 7     解析出的长度存放在:  PointsLen
 8     解析出来的Phi数据存放在:PhiArray
 9     解析出来的P数据存放在:PArray
10     解析出来的报警位置数据存放在:AlarmArray
11 */
12 /************************************************************************/
13 
14 
15 int main()
16 {
17     //string RecvStr="101,1,6.810597,293,4495,0.000052,0.000025,0.000069,0.000038,0.000006,0.000016,0.000004,0.000003,0.000006,0.000009,0.000009,0.000005,0.000008,0.000003,0.000028,0.000013,0.000075,0.000005,0.000007,0.000003,0.000007,0.000004,0.000012,0.000010,0.000005,0.000012,0.000021,0.000005,0.000004,0.000026,0.000009,0.000007,0.000002,0.000051,0.000005,0.000004,0.000009,0.000007,0.000001,0.000003,0.000002,0.000004,0.000002,0.000002,0.000005,0.000008,0.000686,0.000074,0.000005,0.000126,0.000041,0.000119,0.000015,0.000003,0.000032,0.000010,0.000030,0.000032,0.000006,0.000009,0.000042,0.000085,0.000004,0.000034,0.000028,0.000030,0.000043,0.000136,0.000012,0.000085,0.000030,0.000022,0.000006,0.000013,0.000075,0.000005,0.000258,0.000003,0.000876,0.000454,0.000010,0.000011,0.000009,0.000005,0.000019,0.000004,0.000006,0.000007,0.000004,0.000007,0.000007,0.000005,0.000003,0.000008,0.000027,0.000012,0.000038,0.000006,0.000006,0.000008,0.000018,0.000006,0.000025,0.000010,0.000014,0.000037,0.000091,0.000021,0.000036,0.000005,0.000008,0.000013,0.000002,0.000040,0.000010,0.000003,0.000009,0.000031,0.000014,0.000004,0.000007,0.000160,0.000235,0.000443,0.000462,0.000682,0.004006,0.010638,0.002449,0.000038,0.000007,0.000058,0.000007,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000035,0.000039,0.000045,0.000045,0.000041,0.000040,0.000039,0.000040,0.000039,0.000041,0.000038,0.000033,0.000023,0.000018,0.000012,0.000010,0.000010,0.000011,0.000010,0.000005,0.000008,0.000011,0.000018,0.000027,0.000027,0.000018,0.000012,0.000006,0.000005,0.000005,0.000005,0.000005,0.000007,0.000005,0.000009,0.000010,0.000006,0.000004,0.000004,0.000005,0.000005,0.000007,0.000006,0.000004,0.000002,0.000001,0.000001,0.000002,0.000004,0.000005,0.000007,0.000007,0.000010,0.000012,0.000009,0.000008,0.000006,0.000006,0.000005,0.000006,0.000005,0.000006,0.000009,0.000014,0.000014,0.000010,0.000004,0.000003,0.000003,0.000002,0.000002,0.000003,0.000004,0.000006,0.000005,0.000003,0.000002,0.000002,0.000001,0.000001,0.000005,0.000003,0.000003,0.000003,0.000004,0.000005,0.000006,0.000007,0.000007,0.000005,0.000003,0.000003,0.000003,0.000004,0.000007,0.000010,0.000012,0.000022,0.000029,0.000029,0.000020,0.000017,0.000012,0.000010,0.000014,0.000017,0.000019,0.000020,0.000013,0.000012,0.000012,0.000014,0.000015,0.000014,0.000014,0.000016,0.000022,0.000022,0.000021,0.000017,0.000011,0.000008,0.000012,0.000015,0.000011,0.000045,0.000171,0.000432,0.000686,0.000933,0.001000,0.000926,0.000819,0.000700,0.000574,0.000463,0.000379,0.000303,0.000245,0.000208,0.000165,0.000133,0.000110,0.000093,0.000077,0.000065,0.000054,0.000045,0.000037,0.000030,0.000025,0.000022,0.000023,0.000019,0.000018,0.000015,0.000010,0.000008,0.000008,0.000007,0.000007,0.000006,0.000005,0.000004,0.000004,0.000004,0.000003,0.000002,0.000003,0.000002,0.000002,0.000001,0.000002,0.000002,0.000002,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000000,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000002,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000000,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000000,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000000,0.000001,0.000001,0.000001,0.000001,0.000000,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000001,0.000000,0.000031,0.000021,5.000000,126.0000000,74.0000000";
18     string RecvStr="101,1,6.810597,4,4495,0.00001,0.00002,0.00003,0.00004,0.00005,0.00006,0.00007,0.00008,5.000,7.000,9.000";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
19     RecvStr=RecvStr.substr(15,RecvStr.length()-15);
20     string Points=RecvStr.substr(0,RecvStr.find(','));
21     int PointsLen=atoi(Points.c_str());
22 
23     cout<<PointsLen<<endl;
24 
25 
26 
27     string RecvStrTemp(RecvStr,RecvStr.find(Points)+Points.length()+1);
28 
29     string BeginPosStr=RecvStrTemp.substr(0,RecvStrTemp.find(','));
30     int BeginPos=atoi(BeginPosStr.c_str());
31 
32     string RecvBufNew(RecvStrTemp,RecvStrTemp.find(BeginPosStr)+BeginPosStr.length()+1);
33     
34 
35     double *PhiArray=new double[PointsLen];
36     double *PArray=new double[PointsLen];
37     
38     int count=0;
39     while(count<PointsLen)
40     {
41         string TempPhiStr=RecvBufNew.substr(0,RecvBufNew.find(','));
42         double ValueOne=atof(TempPhiStr.c_str());
43         
44         PhiArray[count]=ValueOne;
45         cout<<"PhiValue:"<<PhiArray[count]<<endl;
46 
47 
48         
49         RecvBufNew=RecvBufNew.substr(TempPhiStr.length()+1,RecvBufNew.length()-TempPhiStr.length());
50 
51         string TempPstr=RecvBufNew.substr(0,RecvBufNew.find(','));
52         double ValueTwo=atof(TempPstr.c_str());
53 
54         PArray[count]=ValueTwo;
55         cout<<"PValue:"<<PArray[count]<<endl;
56 
57         int a=TempPstr.length()+1;
58         int b=RecvBufNew.length()-TempPstr.length();
59 
60         if(b==0)
61         {
62             delete[] PhiArray;
63             delete[] PArray;
64             system("pause");
65             return 0;
66         }
67 
68         RecvBufNew=RecvBufNew.substr(TempPstr.length()+1,RecvBufNew.length()-TempPstr.length());
69         count++;
70     }
71 
72 
73     int AlarmArray[10]={0};
74     int AlarmCount=0;
75     while(!RecvBufNew.empty())
76     {
77         string TempPhiStr=RecvBufNew.substr(0,RecvBufNew.find(','));
78         AlarmArray[AlarmCount]=(int)atof(TempPhiStr.c_str());
79         cout<<"AlarmPos:"<<AlarmArray[AlarmCount]<<endl;
80         AlarmCount++;
81         int a=TempPhiStr.length()+1;
82         int b=RecvBufNew.length()-TempPhiStr.length();
83 
84         if(b==0)
85             break;
86         RecvBufNew=RecvBufNew.substr(TempPhiStr.length()+1,RecvBufNew.length()-TempPhiStr.length());
87     }
88 
89 
90     delete[] PhiArray;
91     delete[] PArray;
92     system("pause");
93     return 0;
94 }

结果:

posted @ 2015-11-16 12:38  vpoet  阅读(1071)  评论(0编辑  收藏  举报