HumitureController.cs
15.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using OnlineStore.Common;
using log4net;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 壁挂王字壳温湿度变送器(485型)
/// </summary>
public class HumitureController
{
//public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static int bautRate = 4800;//波特率
private static Parity parity = Parity.None;//校验位
private static int dataBits = 8;//数据位
private static StopBits stopBits = StopBits.One; //停止位
private static string LogName = "";
private static Dictionary<string, AcSerialBean> serialBeanMap = new Dictionary<string, AcSerialBean>();
public static int HumitureControllerType = ConfigAppSettings.GetIntValue(Setting_Init.HumitureControllerType);
public static bool Init(string port)
{
if (serialBeanMap.ContainsKey(port))
{
return true;
}
LogName = "温湿度传感器[" + port + "]";
AcSerialBean sb = null;
if (HumitureControllerType.Equals(1))
{
bautRate = 9600;//波特率
}
sb = new AcSerialBean(port, bautRate, parity, dataBits, stopBits);
try
{
if (sb.openPort())
{
serialBeanMap.Add(port, sb);
var ht = HumitureController.QueryData(port);
LogUtil.info(LogName + "串口" + port + "打开成功!温度:"+ ht.Temperate+",湿度:"+ht.Humidity);
return true;
}
else
{
LogUtil.error( LogName + "串口" + port + "打开失败!");
return false;
}
}
catch (Exception ex)
{
LogUtil.error("串口" + port + "打开失败:" + ex.ToString());
}
return true;
}
/// <summary>
/// 释放资源
/// </summary>
public static void CloseAllPort()
{
List<string> kes = new List<string>(serialBeanMap.Keys);
foreach (string key in kes)
{
ClosePort(key);
}
}
private static void ClosePort(string portName)
{
AcSerialBean bean = GetSerialBean(portName);
if (bean == null)
{
LogUtil.info("串口【" + portName + "】未打开,不需要关闭");
return;
}
//清理缓存
bean.clearInBuffer();
bean.clearOutBuffer();
bean.closePort();
if (serialBeanMap.ContainsKey(portName))
{
serialBeanMap.Remove(portName);
}
LogUtil.info("温湿度控制器 关闭串口【" + portName + "】 ");
}
private static AcSerialBean GetSerialBean(string portName)
{
if (serialBeanMap.ContainsKey(portName))
{
return serialBeanMap[portName];
}
return null;
}
// public static ASTemperateParam LastData = new ASTemperateParam(0, 0);
public static HumitureParam QueryData(string port)
{
HumitureParam param = new HumitureParam(0, 0,100);
param.DeviceAddress = port;
List<double> data = queryData(port );
if (data.Count.Equals(3))
{
//double humidityadjust = (double)ConfigAppSettings.GetNumValue(Setting_Init.Device_HumidityAdjust);
//double humiditylimited = (double)ConfigAppSettings.GetNumValue(Setting_Init.Device_HumidityLimited);
//if (humidityadjust != 0 && humiditylimited != 0)
//{
// if (data[0] + humidityadjust < humiditylimited)
// {
// data[0] = humiditylimited;
// }
// else
// data[0] += humidityadjust;
//}
data[0] = ProcessHumity(data[0]);
//double tempadjust = (double)ConfigAppSettings.GetNumValue(Setting_Init.Device_TemptureAdjust);
//double templimited = (double)ConfigAppSettings.GetNumValue(Setting_Init.Device_TemptureLimited);
//if (tempadjust != 0 && templimited != 0)
//{
// if (data[1] + tempadjust < templimited)
// {
// data[1] = templimited;
// }
// else
// data[1] += tempadjust;
//}
data[1] = ProcessTemp(data[1]);
param = new HumitureParam(data[1], data[0], data[2]);
}
return param;
}
/// <summary>
/// 温度数据处理
/// </summary>
/// <param name="nowTemp"></param>
/// <returns></returns>
private static double ProcessTemp(double nowTemp)
{
double temp = nowTemp;
double minVal = 5;//ConfigHelper.Config.Get("TempThreshold_Min", 5);
double midVal = 14;// ConfigHelper.Config.Get("TempThreshold_Mid", 14);
double maxVal = 20;// ConfigHelper.Config.Get("TempThreshold_Max", 20);
//温度计算
if (nowTemp <= minVal)
{
return temp;
}
else if (nowTemp <= midVal)
{
temp = (nowTemp - minVal) * 0.6 + minVal;
}
else if (nowTemp < maxVal)
{
temp = (nowTemp - 11) * (nowTemp - 10) / 10 + 11;
}
return Math.Round(temp, 1);
}
/// <summary>
/// 湿度数据处理
/// </summary>
/// <param name="nowHumity"></param>
/// <returns></returns>
private static double ProcessHumity(double nowHumity)
{
double temp = nowHumity;
double minVal = ConfigHelper.Config.Get("HumidityThreshold_Min", 5);
double maxVal = ConfigHelper.Config.Get("HumidityThreshold_Max", 17);
double coefVal = ConfigHelper.Config.Get("HumidityThreshold_Coef", 0.4);
//湿度计算
if (nowHumity <= minVal)
{
return temp;
}
else if (nowHumity <= maxVal)
{
temp = (nowHumity - minVal) * coefVal + minVal;
}
return Math.Round(temp, 1);
}
/// <summary>
/// 返回温度和湿度
/// </summary>
/// <returns></returns>
private static List<double> queryData(string port)
{
// 温度计算:
//当温度低于 0 ℃ 时温度数据以补码的形式上传。
//温度:FF9B H(十六进制)= -101 => 温度 = -10.1℃
//湿度计算:
//湿度:292 H(十六进制) = 658 => 湿度 = 65.8 % RH
AcSerialBean sb = GetSerialBean(port);
if (sb == null)
{
return new List<double>();
}
byte[] sendData = new byte[8];
sendData[0] = 0x01;
if (HumitureControllerType.Equals(1))
{
sendData[1] = 0x04;
}
else
{
sendData[1] = 0x03;
}
sendData[2] = 0x00;
sendData[3] = 0x00;
sendData[4] = 0x00;
sendData[5] = 0x03;
sendData[6] = 0x00;
sendData[7] = 0x00;
sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData);
//LogUtil.debug("温湿度控制器发送数据:" + str);
byte[] reviceData = new byte[9];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
return getReviceData(reviceData);
}
public static int QueryHistoryCount(string port)
{
AcSerialBean sb = GetSerialBean(port);
if (sb == null|| HumitureControllerType.Equals(2).Equals(false))
{
return -1;
}
byte[] sendData = new byte[8];
sendData[0] = 0x01;
sendData[1] = 0x04;
sendData[2] = 0x20;
sendData[3] = 0x00;
byte[] addrByte = AcSerialBean.StringToByte("2000");
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
sendData[4] = 0x00;
sendData[5] = 0x02;
sendData[6] = 0x00;
sendData[7] = 0x00;
sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData);
// LogUtil.debug("温湿度控制器发送数据:" + str);
byte[] reviceData = new byte[9];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
return getReviceIntData(reviceData);
}
public static int QueryCurrCount(string port)
{
AcSerialBean sb = GetSerialBean(port);
if (sb == null || HumitureControllerType.Equals(2).Equals(false))
{
return -1;
}
byte[] sendData = new byte[8];
sendData[0] = 0x01;
sendData[1] = 0x04;
sendData[2] = 0x20;
sendData[3] = 0x02;
sendData[4] = 0x00;
sendData[5] = 0x02;
sendData[6] = 0x00;
sendData[7] = 0x00;
sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData);
//LogUtil.debug("温湿度控制器发送数据:" + str);
byte[] reviceData = new byte[9];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
return getReviceIntData(reviceData);
}
public static List<object> QueryHistory(string port)
{
List<object> list = new List<object>();
AcSerialBean sb = GetSerialBean(port);
if (sb == null || HumitureControllerType.Equals(2).Equals(false))
{
return list;
}
byte[] sendData = new byte[8];
sendData[0] = 0x01;
sendData[1] = 0x04;
sendData[2] = 0x20;
sendData[3] = 0x04;
sendData[4] = 0x00;
sendData[5] = 0x05;
sendData[6] = 0x00;
sendData[7] = 0x00;
sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData);
//LogUtil.debug("温湿度控制器发送数据:" + str);
byte[] reviceData = new byte[15];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
try
{
if (reviceData != null && reviceData.Length >= 9)
{
string temp = String.Format("{0:X2}", reviceData[3]) + String.Format("{0:X2}", reviceData[4]);
string hum = String.Format("{0:X2}", reviceData[5]) + String.Format("{0:X2}", reviceData[6]);
double tempV = (double)Convert.ToInt32(temp, 16) / 10;
double humV = (double)Convert.ToInt32(hum, 16) / 10;
int year = Convert.ToInt32(String.Format("{0:X2}", reviceData[7]), 16);
int mouth = Convert.ToInt32(String.Format("{0:X2}", reviceData[8]), 16);
int day = Convert.ToInt32(String.Format("{0:X2}", reviceData[9]), 16);
int hour = Convert.ToInt32(String.Format("{0:X2}", reviceData[10]), 16);
int minute = Convert.ToInt32(String.Format("{0:X2}", reviceData[11]), 16);
int second = Convert.ToInt32(String.Format("{0:X2}", reviceData[12]), 16);
DateTime time = new DateTime(year, mouth, day, hour, minute, second);
list.Add(tempV);
list.Add(humV);
list.Add(time);
}
}
catch (Exception ex)
{
LogUtil.info(LogName + "转换出错:" + ex.ToString());
}
return list;
}
private static byte[] buildCheckData(byte[] sendData, int length)
{
ushort pChecksum = 0;
AcSerialBean.CalculateCRC(sendData, length, out pChecksum);
string checkStr = Convert.ToString(pChecksum, 16);
byte[] checkByte = AcSerialBean.StringToByte(checkStr);
if (checkByte.Length == 1)
{
sendData[length] = checkByte[0];
sendData[length + 1] = 0x00;
}
else
{
sendData[length + 1] = checkByte[0];
sendData[length] = checkByte[1];
}
return sendData;
}
private static int getReviceIntData(byte[] dataArray)
{
List<double> list = new List<double>();
try
{
if (dataArray == null)
{
return -1;
}
if (dataArray.Length >= 9)
{
string data = String.Format("{0:X2}", dataArray[5]) + String.Format("{0:X2}", dataArray[6]+String.Format("{0:X2}", dataArray[3]) + String.Format("{0:X2}", dataArray[4]) );
int tempV = Convert.ToInt32(data, 16);
return tempV;
}
}
catch (Exception ex)
{
LogUtil.info(LogName + "转换出错:" + ex.ToString());
}
return -1;
}
private static List<double> getReviceData(byte[] dataArray)
{
List<double> list = new List<double>();
try
{
if (dataArray == null)
{
return list;
}
if (dataArray.Length >= 9)
{
string temp = String.Format("{0:X2}", dataArray[3]) + String.Format("{0:X2}", dataArray[4]);
string hum = String.Format("{0:X2}", dataArray[5]) + String.Format("{0:X2}", dataArray[6]);
string Oxygen = String.Format("{0:X2}", dataArray[7]) + String.Format("{0:X2}", dataArray[8]);
double tempV = (double)Convert.ToInt32(temp, 16) / 10;
double humV = (double)Convert.ToInt32(hum, 16) / 10;
double OxygenV = (double)Convert.ToInt32(Oxygen, 16) / 10;
list.Add(tempV);
list.Add(humV);
list.Add(OxygenV);
}
}
catch (Exception ex)
{
LogUtil.info(LogName + "转换出错:" + ex.ToString());
}
return list;
}
}
}