CodeFile1.cs
10.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Asa
{
/// <summary>
/// X-ray Source (AVJ Technologies Company)
/// </summary>
public class XRay
{
private System.IO.Ports.SerialPort port;
private System.Threading.Thread tSend;
private log4net.ILog log;
private bool warmUpComplete;
private bool loop;
private bool _receive;
private byte[] _buffer;
/// <summary>
/// 预热事件委托
/// </summary>
/// <param name="state">预热完成的状态</param>
/// <param name="text">文本</param>
public delegate void WarmUpEvent(bool state, string text);
/// <summary>
/// 预热事件
/// </summary>
public event WarmUpEvent WarmUp;
/// <summary>
/// X-ray Source
/// </summary>
/// <param name="logName"></param>
public XRay(string logName = "XRay")
{
port = new System.IO.Ports.SerialPort //9600,N,8,1
{
BaudRate = 9600,
Parity = System.IO.Ports.Parity.None,
DataBits = 8,
StopBits = System.IO.Ports.StopBits.One
};
port.DataReceived += Port_DataReceived;
_receive = false;
log = log4net.LogManager.GetLogger(logName);
}
/// <summary>
/// 串口打开或关闭状态
/// </summary>
public bool IsPortOpen { get { return port.IsOpen; } }
/// <summary>
/// X射线打开或关闭状态
/// </summary>
public bool IsRayOpen { get; private set; } = false;
/// <summary>
/// 打开设备
/// </summary>
/// <param name="portName">串口名称</param>
/// <param name="lastDate">最后使用日期</param>
/// <returns></returns>
public bool Open(string portName, string lastDate)
{
try
{
port.PortName = portName;
port.Open();
int days;
if (string.IsNullOrWhiteSpace(lastDate))
{
days = 3;
}
else
{
DateTime lastUsed = Convert.ToDateTime(lastDate);
TimeSpan ts = DateTime.Now.Subtract(lastUsed);
days = ts.Days;
}
log.Info("当前距离最后使用的天数:" + days);
loop = true;
tSend = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WarmUpProcess));
tSend.Start(days);
return port.IsOpen;
}
catch (Exception ex)
{
log.Error("Open", ex);
return false;
}
}
/// <summary>
/// 关闭设备
/// </summary>
public void Close()
{
port.Close();
}
/// <summary>
/// 设置电压电流
/// </summary>
/// <param name="voltage">电压kV(除小数点外必须4位数,不够的补0,小数必须1位)</param>
/// <param name="current">电流uA(必须4位数,没有小数,不够的补0)</param>
public bool SetVC(string voltage, string current)
{
bool rtn = SendCmd("VP" + voltage);
if (!rtn) return rtn;
System.Threading.Thread.Sleep(500);
rtn = SendCmd("CP" + current);
return rtn;
}
/// <summary>
/// 停止
/// </summary>
/// <returns></returns>
public bool Stop()
{
if (!warmUpComplete) return false;
bool rtn1 = SendCmd("ENBL0");
loop = false;
IsRayOpen = false;
return rtn1;
}
/// <summary>
/// 启动
/// </summary>
/// <returns></returns>
public bool Start()
{
if (!warmUpComplete) return false;
bool rtn = SendCmd("ENBL1");
if (rtn)
{
loop = true;
IsRayOpen = true;
tSend = new System.Threading.Thread(new System.Threading.ThreadStart(WatchDog));
tSend.Start();
}
return rtn;
}
private bool SendCmd(string cmd)
{
byte[] temp = Encoding.ASCII.GetBytes(cmd);
byte[] buff = new byte[temp.Length + 2];
Array.Copy(temp, 0, buff, 1, temp.Length);
buff[0] = 0x02; //包头
buff[buff.Length - 1] = 0x0D; //包尾
try
{
_receive = false;
log.Info("Send " + cmd + " Hex: " + HexBuff(buff));
port.Write(buff, 0, buff.Length);
bool rtn = Wait();
if (rtn)
{
string result = "";
if (_buffer[0] == 0x02 && _buffer[_buffer.Length - 1] == 0x0D)
result = Encoding.ASCII.GetString(_buffer, 1, _buffer.Length - 2);
log.Info("Received " + result + " Hex: " + HexBuff(_buffer));
}
else
{
log.Info("Received Timeout");
}
return rtn;
}
catch (Exception ex)
{
log.Error("SendCmd", ex);
return false;
}
}
private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(50);
byte[] temp = new byte[100];
int count = port.Read(temp, 0, temp.Length);
_buffer = new byte[count];
Array.Copy(temp, 0, _buffer, 0, count);
_receive = true;
}
private bool Wait()
{
int time = 0;
while (time < 5000)
{
if (_receive) return true;
System.Threading.Thread.Sleep(50);
time += 50;
}
return false;
}
private string HexBuff(byte[] buff)
{
string s = "";
if (buff == null) return s;
for (int i = 0; i < buff.Length; i++)
s += buff[i].ToString("X2") + " ";
return s;
}
/// <summary>
/// 看门狗时间,(多线程)
/// </summary>
private void WatchDog()
{
//设备在打开X光后,750ms内没有任何命令,就会关闭X光
while (loop)
{
bool rtn = SendCmd("WDTE");
if (!rtn) return;
System.Threading.Thread.Sleep(600);
}
}
/// <summary>
/// 预热处理,(多线程)
/// </summary>
/// <param name="obj">天数</param>
private void WarmUpProcess(object obj)
{
int days = (int)obj;
int millisecond = 0;
int step = 0; //步骤
int timer1 = 0; //计时器
int timer2 = 0;
string s;
bool rtn;
string[] voltage = new string[10] { "030.0", "035.5", "041.1", "046.6", "052.2", "057.7", "063.3", "068.8", "074.4", "080.0" }; //电压,固定值
string[] current = new string[10] { "0200", "0250", "0300", "0350", "0400", "0450", "0500", "0550", "0600", "0700" }; //电流,固定值
if (days < 3)
millisecond = 0;
else if (days >= 3 && days < 30) //3-30天
millisecond = 30000; //30s
else if (days >= 30 && days < 90) //1-3月
millisecond = 60000; //60s
else if (days >= 90) //3个月以上
millisecond = 300000; //300s
if (millisecond == 0)
{
s = "最后使用天数小于3天,不需要预热。";
log.Info(s);
warmUpComplete = true;
loop = false;
WarmUp?.Invoke(true, s);
return;
}
while (loop)
{
if (timer1 == 0)
{
rtn = SendCmd("VP" + voltage[step]);
if (!rtn)
{
loop = false;
break;
}
System.Threading.Thread.Sleep(50);
rtn = SendCmd("CP" + current[step]);
if (!rtn)
{
loop = false;
break;
}
if (step == 0) //第一步要打开平板
{
System.Threading.Thread.Sleep(50);
rtn = SendCmd("ENBL1");
if (!rtn)
{
loop = false;
break;
}
}
s = string.Format("预热{0}/10,{1}kV,{2}uA,{3}s", step + 1, voltage[step], current[step], millisecond);
log.Info(s);
}
if (timer2 == 0)
{
rtn = SendCmd("WDTE");
if (!rtn) break;
}
timer1 += 100;
timer2 += 100;
if (timer1 >= millisecond)
{
timer1 = 0;
step++;
if (step == 10)
loop = false;
}
if (timer2 >= 600)
{
timer2 = 0;
}
System.Threading.Thread.Sleep(100);
}
if (step == 10)
{
s = "预热完成";
log.Info(s);
warmUpComplete = true;
loop = false;
WarmUp?.Invoke(true, s);
}
else
{
s = "预热未完成";
log.Info(s);
warmUpComplete = false;
loop = false;
WarmUp?.Invoke(false, s);
}
rtn = SendCmd("ENBL0");
}
}
}