HumitureController.cs
11.1 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
using OnlineStore;
using log4net;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
namespace OnlineStore.Common
{
/// <summary>
/// 壁挂王字壳温湿度变送器(485型)
/// </summary>
public class HumitureController
{
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static bool IsRun = false;
public static string serialPort = "";
private static int bautRate = 4800;//波特率
private static Parity parity = Parity.None;//校验位
private static int dataBits = 8;//数据位
private static StopBits stopBits = StopBits.One; //停止位
private static AcSerialBean sb = null;
private static string LogName = "";
public static int HumitureControllerType = ConfigAppSettings.GetIntValue(Setting_Init.HumitureControllerType);
public static bool Init(string port)
{
if (IsRun && port.Equals(serialPort))
{
return true;
}
else if (IsRun)
{
Release();
}
LogName = crc.GetString("Res0037","温湿度传感器[") + 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;
return true;
}
else
{
LogUtil.error(LOGGER, LogName + "串口打开失败!");
IsRun = false;
return false;
}
}
catch (Exception ex)
{
IsRun = false;
}
return true;
}
/// <summary>
/// 释放资源
/// </summary>
public static void Release()
{
if (sb != null)
{
sb.closePort();
}
IsRun = false;
}
public static ASTemperateParam LastData = new ASTemperateParam(0, 0);
public static ASTemperateParam QueryData()
{
ASTemperateParam param = new ASTemperateParam(0, 0);
List<double> data = queryData();
if (data.Count.Equals(2))
{
param = new ASTemperateParam(data[1], data[0]);
}
LastData = param;
return param;
}
/// <summary>
/// 返回温度和湿度
/// </summary>
/// <returns></returns>
private static 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] = 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 getReviceData(reviceData);
}
public static int QueryHistoryCount()
{
if (IsRun.Equals(false) || 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()
{
if (IsRun.Equals(false) || 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()
{
List<object> list = new List<object>();
if (IsRun.Equals(false)||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)
{
LOGGER.Info(LogName + crc.GetString("Res0038","转换出错:") + 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)
{
LOGGER.Info(LogName + crc.GetString("Res0038","转换出错:") + 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]);
double tempV = (double)Convert.ToInt32(temp, 16)/10;
double humV =(double) Convert.ToInt32(hum, 16)/10;
list.Add(tempV);
list.Add(humV);
}
}
catch (Exception ex)
{
LOGGER.Info(LogName + crc.GetString("Res0038","转换出错:") + ex.ToString());
}
return list;
}
}
}