C8WeightSensor.cs
5.0 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class C8WeightSensor
{
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 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 double queryData(string port)
{
double weight = 0d;
AcSerialBean sb = GetSerialBean(port);
if (sb == null)
{
return weight;
}
LogName = "[OKLE_" + port + "]";
//12..1.2读取力值(毛重)指令,指令格式:02 41 42 30 33 03
byte[] sendData = new byte[6];
sendData[0] = 0x02;
sendData[1] = 0x41;
sendData[2] = 0x42;
sendData[3] = 0x30;
sendData[4] = 0x44;
sendData[5] = 0x03;
//sendData = buildCheckData(sendData, sendData.Length - 2);
string str = AcSerialBean.byteToHexStr(sendData, " ");
byte[] reviceData = new byte[14];
bool isOk = false;
sb.SendCommand(sendData, ref reviceData, 100, out isOk);
LogUtil.debug(LogName + "发送数据:" + str + ",收到数据 :" + AcSerialBean.byteToHexStr(reviceData, " "));
return getReviceData(reviceData);
}
public static double CalWeight(double value, double dValue = 0, double xishu = 1)
{
double v = (double)(value - dValue) * xishu;
LogUtil.debug("重量计算 (" + value + "-" + dValue + ")*" + xishu + "=" + v);
return v;
}
private static double getReviceData(byte[] dataArray)
{
double value = 0d;
try
{
if (dataArray.Length >= 14)
{
//以栈号01仪表为例子
//01仪表发送的毛重消息为02 41 62 2B 30 30 31 2E 30 30 30 32 37 03。
//2B 30 30 31 2E 30 30 30位具体的毛重数据,转换为ASCII码为 + 001.000;
//32H为校验码高位,37H为校验码低位,03H为结束符;
var datatxt = Encoding.ASCII.GetString(dataArray);
datatxt = datatxt.Substring(3, 8);
double.TryParse(datatxt, out value);
}
}
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;
}
}