HumitureController.cs
9.8 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
using log4net;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Timers;
namespace OnlineStore.Common
{
/// <summary>
/// 壁挂王字壳温湿度变送器(485型)
/// </summary>
public class HumitureController
{
public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public bool IsRun = false;
public string serialPort = "";
private int bautRate = 4800;//波特率
private Parity parity = Parity.None;//校验位
private int dataBits = 8;//数据位
private StopBits stopBits = StopBits.One; //停止位
private AcSerialBean sb = null;
private string LogName = "";
public int HumitureControllerType = 0;// ConfigAppSettings.GetIntValue(Setting_Init.HumitureControllerType);
Timer timer = new Timer();
public bool Init(string port)
{
if (IsRun && port.Equals(serialPort))
{
return true;
}
else if (IsRun)
{
Release();
}
LogName = "温湿度传感器[" + port + "]";
if (sb == null)
{
serialPort = port;
if (HumitureControllerType.Equals(1))
{
bautRate = 9600;//波特率
}
sb = new AcSerialBean(serialPort, bautRate, parity, dataBits, stopBits);
}
try
{
if (sb.openPort())
{
IsRun = true;
timer.Interval = 5000;
timer.Elapsed += Timer_Elapsed;
timer.Start();
return true;
}
else
{
LogUtil.error(LogName + "串口打开失败!");
IsRun = false;
return false;
}
}
catch (Exception ex)
{
LogUtil.error(LogName + ex.ToString());
IsRun = false;
}
return true;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
QueryData();
}
/// <summary>
/// 释放资源
/// </summary>
public void Release()
{
if (sb != null)
{
sb.closePort();
}
IsRun = false;
}
public ASTemperateParam LastData = new ASTemperateParam(0, 0, 0);
public bool QueryData()
{
ASTemperateParam param = new ASTemperateParam(0, 0, 0);
List<double> data = queryData();
if (data.Count.Equals(3))
{
//if (Setting_Init.Device_HumidityAdjust != 0 && Setting_Init.Device_HumidityLimited != 0)
//{
// if (data[0] + Setting_Init.Device_HumidityAdjust < Setting_Init.Device_HumidityLimited)
// {
// data[0] = Setting_Init.Device_HumidityLimited;
// }
// else
// data[0] += Setting_Init.Device_HumidityAdjust;
//}
data[0] = ProcessHumity(serialPort, data[0]);
//if (Setting_Init.Device_TemptureAdjust != 0 && Setting_Init.Device_TemptureLimited != 0)
//{
// if (data[1] + Setting_Init.Device_TemptureAdjust < Setting_Init.Device_TemptureLimited)
// {
// data[1] = Setting_Init.Device_TemptureLimited;
// }
// else
// data[1] += Setting_Init.Device_TemptureAdjust;
//}
data[1] = ProcessTemp(serialPort, data[1]);
var maxTemp = ConfigHelper.Config.Get("MaxTempThreshold", 60);
var minTemp = ConfigHelper.Config.Get("MinTempThreshold", 0);
if (data[1] >= maxTemp || data[1] <= minTemp)//温度
{
return false;
}
var maxHum = ConfigHelper.Config.Get("MaxHumidityThreshold", 100);
var minHum = ConfigHelper.Config.Get("MinHumidityThreshold", 0);
if (data[0] >= maxHum || data[0] <= minHum)//湿度
{
return false;
}
param = new ASTemperateParam(data[1], data[0], data[2]);
LastData = param;
return true;
}
return false;
}
/// <summary>
/// 温度数据处理
/// </summary>
/// <param name="nowTemp"></param>
/// <returns></returns>
private static double ProcessTemp(string port, 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;
}
else
{
var calib = ConfigHelper.Config.Get($"TemptureCalib_{port}", 0);
nowTemp += calib;
return nowTemp;
}
return Math.Round(temp, 1);
}
/// <summary>
/// 湿度数据处理
/// </summary>
/// <param name="nowHumity"></param>
/// <returns></returns>
private static double ProcessHumity(string port, 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 List<double> queryData()
{
// 温度计算:
//当温度低于 0 ℃ 时温度数据以补码的形式上传。
//温度:FF9B H(十六进制)= -101 => 温度 = -10.1℃
//湿度计算:
//湿度:292 H(十六进制) = 658 => 湿度 = 65.8 % RH
if (IsRun.Equals(false))
{
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);
}
private 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 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)
{
LOGGER.Info(LogName + "转换出错:" + ex.ToString());
}
return list;
}
}
}