SolderingManager.cs
8.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSA_V.Common;
using System.IO.Ports;
using log4net;
using System.Reflection;
namespace TSA_V.DeviceLibrary
{
/// <summary>
/// TODO:烙铁管理类
/// </summary>
public class SolderingManager
{
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static bool IsRun = false;
public static string serialPort = ConfigAppSettings.GetValue(Setting_Init.JBC_IRON_SERIAL_PORT);//串口号
public static string equipmentPort = ConfigAppSettings.GetValue(Setting_Init.JBC_IRON_EQUIPMENT_PORT);//设备端口号
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 = new SerialBean(serialPort, bautRate, parity, dataBits, stopBits);
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
public static bool Init()
{
try
{
if (IsRun)
{
return true ;
}
if (sb.openPort())
{
bool isOk = true;
parseCommand("WPS" + equipmentPort + "00000", 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("WPS" + equipmentPort + "00002",out isOk);
sb.closePort();
IsRun = false;
}
/// <summary>
/// 查询设置温度
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static int querySettingTemp( )
{
bool isOk = false;
byte[] settingTemp = parseCommand("RST" + equipmentPort,out isOk);
string temp = "";
for (int i = 5; i <= 9; i++)
{
temp += (char)settingTemp[i] + "";
}
int tem = 0;
try
{
tem = Convert.ToInt32(temp);
}
catch (Exception ex)
{
LogUtil.debug(LOGGER, "转换温度出错:" + temp);
}
return tem;
}
/// <summary>
/// 查询实际温度
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static int queryActualTemp()
{
if (IsRun)
{
bool isOk = false;
byte[] actualTemp = parseCommand("RTT" + equipmentPort, out isOk);
return getReviceTemp(actualTemp);
}
else
{
LogUtil.debug("烙铁未初始化,调用了queryActualTemp直接返回0");
return 0;
}
}
public static int getReviceTemp(byte[] tempArray)
{
string temp = "";
try
{
if (tempArray == null)
{
return 0;
}
for (int i = 5; i <= 9; i++)
{
temp += (char)tempArray[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;
}
/// <summary>
/// 设置温度
/// </summary>
/// <param name="commandText"></param>
public static bool setTemp(string temp)
{
if (!IsRun)
{
LogUtil.info("烙铁setTemp前未初始化,先初始化");
Init();
}
bool isOk = false;
parseCommand("WST" + equipmentPort + temp.PadLeft(5, '0'), out isOk);
return isOk;
}
public static int ReadMaxTemp()
{
bool isOk = false;
byte[] maxTemp = parseCommand("RMAT" , out isOk);
return getReviceTemp(maxTemp);
}
public static int ReadMinTemp()
{
bool isOk = false;
byte[] minTemp = parseCommand("RMIT", out isOk);
return getReviceTemp(minTemp);
}
public static bool IsRightTemp(int temperature, int minTemp, int maxTemp)
{
int temp = SolderingManager.queryActualTemp();
if (temperature != temp && (temp < minTemp))
//if (wait.Temperature != temp && (temp < wait.TemperatureMin || temp > wait.TemperatureMax))
{
return false;
}
else
{
return true;
}
}
public static int ReadPortError()
{
if (IsRun)
{
bool isOk = false;
byte[] minTemp = parseCommand("RPE" + equipmentPort, out isOk);
return getReviceTemp(minTemp);
}
else
{
return 0;
}
}
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)
{
// 00000 OK
//00001 Short-circuit
//00002 Short-circuit non-recoverable, equipment should be restarted
//00003 Open circuit
//00004 No tool
//00005 No tool accepted
//00006 Tool detection
//00007 Stop due to maximums powers (Not implemented)
//00008 Stop due to overload (MOS)
string errMsg="";
switch(error){
case 1:
errMsg="Short-circuit";
break;
case 2:
errMsg="Short-circuit non-recoverable, equipment should be restarted";
break;
case 3:
errMsg="Open circuit";
break;
case 4:
errMsg="No tool";
break;
case 5:
errMsg="No tool accepted";
break;
case 6:
errMsg="Tool detection";
break;
case 7:
errMsg="Stop due to maximums powers (Not implemented)";
break;
case 8:
errMsg="Stop due to overload (MOS)";
break;
}
return errMsg;
}
}
}