OKLEController.cs
5.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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 Dictionary<string, double> ValueBuffer = new Dictionary<string, double>();
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 + "]";
if (!ValueBuffer.ContainsKey(port))
ValueBuffer.Add(port, 0);
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 queryData(string port,out double weight)
{
weight = 0d;
AcSerialBean sb = GetSerialBean(port);
if (sb == null)
{
return false;
}
if (!Monitor.TryEnter(sb))
return false;
try
{
LogName = "[OKLE_" + port + "]";
//12..1.2读取力值(毛重)指令,指令格式:01 03 00 50 00 02 C4 1A
//01 03 00 00 00 02 C4 0B
byte[] sendData = new byte[8];
sendData[0] = 0x01;
sendData[1] = 0x03;
sendData[2] = 0x00;
sendData[3] = 0x00;
sendData[4] = 0x00;
sendData[5] = 0x02;
sendData[6] = 0xC4;
sendData[7] = 0x0B;
//sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData, " ");
byte[] reviceData = new byte[15];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
//LogUtil.debug(LogName + "发送数据:" + str + ",收到数据 :" + AcSerialBean.byteToHexStr(reviceData, " "));
return getReviceData(reviceData,out weight);
}
catch(Exception e) {
LogUtil.debug(LogName + e.ToString());
return false;
}
finally {
Monitor.Exit(sb);
}
}
private static bool getReviceData(byte[] dataArray, out double value)
{
value = 0d;
try
{
//3D 53 47 2B 30 30 32 30 2E 30 31 6B BE 0D 0A
//=SG+0020.01k�
var tempstr = Encoding.ASCII.GetString(dataArray);
if (tempstr.StartsWith("="))
{
var numstr = tempstr.Substring(3, 8);
if (!double.TryParse(numstr, out value))
{
LogUtil.info(LogName + "转换出错:" + numstr.ToString());
return false;
}
return true;
}
else
{
LogUtil.info(LogName + "转换出错:" + tempstr.ToString());
return false;
}
}
catch (Exception ex)
{
LogUtil.info(LogName + "转换出错:" + ex.ToString());
}
return false;
}
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;
}
}