SendWireManager.cs
9.3 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
using log4net;
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace URSoldering.DeviceLibrary
{
/// <summary>
/// 送丝管理类
/// </summary>
public class SendWireManager
{
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static bool IsRun = false;
public static string serialPort = "";
private static int bautRate = 19200;//波特率
private static Parity parity = Parity.None;//校验位
private static int dataBits = 8;//数据位
private static StopBits stopBits = StopBits.One; //停止位
private static SerialBean sb = null;
private static string CMD_WriteMode = "WMOD00002";
private static string CMD_SetSpeed = "WSPD";
private static string CMD_SetSendLength = "WLEN";
private static string CMD_GetSpeed = "RLEN";
private static string CMD_GetSendLength = "RSPD";
private static string CMD_GetStatus = "RECV";
private static string CMD_StartForwardSend = "WSFD00001";
private static string CMD_StartBackSend = "WSFD00000";
private static string CMD_StopSend = "WSSD00000";
private static string CMD_Reset = "WCTP00000";
//private static string CMD_WriteMode = "02 57 4D 4F 44 30 30 30 30 32 03 22";
//private static string CMD_SetSpeed = "02 57 53 50 44 30 30 31 31 30 03 21";
//private static string CMD_SetSendLength = "02 57 4C 45 4E 30 31 31 30 30 03 21";
//private static string CMD_GetSpeed = "02 52 53 50 44 03 14";
//private static string CMD_GetSendLength = "02 52 4C 45 4E 03 14";
//private static string CMD_GetStatus = "02 52 54 45 53 03 11";
//private static string CMD_StartForwardSend = "02 57 53 46 44 30 30 30 30 31 03 36";
//private static string CMD_StartBackSend = "02 57 53 46 44 30 30 30 30 30 03 37";
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
public static bool Init(string port)
{
if (sb == null)
{
serialPort = port;
new SerialBean(serialPort, bautRate, parity, dataBits, stopBits);
}
try
{
if (sb.openPort())
{
bool isOk = true;
parseCommand(CMD_WriteMode, out isOk);
if (isOk)
{
IsRun = true;
return true;
}
else
{
LogUtil.error("送丝机初始化失败!");
Release();
return false;
}
}
else
{
LogUtil.error(LOGGER, "送丝机串口打开失败!");
IsRun = false;
return false;
}
}
catch (Exception ex)
{
IsRun = false;
}
return true;
}
/// <summary>
/// 释放资源
/// </summary>
public static void Release()
{
bool isOk = false;
parseCommand(CMD_StopSend, out isOk);
sb.closePort();
IsRun = false;
}
public static void Reset()
{
bool isOk = false;
parseCommand(CMD_Reset, out isOk);
}
public static int querySpeed()
{
bool isOk = false;
byte[] reviceData = parseCommand(CMD_GetSpeed, out isOk);
return getReviceData(reviceData);
}
public static int queryLength()
{
bool isOk = false;
byte[] reviceData = parseCommand(CMD_GetSendLength, out isOk);
return getReviceData(reviceData);
}
public static int getReviceData(byte[] dataArray)
{
string temp = "";
try
{
if (dataArray == null)
{
return 0;
}
for (int i = 5; i <= 9; i++)
{
temp += (char)dataArray[i] + "";
}
}
catch (Exception ex)
{
LOGGER.Info("转换出错:" + ex.ToString());
}
int tem = 0;
try
{
tem = Convert.ToInt32(temp);
}
catch (Exception ex)
{
LogUtil.debug(LOGGER, "转换温度出错:" + temp);
}
return tem;
}
public static void SendWire(double time, int speed)
{
setSpeed(speed);
setLength(time * speed);
StartFSend();
}
public static bool setSpeed(double speed)
{
bool isOk = false;
int data = (int)speed * 10;
parseCommand(CMD_SetSpeed + data.ToString().PadLeft(5, '0'), out isOk);
return isOk;
}
public static bool setLength(double length)
{
bool isOk = false;
int data = (int)length * 10;
parseCommand(CMD_SetSendLength + data.ToString().PadLeft(5, '0'), out isOk);
return isOk;
}
public static bool StartFSend()
{
bool isOk = false;
parseCommand(CMD_StartForwardSend, out isOk);
return isOk;
}
public static bool StartBSend()
{
bool isOk = false;
parseCommand(CMD_StartBackSend, out isOk);
return isOk;
}
public static int ReadPortError()
{
bool isOk = false;
byte[] reviceData = parseCommand("RECV", out isOk);
return getReviceData(reviceData);
}
public static bool StopSend()
{
bool isOk = false;
parseCommand(CMD_StopSend, out isOk);
return isOk;
}
public static int ReadCounter()
{
bool isOk = false;
byte[] reviceData = parseCommand("RCTP", out isOk);
int value = getReviceData(reviceData);
return value;
}
private static byte[] parseCommand(string commandText, out bool isOk)
{
byte[] message = new byte[commandText.Length + 2];
message[0] = (byte)2;
for (int i = 1; i < commandText.Length + 1; i++)
{
message[i] = (byte)commandText[i - 1];
}
message[message.Length - 1] = (byte)3;
ushort bcc = 0;
SerialBean.CalculateBCC(message, message.Length, out bcc);
/**
* 读命令返回值包含数据域
* 写命令返回值不包含数据域
*/
byte[] data = null;
byte[] messageAll = new byte[message.Length + 1];
message.CopyTo(messageAll, 0);
messageAll[messageAll.Length - 1] = (byte)bcc;
if ("R".Equals(commandText.Substring(0, 1)))
{
data = new byte[messageAll.Length + 5];
}
else
{
data = new byte[messageAll.Length - 5];
}
sb.SendCommand(messageAll, ref data, 2, out isOk);
return data;
}
public static string GetErrorStr(int error)
{
string errMsg = "";
// 00000 OK
//00001 Tin feeding is not allowed
switch (error)
{
case 0:
errMsg = "OK";
break;
case 1:
errMsg = "Tin feeding is not allowed";
break;
}
return errMsg;
//Number:00001 BCC error (校验时发生帧错误) ASCII:0x30 0x30 0x30 0x30 0x31
// 00002 Format error (通信格式不正确) ASCII:0x30 0x30 0x30 0x30 0x32
// 00003 Out of range (修正值超出极限) ASCII:0x30 0x30 0x30 0x30 0x33
// 00004 Control error (不接受控制码) ASCII:0x30 0x30 0x30 0x30 0x34
// 00005 Control mode (你必须控制设备,模式机器人) ASCII:0x30 0x30 0x30 0x30 0x35
// 00006 Station model error (未知状态) ASCII:0x30 0x30 0x30 0x30 0x36
// 99999 Undefined (未定义的错误) ASCII:0x39 0x39 0x39 0x39 0x39
switch (error)
{
case 1:
errMsg = "BCC error";
break;
case 2:
errMsg = "Format error";
break;
case 3:
errMsg = "Out of range";
break;
case 4:
errMsg = "Control error";
break;
case 5:
errMsg = "Control mode";
break;
case 6:
errMsg = "Station model error";
break;
}
return errMsg;
}
}
}