ABB.cs
18.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using System.Net;
using System.Net.Sockets;
using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.IOSystemDomain;
using System.Diagnostics;
using common = DoubleLine.Common;
namespace BLL
{
public class ABB
{
//private readonly Dictionary<ControllerState, string> STATE_STRING = new Dictionary<ControllerState, string>() { { ControllerState.Init, "初始化" }, { ControllerState.MotorsOff, "手动" }, { ControllerState.MotorsOn, "自动" }, { ControllerState.GuardStop, "卡住" }, { ControllerState.EmergencyStop, "急停" }, { ControllerState.EmergencyStopReset, "急停复位" }, { ControllerState.SystemFailure, "系统故障" }, { ControllerState.Unknown, "未知" } };
private bool loop;
private string[] _cmd;
private Socket server;
private Thread tListenClient;
private Thread tStatus;
private Client[] _client;
private Thread tMultitask;
private NetworkScanner scanner = null;
private NetworkWatcher networkwatcher = null;
public delegate void StatusEvent(int idx);
public event StatusEvent StatusChanged;
public event StatusEvent ServerChanged;
public ABB()
{
_client = new Client[3];
for (int i = 0; i < _client.Length; i++)
_client[i] = new Client();
}
/// <summary>
/// 停止多个任务
/// </summary>
public bool StopMultitask { set; get; } = false;
public void Start()
{
common.log.OutInfo("开启ABB机器人");
LoadController();
OpenServer();
}
public void Exit()
{
loop = false;
//tListenClient.Abort();
if (_client != null)
{
for (int i = 0; i < _client.Length; i++)
{
_client[i].Loop = false;
_client[i].IsConn = false;
if (_client[i].Socket != null)
_client[i].Socket.Close();
}
}
if (server != null)
server.Close();
common.log.OutInfo("关闭ABB机器人");
}
public void SendCommand(int idx, string s)
{
try
{
if (_client[idx] == null) return;
if (!_client[idx].IsConn) return;
byte[] buff = Encoding.ASCII.GetBytes(s);
_client[idx].Socket.Send(buff);
common.log.OutInfo(common.ABB_Info[idx].Name + " Send:" + s);
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
public void Multitask(int idx, string[] cmd)
{
_cmd = cmd;
StopMultitask = false;
tMultitask = new Thread(new ParameterizedThreadStart(Multitask));
tMultitask.Start(idx);
}
/// <summary>
/// Get指令
/// </summary>
/// <param name="idx">机器人索引号</param>
/// <param name="point">架位,带p</param>
/// <param name="speed">速度</param>
public void MoveGet(int idx, string point, int speed)
{
common.ABB_Info[idx].GetStart = true;
common.ABB_Info[idx].GetEnd = false;
common.ABB_Info[idx].PutStart = false;
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = false;
point = point.ToLower();
string s = string.Format("{0},{1},{2},{3}", common.ABB_MOVEGET, point, "L", speed);
SendCommand(idx, s);
common.log.OutInfo(common.ABB_Info[idx].Name + " MoveGet Start");
}
/// <summary>
/// Put指令
/// </summary>
/// <param name="idx">机器人索引号</param>
/// <param name="point">架位,带c/d</param>
/// <param name="speed">速度</param>
public void MovePut(int idx, string point, int speed)
{
common.ABB_Info[idx].GetStart = false;
common.ABB_Info[idx].GetEnd = false;
common.ABB_Info[idx].PutStart = true;
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = false;
point = point.ToLower();
string s = string.Format("{0},{1},{2},{3}", common.ABB_MOVEPUT, point, "L", speed);
SendCommand(idx, s);
common.log.OutInfo(common.ABB_Info[idx].Name + " MovePut Start");
}
private void StartDevice(int idx)
{
try
{
if (_client[idx].Ctl == null)
{
common.log.OutInfo(common.ABB_Info[idx].Name + " 没有连接");
}
else
{
if (_client[idx].IsConn)
{
common.log.OutInfo(common.ABB_Info[idx].Name + " 已经打开");
}
else
{
Signal signal = _client[idx].Ctl.IOSystem.GetSignal("di_start");
((DigitalSignal)signal).Set();
Thread.Sleep(100);
((DigitalSignal)signal).Reset();
common.ABB_Info[idx].IsOpen = true;
common.log.OutInfo(common.ABB_Info[idx].Name + " 打开设备");
}
}
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
private void StopDevice(int idx)
{
try
{
if (_client[idx].Ctl == null)
{
common.log.OutInfo(common.ABB_Info[idx].Name + " 没有连接");
}
else
{
Signal signal = _client[idx].Ctl.IOSystem.GetSignal("di_stop");
((DigitalSignal)signal).Set();
Thread.Sleep(100);
((DigitalSignal)signal).Reset();
Thread.Sleep(100);
common.ABB_Info[idx].IsOpen = false;
if (tMultitask != null) tMultitask.Abort();
_client[idx].IsConn = false;
_client[idx].Loop = false;
_client[idx].Socket.Close();
ServerChanged?.Invoke(idx);
common.log.OutInfo(common.ABB_Info[idx].Name + " 关闭");
}
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
private void Multitask(object obj)
{
int idx = (int)obj;
if (_client[idx] == null) return;
if (!_client[idx].IsConn) return;
for (int i = 0; i < _cmd.Length; i++)
{
common.ABB_Info[idx].PutStart = true;
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = false;
SendCommand(idx, _cmd[i]);
while (true)
{
Thread.Sleep(1000);
if (common.ABB_Info[idx].PutEnd)
break;
}
if (StopMultitask)
{
StopMultitask = false;
break;
}
Thread.Sleep(1000);
}
}
private void ListenClient()
{
while (loop)
{
try
{
Socket socket = server.Accept(); //这边会暂停,不需要sleep
IPEndPoint ep = (IPEndPoint)socket.RemoteEndPoint;
Thread thread = new Thread(new ParameterizedThreadStart(Received));
string ip = ep.Address.ToString();
int idx = Array.FindIndex(common.ABB_Info, s => s.IP == ip);
if (idx == -1) continue;
_client[idx].Socket = socket;
_client[idx].Receive = thread;
_client[idx].IsConn = true;
_client[idx].Loop = true;
common.ABB_Info[idx].IsOpen = true;
//common.ABB_Info[idx].State = "";
thread.Start(idx);
ServerChanged?.Invoke(idx);
common.log.OutInfo(common.ABB_Info[idx].Name + " 连接到服务端");
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
}
private void ListenStatus()
{
int sum;
while (loop)
{
sum = 0;
try
{
for (int i = 0; i < _client.Length; i++)
{
if (_client[i].Ctl == null)
{
common.log.OutInfo("ListenStatus " + common.ABB_Info[i].Name + " 不存在");
}
else
{
if (_client[i].Ctl.State.ToString() == common.ABB_Info[i].State)
continue;
common.ABB_Info[i].State = _client[i].Ctl.State.ToString();
StatusChanged?.Invoke(i);
if (_client[i].Ctl.State == ControllerState.MotorsOn)
{
if (!_client[i].IsConn) StartDevice(i);
}
else
{
if (_client[i].IsConn)
StopDevice(i);
}
if (_client[i].IsConn) sum++;
}
}
}
catch (Exception ex)
{
common.log.OutError(ex);
}
Thread.Sleep(1000);
}
}
private void Received(object obj)
{
int idx = (int)obj;
Socket client = _client[idx].Socket;
while (_client[idx].Loop)
{
if (!_client[idx].IsConn) break;
if (client.Available > 0)
{
byte[] buffer = new byte[1024];
int count = client.Receive(buffer);
//返回的命令用逗号(,)分隔,最后有分号(;)
string ss = Encoding.ASCII.GetString(buffer, 0, count);
common.log.OutInfo(common.ABB_Info[idx].Name + " Receive:" + ss);
string[] arr = ss.Split(',');
if (arr[0] == common.ABB_MOVEGET)
{
common.ABB_Info[idx].GetStart = false;
common.ABB_Info[idx].GetEnd = true;
common.log.OutInfo(common.ABB_Info[idx].Name + " Start=false End=true");
}
else if (arr[0] == common.ABB_MOVEPUT)
{
common.ABB_Info[idx].PutStart = false;
if (arr[4].StartsWith("OK"))
{
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = true;
common.log.OutInfo(common.ABB_Info[idx].Name + " Start=false In=false End=true");
}
else if (arr[4].StartsWith("FINISHED"))
{
common.ABB_Info[idx].PutIn = true;
common.ABB_Info[idx].PutEnd = false;
common.log.OutInfo(common.ABB_Info[idx].Name + " Start=false In=true End=false");
}
else
{
common.log.OutInfo(common.ABB_Info[idx].Name + " arr[4]=" + arr[4]);
}
}
}
Thread.Sleep(100);
}
}
private void OpenServer()
{
try
{
string ip = System.Configuration.ConfigurationManager.AppSettings["Local_IP"];
IPAddress[] add = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
bool find = false;
foreach (IPAddress ipAdd in add)
{
if (ipAdd.AddressFamily.ToString() == "InterNetwork")
{
if (ip == ipAdd.ToString())
{
find = true;
break;
}
}
}
if (find)
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(ip), 21);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(localEP);
server.Listen(10);
tListenClient = new Thread(new ThreadStart(ListenClient));
tStatus = new Thread(new ThreadStart(ListenStatus));
loop = true;
tListenClient.Start();
tStatus.Start();
common.log.OutInfo("ABB Server Open");
}
else
{
common.log.OutInfo("没有找到本地IP:" + ip);
common.log.OutTextBox("没有找到本地IP:" + ip);
}
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
public void LoadController()
{
try
{
scanner = new NetworkScanner();
scanner.Scan();
common.log.OutInfo("ABB Controller Count=" + scanner.Controllers.Count);
foreach (ControllerInfo controllerInfo in scanner.Controllers)
{
string ip = controllerInfo.IPAddress.ToString();
int idx = Array.FindIndex(common.ABB_Info, s => s.IP == ip);
if (idx == -1) return;
_client[idx] = new Client { Ctl = ControllerFactory.CreateFrom(controllerInfo) };
_client[idx].Ctl.Logon(UserInfo.DefaultUser);
//_client[idx].Ctl.StateChanged += Controller_StateChanged;
common.ABB_Info[idx].GetStart = false;
common.ABB_Info[idx].GetEnd = true;
common.ABB_Info[idx].PutStart = false;
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = true;
common.ABB_Info[idx].State = "";
common.log.OutInfo(common.ABB_Info[idx].Name + " State=" + _client[idx].Ctl.State.ToString());
}
networkwatcher = new NetworkWatcher(scanner.Controllers);
networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(FoundEvent);
networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(LostEvent);
networkwatcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
private void Controller_StateChanged(object sender, StateChangedEventArgs e)
{
//try
//{
// Controller ctl = (sender as Controller);
// int idx = Array.FindIndex(common.ABB_Info, s => s.IP == ctl.IPAddress.ToString());
// if (idx == -1) return;
// common.ABB_Info[idx].State = ctl.State.ToString();
// StatusChanged?.Invoke(idx);
// if (ctl.State == ControllerState.MotorsOn)
// {
// if(!_client[idx].IsConn)
// StartDevice(idx);
// }
// else
// {
// if (_client[idx].IsConn)
// StopDevice(idx);
// }
//}
//catch (Exception ex)
//{
// common.log.OutError(ex);
//}
}
private void FoundEvent(object sender, NetworkWatcherEventArgs e)
{
try
{
string ip = e.Controller.IPAddress.ToString();
common.log.OutInfo("Find IP=" + ip);
int idx = Array.FindIndex(common.ABB_Info, s => s.IP == ip);
if (idx == -1) return;
_client[idx].Ctl = ControllerFactory.CreateFrom(e.Controller);
_client[idx].Ctl.Logon(UserInfo.DefaultUser);
//_client[idx].IP = common.ABB_Info[idx].IP;
common.ABB_Info[idx].GetStart = false;
common.ABB_Info[idx].GetEnd = true;
common.ABB_Info[idx].PutStart = false;
common.ABB_Info[idx].PutIn = false;
common.ABB_Info[idx].PutEnd = true;
common.ABB_Info[idx].State = _client[idx].Ctl.State.ToString();
common.log.OutInfo(common.ABB_Info[idx].Name + " State=" + _client[idx].Ctl.State.ToString());
}
catch (Exception ex)
{
common.log.OutError(ex);
}
}
private void LostEvent(object sender, NetworkWatcherEventArgs e)
{
}
private class Client
{
/// <summary>
/// 客户端IP地址
/// </summary>
//public string IP;
/// <summary>
/// Socket TCP
/// </summary>
public Socket Socket;
/// <summary>
/// 接收事件
/// </summary>
public Thread Receive;
/// <summary>
/// 控制器
/// </summary>
public Controller Ctl;
public bool Loop;
/// <summary>
/// 是否连接服务器
/// </summary>
public bool IsConn;
public Client()
{
//IP = "";
Loop = false;
IsConn = false;
}
}
}
}