OKLEController.cs
6.7 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class OKLEController
{
private static int bautRate = 9600;//波特率
private static Parity parity = Parity.None;//校验位
private static int dataBits = 8;//数据位
private static StopBits stopBits = StopBits.One; //停止位
private static Dictionary<string, AcSerialBean> SerialMap = new Dictionary<string, AcSerialBean>();
private static string LogName = "";
private static AcSerialBean GetSerialBean(string port)
{
if (SerialMap.ContainsKey(port))
{
return SerialMap[port];
}
return null;
}
public static void OpenAll(string[] ports)
{
foreach (string port in ports)
{
Init(port);
}
}
public static bool Init(string port)
{
if (SerialMap.ContainsKey(port))
{
return true;
}
LogName = "[OKLE_" + port + "]";
AcSerialBean sb = new AcSerialBean(port, bautRate, parity, dataBits, stopBits);
try
{
if (sb.openPort())
{
if (SerialMap.ContainsKey(port))
{
SerialMap.Remove(port);
}
SerialMap.Add(port, sb);
return true;
}
else
{
LogUtil.error(LogName + "串口打开失败!");
return false;
}
}
catch (Exception ex)
{
LogUtil.error(LogName + "打开出错:" + ex.ToString());
}
return true;
}
public static bool IsRun(string port)
{
if (GetSerialBean(port) != null)
{
return true;
}
return false;
}
/// <summary>
/// 释放资源
/// </summary>
public static void Release(params string[] ports)
{
if (ports.Length <= 0)
{
ports = (new List<string>(SerialMap.Keys)).ToArray();
}
foreach (string port in ports)
{
try
{
AcSerialBean sb = GetSerialBean(port);
if (sb != null)
{
sb.closePort();
}
//IsRun = false;
SerialMap.Remove(port);
}
catch (Exception ex)
{
LogUtil.error("Release OKLE port [" + port + "] error :" + ex.ToString());
}
}
}
public static bool Reset(string port)
{
AcSerialBean sb = GetSerialBean(port);
if (sb == null)
{
return false;
}
LogName = "[OKLE_" + port + "]";
//12.1.4执行手动置零
//指令格式:01 10 00 5E 00 01 02 00 01 6A EE
byte[] sendData = new byte[11];
sendData[0] = 0x01;
sendData[1] = 0x10;
sendData[2] = 0x00;
sendData[3] = 0x5E;
sendData[4] = 0x00;
sendData[5] = 0x01;
sendData[6] = 0x02;
sendData[7] = 0x00;
sendData[8] = 0x01;
sendData[9] = 0x00;
sendData[10] = 0x00;
sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData);
LogUtil.debug(LogName + "发送数据:" + str);
byte[] reviceData = new byte[9];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
return isOk;
}
public static double queryData(string port)
{
double weight = 0d;
AcSerialBean sb = GetSerialBean(port);
if (sb == null)
{
return weight;
}
LogName = "[OKLE_" + port + "]";
//12..1.2读取力值(毛重)指令,指令格式:01 03 00 50 00 02 C4 1A
byte[] sendData = new byte[8];
sendData[0] = 0x01;
sendData[1] = 0x03;
sendData[2] = 0x00;
sendData[3] = 0x50;
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(LogName + "发送数据:" + str);
byte[] reviceData = new byte[9];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
return getReviceData(reviceData);
}
private static double getReviceData(byte[] dataArray)
{
double value = 0d;
try
{
if (dataArray.Length >= 9)
{
//返回格式:01 03 04 FF FF C1 F0 AB C3(数据根据实际情况变化)
//地址 功能码 字节数 第一组寄存器数据 第二组寄存器数据 CRC16校验
//01 03 04 FF FF C1 F0 AB C3
string highV = String.Format("{0:X2}", dataArray[3]) + String.Format("{0:X2}", dataArray[4]);
string lowV = String.Format("{0:X2}", dataArray[5]) + String.Format("{0:X2}", dataArray[6]);
value = (double)Convert.ToInt32(highV + lowV, 16) / 10000;
}
}
catch (Exception ex)
{
LogUtil.info(LogName + "转换出错:" + ex.ToString());
}
return value;
}
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;
}
}
}