BenQ_AGV.cs
12.8 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 逐鹿的AGV小车
/// </summary>
public class BenQ_AGV
{
private Socket client;
private Thread tReceive;
private Thread tState;
private Thread tReconn;
private string _ip;
private int packetID;
private byte[] buff;
private string buffStr;
private string queryPacket; //通知询问指令封包ID
private bool exit = false;
private bool loop = false;
public delegate void StatusEvent(bool status);
public event StatusEvent Status;
public BenQ_AGV()
{
Stop = false;
_ip = ConfigAppSettings.GetValue(Setting_Init.BenQ_AGV);
packetID = 0;
loop = true;
tReconn = new Thread(new ThreadStart(Reconnect));
tReconn.Start();
}
/// <summary>
/// 服务器是否连接
/// </summary>
public bool IsConn { set; get; }
/// <summary>
/// 左侧存在料架
/// </summary>
public bool LeftExist { set; get; }
/// <summary>
/// 右侧存在料架
/// </summary>
public bool RightExist { set; get; }
/// <summary>
/// 左侧入料请求
/// </summary>
public bool LeftEnter { set; get; }
/// <summary>
/// 右侧入料请求
/// </summary>
public bool RightEnter { set; get; }
/// <summary>
/// 右侧出料请求
/// </summary>
public bool RightLeave { set; get; }
/// <summary>
/// 停止动作
/// </summary>
public bool Stop { set; get; }
/// <summary>
/// RFID两位的文本
/// </summary>
public string RFID { set; get; } = "";
/// <summary>
/// 是否可以左侧入料
/// </summary>
/// <param name="state">true可以入料</param>
public void MayLeftEnter(bool state)
{
LeftEnter = false;
if (IsConn)
{
SendResponse(2, state);
SendEnterState(2, state);
}
}
/// <summary>
/// 是否可以右侧入料
/// </summary>
/// <param name="state">true可以入料</param>
public void MayRightEnter(bool state)
{
RightEnter = false;
if (IsConn)
{
SendResponse(1, state);
SendEnterState(1, state);
}
}
/// <summary>
/// 是否可以出料
/// </summary>
/// <param name="state">true可以出料</param>
public void MayLeave(bool state)
{
RightLeave = false;
if (IsConn)
{
SendResponse(1, state);
SendLeaveState(state);
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
exit = true;
loop = false;
IsConn = false;
LogUtil.info("BenQ_AGV : =====程序结束=====\r\n");
}
/// <summary>
/// 连接
/// </summary>
private void Connect()
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(_ip, 1000);
ping.Dispose();
if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 500);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
client.Connect(System.Net.IPAddress.Parse(_ip), 9800);
exit = false;
tReceive = new Thread(new ThreadStart(Listen));
tReceive.Start();
tState = new Thread(new ThreadStart(SendCmd));
tState.Start();
LogUtil.info("BenQ_AGV : AGV连接成功");
IsConn = true;
}
else
{
LogUtil.error("BenQ_AGV : Ping " + _ip + " 请求没有响应");
IsConn = false;
}
}
catch (Exception ex)
{
exit = true;
LogUtil.error("BenQ_AGV : " + ex.ToString());
IsConn = false;
}
}
/// <summary>
/// 发送入口状态
/// </summary>
/// <param name="dev">1右侧,2左侧</param>
/// <param name="roll"></param>
private void SendEnterState(int dev, bool roll)
{
string s;
string msg = "@1";
//封包ID
packetID++;
if (packetID == 500)
packetID = 1;
s = packetID.ToString().PadLeft(3, '0');
msg += s;
//设备编码
msg += "00" + dev;
//工作状态
if (Stop)
msg += "3";
else
msg += "1";
//RFID,入口没有
msg += "0000";
//外侧内测
msg += "00";
//动力滚筒
if (Stop)
msg += "1";
else
{
if (roll)
msg += "3";
else
msg += "1";
}
//上料口
if (LeftExist)
msg += "2";
else
msg += "1";
//下料口
if (RightExist)
msg += "2";
else
msg += "1";
//9个0
msg += "000000000";
//校验和
msg += "00";
msg += "!";
try
{
client.Send(Encoding.ASCII.GetBytes(msg));
LogUtil.debug("BenQ_AGV : SendEnterState " + msg);
}
catch (Exception ex)
{
LogUtil.error("BenQ_AGV : " + ex.ToString());
IsConn = false;
exit = true;
}
}
/// <summary>
/// 发送出口状态
/// </summary>
/// <param name="roll"></param>
private void SendLeaveState(bool roll)
{
string s;
string msg = "@1";
//封包ID
packetID++;
if (packetID == 500)
packetID = 1;
s = packetID.ToString().PadLeft(3, '0');
msg += s;
//设备编码
msg += "001";
//工作状态
if (Stop)
msg += "3";
else
msg += "1";
//RFID
if (Stop)
msg += "0000";
else
msg += RFID.PadLeft(4, '0');
//外侧内测
msg += "00";
//动力滚筒
if (Stop)
msg += "1";
else
{
if (roll)
msg += "2";
else
msg += "1";
}
//上料口
if (LeftExist)
msg += "2";
else
msg += "1";
//下料口
if (RightExist)
msg += "2";
else
msg += "1";
//9个0
msg += "000000000";
//校验和
msg += "00";
msg += "!";
try
{
client.Send(Encoding.ASCII.GetBytes(msg));
LogUtil.debug("BenQ_AGV : SendLeaveState " + msg);
}
catch (Exception ex)
{
LogUtil.error("BenQ_AGV : " + ex.ToString());
IsConn = false;
exit = true;
}
}
/// <summary>
/// 发送设备询问应答
/// </summary>
/// <param name="dev">1右侧,2左侧</param>
/// <param name="sta"></param>
private void SendResponse(int dev, bool sta)
{
string msg = "@2";
//封包ID
msg += queryPacket;
//设备编码
msg += "00" + dev;
//执行结果,1通过,2不通过
msg += sta ? "1" : "2";
//18个0
msg += "000000000000000000";
//校验和
msg += "00";
//结束符号
msg += "!";
try
{
client.Send(Encoding.ASCII.GetBytes(msg));
LogUtil.debug("BenQ_AGV : SendResponse " + msg);
}
catch (Exception ex)
{
LogUtil.error("BenQ_AGV : " + ex.ToString());
IsConn = false;
exit = true;
}
}
private void Reconnect()
{
int count = 0;
while (loop)
{
if (IsConn)
{
Thread.Sleep(5000);
}
else
{
Thread.Sleep(1000);
Connect();
if (IsConn)
{
count = 0;
Status?.Invoke(true);
}
else
{
LogUtil.info("BenQ_AGV : AGV连接失败" + ++count + "次");
Status?.Invoke(false);
}
//Thread.Sleep(2000);
}
}
}
private void SendCmd()
{
while (true)
{
if (IsConn) SendEnterState(2, false);
Thread.Sleep(1500);
if (IsConn) SendEnterState(1, false);
Thread.Sleep(1500);
if (IsConn) SendLeaveState(false);
Thread.Sleep(1500);
if (exit) break;
}
}
private void Listen()
{
byte[] temp = new byte[50];
while (true)
{
Thread.Sleep(50);
try
{
if (client.Available > 0)
{
int count = client.Receive(temp);
buff = new byte[count];
Array.Copy(temp, 0, buff, 0, count);
buffStr = Encoding.ASCII.GetString(buff);
LogUtil.debug("BenQ_AGV : Received " + buffStr);
if (temp[1] == 65) //A
CodeA();
else if (temp[1] == 66) //B
CodeB();
}
}
catch (Exception ex)
{
LogUtil.error("BenQ_AGV : " + ex.ToString());
IsConn = false;
exit = true;
}
if (exit) break;
}
}
private void CodeA()
{
if (buffStr[7] == '1') //右侧出料
SendLeaveState(false);
else if (buffStr[7] == '2') //左右侧进料
SendEnterState(2, false);
}
private void CodeB()
{
LogUtil.debug("Enter CodeB");
queryPacket = buffStr.Substring(2, 3);
if (Stop)
{
SendResponse(buffStr[7], false);
}
else
{
if (buffStr[7] == '2') //左侧
{
LogUtil.debug("Enter CodeB 左侧:" + buffStr[8]);
LeftEnter = true;
}
else if (buffStr[7] == '1') //右侧
{
if (buffStr[8] == '1') //出去
RightLeave = true;
if (buffStr[8] == '2') //进入
RightEnter = true;
LogUtil.debug("Enter CodeB 右侧:" + buffStr[8]);
}
else
{
LogUtil.debug("Enter CodeB ELSE: " + buffStr[7]);
}
}
}
}
}