ningshuxia
2022-11-18 b4093bc3cbe7e1c347bd9d366528a74b8a18f570
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingSocket.helper
{
    /// <summary>
    /// 作废
    /// </summary>
    public class MsgHelper
    {
        public bool Msg(string msg)
        {
 
            string strMsg = "41 54 2B 44 61 74 61 3D 0F A1 9B 98 00 49 C6 33 01 64 13 01 00 02 AB 5C 42 11 7B B5 3E 56 FF 5D 3E E5 58 02 00 00 00 00 00 1F 02 00 00 05 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 75";
            string error_info = null;
            var byteMessage = StringToHexValuve(strMsg, out error_info);
            if (byteMessage == null)
            {
                return false;
            }
 
            AnaMsg(byteMessage.ToArray());
 
            return true;
        }
 
 
        private void AnaMsg(byte[] byteMessage)
        {
 
            if (byteMessage == null)
                return;
            if (byteMessage.Length < 70)
                return;
            var list = new List<Model.MonitorDataDockingReceiveRecord>();
 
            string msg;
            var now = DateTime.Now;
            //包头
            byte[] byte_start = new byte[8];
            Array.Copy(byteMessage, 0, byte_start, 0, 8);
            var str_start = Encoding.ASCII.GetString(byte_start);
            if (str_start != "AT+Data=")
                return;
            if (byteMessage[8] != 0x0F)
                return;
 
            //GPRS模块识别码
            byte[] byte_data1 = new byte[4];
            Array.Copy(byteMessage, 9, byte_data1, 0, 4);
            var int_data1 = bytesToInt4(byte_data1.Reverse().ToArray(), 0);
 
            //数据读取日期
            byte[] byte_data2 = new byte[4];
            Array.Copy(byteMessage, 13, byte_data2, 0, 4);
            var int_data2 = bytesToInt4(byte_data2.Reverse().ToArray(), 0);
 
            //数据读取时间
            byte[] byte_data3 = new byte[4];
            Array.Copy(byteMessage, 17, byte_data3, 0, 4);
            var int_data3 = bytesToInt4(byte_data3.Reverse().ToArray(), 0);
 
            //瞬时流量,m3/h
            byte[] byte_data4 = new byte[4];
            Array.Copy(byteMessage, 21, byte_data4, 0, 4);
            var int_data4 = GetDouble1(byte_data4.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1590274355240243200,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data4.ToString()
            });
 
            //瞬时压力,Mpa
            byte[] byte_data5 = new byte[4];
            Array.Copy(byteMessage, 25, byte_data5, 0, 4);
            var int_data5 = GetDouble1(byte_data5.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589903942416994304,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data5.ToString()
            });
 
            //瞬时流速,m3/h
            byte[] byte_data6 = new byte[4];
            Array.Copy(byteMessage, 29, byte_data6, 0, 4);
            var int_data6 = GetDouble1(byte_data6.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589904380742733824,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data6.ToString()
            });
 
            //正向累计整数
            byte[] byte_data7 = new byte[4];
            Array.Copy(byteMessage, 33, byte_data7, 0, 4);
            var int_data7 = GetDouble1(byte_data7.Reverse().ToArray(), 0);
 
            //反向累计整数
            byte[] byte_data8 = new byte[4];
            Array.Copy(byteMessage, 37, byte_data8, 0, 4);
            var int_data8 = GetDouble1(byte_data8.Reverse().ToArray(), 0);
 
            //正向累计的小数/1000
            byte[] byte_data9 = new byte[2];
            Array.Copy(byteMessage, 41, byte_data9, 0, 2);
            var int_data9 = GetDouble1(byte_data9.Reverse().ToArray(), 0);
 
            //添加小数
            int_data7 += int_data9;
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589903863446638592,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data7.ToString()
            });
 
            //反向累计的小数/1000
            byte[] byte_data10 = new byte[2];
            Array.Copy(byteMessage, 43, byte_data10, 0, 2);
            var int_data10 = GetDouble1(byte_data10.Reverse().ToArray(), 0);
 
            //添加小数
            int_data8 += int_data10;
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589904635718668288,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data8.ToString()
            });
 
            //水表电池电量
            byte[] byte_data11 = new byte[1];
            Array.Copy(byteMessage, 45, byte_data11, 0, 1);
            var int_data11 = GetDouble1(byte_data11.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906693322575872,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data11.ToString()
            });
 
            //GPRS电池电量
            byte[] byte_data12 = new byte[1];
            Array.Copy(byteMessage, 46, byte_data12, 0, 1);
            var int_data12 = GetDouble1(byte_data12.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906830698614784,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data12.ToString()
            });
 
            //系统报警
            byte[] byte_data13 = new byte[1];
            Array.Copy(byteMessage, 47, byte_data13, 0, 1);
            var int_data13 = GetDouble1(byte_data13.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589905499946618880,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data13.ToString()
            });
 
            //水表空管报警
            byte[] byte_data14 = new byte[1];
            Array.Copy(byteMessage, 48, byte_data14, 0, 1);
            var int_data14 = GetDouble1(byte_data14.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589905855032201216,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data14.ToString()
            });
 
            //断励磁报警
            byte[] byte_data15 = new byte[1];
            Array.Copy(byteMessage, 49, byte_data15, 0, 1);
            var int_data15 = GetDouble1(byte_data15.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589905963715006464,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data15.ToString()
            });
 
            //流量上线报警
            byte[] byte_data16 = new byte[1];
            Array.Copy(byteMessage, 50, byte_data16, 0, 1);
            var int_data16 = GetDouble1(byte_data16.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906172004143104,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data16.ToString()
            });
 
            //流量下线报警
            byte[] byte_data17 = new byte[1];
            Array.Copy(byteMessage, 51, byte_data17, 0, 1);
            var int_data17 = GetDouble1(byte_data17.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906236487372800,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data17.ToString()
            });
 
            //压力上限报警
            byte[] byte_data18 = new byte[1];
            Array.Copy(byteMessage, 52, byte_data18, 0, 1);
            var int_data18 = GetDouble1(byte_data18.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906299754254336,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data18.ToString()
            });
 
            //压力下限报警
            byte[] byte_data19 = new byte[1];
            Array.Copy(byteMessage, 53, byte_data19, 0, 1);
            var int_data19 = GetDouble1(byte_data19.Reverse().ToArray(), 0);
            list.Add(new Model.MonitorDataDockingReceiveRecord()
            {
                SysId = 1589906389663354880,
                RecordType = Model.eMonitorType.General,
                SrcTime = now,
                SrcValue = int_data19.ToString()
            });
        }
 
        public static int bytesToInt4(byte[] src, int offset = 0)
        {
            int value;
            value = (src[offset] & 0xFF) << 24
                    | (src[offset + 1] & 0xFF) << 16
                    | (src[offset + 2] & 0xFF) << 8
                    | src[offset + 3] & 0xFF;
            return value;
        }
 
        private double GetDouble1(byte[] value, int offset = 0)
        {
 
            //return BitConverter.ToDouble(byteMessage, startPosition);
            return BitConverter.ToSingle(value.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
        }
 
        /// <summary>
        /// 字符串转16进制字符2
        /// </summary>
        /// <param name="content">字符串</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        private List<byte> StringToHexValuve(string content, out string error_info)
        {
            //去掉空格
            string[] arr = content.Split(' ');
            if (arr.Length < 3)
            {
                arr = content.Split('-');
                if (arr.Length < 3)
                {
                    error_info = "字符无法解析";
                    return null;
                }
            }
            List<byte> result = new List<byte>();
            for (int i = 0; i < arr.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(arr[i]))
                    continue;
 
                var dddd = Convert.ToByte(arr[i], 16);
                result.Add(dddd);
            }
            error_info = null;
            return result;
        }
 
 
    }
}