ABBControl.cs
21.1 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.IOSystemDomain;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
public class ABBControl
{
public static int DefaultSpeed = ConfigAppSettings.GetIntValue(Setting_Init.DefaultSpeed);
public static string DI_Start = "di_start";//启动程序
public static string DI_Stop = "di_stop";//停止程序
public static string DO_ES = "do_ES";//急停状态
public static string DO_IsRun = "do_IsRun";//机器人程序运行状态
public static string Do_AutoOn = "do_AutoOn";//是否自动状态
private static NetworkScanner scanner = null;
// private Controller controller = null;
// private static Task[] tasks = null;
private static NetworkWatcher networkwatcher = null;
public static Dictionary<string, Controller> controllerMap = new Dictionary<string, Controller>();
public static Dictionary<string, ControllerInfo> controllerInfoMap = new Dictionary<string, ControllerInfo>();
public delegate void ControllerAddDelegate(ControllerInfo controller);
public static event ControllerAddDelegate ControllerAddEvent;
public static void Load()
{
StartServer();
controllerMap = new Dictionary<string, Controller>();
controllerInfoMap = new Dictionary<string, ControllerInfo>();
scanner = new NetworkScanner();
scanner.Scan();
ControllerInfoCollection controllers = scanner.Controllers;
int i = 0;
foreach (ControllerInfo controllerInfo in controllers)
{
AddControllerInfo(controllerInfo);
i++;
}
networkwatcher = new NetworkWatcher(scanner.Controllers);
networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
networkwatcher.EnableRaisingEvents = true;
if (DefaultSpeed <= 0)
{
DefaultSpeed = 600;
ConfigAppSettings.SaveValue(Setting_Init.DefaultSpeed, 600);
}
}
private static bool AddControllerInfo(ControllerInfo controllerInfo)
{
try
{
Controller con = ControllerFactory.CreateFrom(controllerInfo);
con.Logon(UserInfo.DefaultUser);
ControllerState state = con.State;
string ip = con.IPAddress.ToString();
RemoveController(ip);
controllerMap.Add(ip, con);
// controller = con;
controllerInfoMap.Add(ip, controllerInfo);
LogUtil.info("成功添加机器人【" + ip + "】");
return true;
}
catch (Exception ex)
{
LogUtil.error("添加机器人【" + controllerInfo.IPAddress.ToString() + "】失败:" + ex.ToString());
}
return false;
}
private static void RemoveController(string ip)
{
if (controllerInfoMap.ContainsKey(ip))
{
controllerInfoMap.Remove(ip);
}
if (controllerMap.ContainsKey(ip))
{
controllerMap.Remove(ip);
}
}
private static void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
ControllerInfo controllerInfo = e.Controller;
if (controllerInfo != null)
{
if (AddControllerInfo(controllerInfo))
{
ControllerAddEvent?.Invoke(controllerInfo);
}
}
}
public static void StartABBControl(string ip, string signalName = "di_start", bool isReset = true)
{
if (isReset)
{
StopABBControl(ip);
}
SetRobotSignal(ip, signalName);
Thread.Sleep(100);
ResetRobotSignal(ip, signalName);
}
public static void StopABBControl(string ip, string signalName = "di_stop")
{
SetRobotSignal(ip, signalName);
Thread.Sleep(100);
ResetRobotSignal(ip, signalName);
}
public static void SetRobotSignal(string ip, string name)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
// string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABB[" + ip + "] GetSignal [" + name + "] =null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Set();
}
}
}
catch (Exception ex)
{
LogUtil.error("ABB[" + ip + "] SetRobotSignal [" + name + "] Error:" + ex.ToString());
}
}
public static void ResetRobotSignal(string ip, string name)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
//string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABB[" + ip + "] GetSignal [" + name + "]= null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
digitalSig.Reset();
}
}
}
catch (Exception ex)
{
LogUtil.error("ABB[" + ip + "] ResetRobotSignal [" + name + "] Error:" + ex.ToString());
}
}
public static int GetSingalState(string ip, string name = "do_ES")
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
//string name = comboBox1.Text;
Signal singalValue = controller.IOSystem.GetSignal(name);
if (singalValue == null)
{
LogUtil.error("ABB[" + ip + "] GetSingal [" + name + "]= null");
}
else
{
DigitalSignal digitalSig = (DigitalSignal)singalValue;
return (int)digitalSig.Value;
}
}
}
catch (Exception ex)
{
LogUtil.error("ABB[" + ip + "] GetSingalState [" + name + "] Error:" + ex.ToString());
}
return 0;
}
public static ControllerState GetRobotState(string ip)
{
try
{
Controller controller = GetControllerByIP(ip);
if (controller != null)
{
ControllerState state = controller.State;
// return state.ToString();
return state;
}
}
catch (Exception ex)
{
LogUtil.error("ABB[" + ip + "] GetRobotState" + "error:" + ex.ToString());
}
return ControllerState.Unknown;
}
public static Controller GetControllerByIP(string ip)
{
if (controllerMap.ContainsKey(ip))
{
return controllerMap[ip];
}
LogUtil.error("ABB [" + ip + "] GetControllerByIP= null");
return null;
}
public static void Dispose()
{
StopServer();
foreach (Controller con in controllerMap.Values)
{
con.Logoff();
con.Dispose();
}
controllerMap.Clear();
}
#region TCPServer
private static TcpServer tcpserver = null;
public static bool IsStart = false;
private static int ABBServerPort = ConfigAppSettings.GetIntValue(Setting_Init.ABBServerPort);
public static string ErrorInfo = "";
public static string Cmd_movep = "movep";
public static string Cmd_moveget = "moveget";
public static string Cmd_moveput = "moveput";
public static string Cmd_validateP = "validateP";
public static Dictionary<string, Client> ClientMap = new Dictionary<string, Client>();
//最后一次软件控制移动到的位置
public static string LastSendPoint = "";
//public delegate void OpEnd(string ip, string result);
//private static event OpEnd OnCmdEnd;
private static object LockObj = new object();
public static Dictionary<string, string> AbbCurrCmd = new Dictionary<string, string>();
private static void StartServer()
{
try
{
if (ABBServerPort <= 0)
{
ABBServerPort = 21;
}
if (!IsStart)
{
ClientMap = new Dictionary<string, Client>();
if (tcpserver == null)
{
tcpserver = new TcpServer();
}
IsStart = true;
tcpserver.Start(ABBServerPort);
tcpserver.AcceptClientEvent += Tcpserver_AcceptClientEvent;
LogUtil.info("ABBTcpServer在端口[" + ABBServerPort + "]监听!");
tcpserver.ReviceMsgEvent += tcp_ReviceMsgEvent;
}
}
catch (Exception ex)
{
LogUtil.error("ABBTcpServer启动[" + ABBServerPort + "]监听错误:" + ex.ToString());
}
}
private static void Tcpserver_AcceptClientEvent(Client client)
{
SaveRobotClient(client);
}
private static object MapLock = "";
private static void SaveRobotClient(Client client)
{
lock (MapLock)
{
IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
string ip = clientipe.Address.ToString();
if (ClientMap.ContainsKey(ip))
{
ClientMap.Remove(ip);
}
ClientMap.Add(ip, client);
}
}
private static void StopServer()
{
try
{
if (IsStart)
{
IsStart = false;
tcpserver.ReviceMsgEvent -= tcp_ReviceMsgEvent;
tcpserver.Stop();
}
}
catch (Exception ex)
{
LogUtil.error("关闭 监听出错:" + ex.ToString());
}
}
private static void tcp_ReviceMsgEvent(Client client, string msg)
{
try
{
if (System.String.IsNullOrEmpty(msg))
{
return;
}
ProcessMsg(client, msg);
}
catch (Exception ex)
{
LogUtil.error("处理ABB消息【" + msg + "】出错:" + ex.ToString());
}
}
public static string FINISHED = "FINISHED";
private static bool ProcessMsg(Client client, string msg)
{
IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
string add = clientipe.Address.ToString();
// string[] msgArray = msg.Split(cmd_spilt);
msg = msg.Replace("\r", "");
LogUtil.info("Revice[" + add + "]:[" + msg + "]");
//msg = msg.Replace(";", "");
string OkStr = "OK";
string preCmd = GetAbbCmd(add);
if (preCmd.Contains(FINISHED))
{
preCmd = preCmd.Replace(FINISHED, "");
}
if (msg.Contains("ERROR"))
{
ErrorInfo = msg;
}
else if ((!preCmd.Equals("")) && msg.Contains(preCmd))
{
if (msg.Contains(OkStr))
{
UpdateAbbCmd(add, "");
//OnCmdEnd?.Invoke(add, msg);
}
else if (msg.Contains(FINISHED))
{
//TODO
UpdateAbbCmd(add, msg);
}
}
//else if (msg.Contains(Cmd_movep) && msg.Contains(OkStr))
//{
// OnCmdEnd?.Invoke(add, msg);
//}
//else if (msg.Contains(Cmd_moveget) && msg.Contains(OkStr))
//{
// OnCmdEnd?.Invoke(add, msg);
// UpdateAbbCmd(add, "");
//}
else if (msg.Contains(OkStr))
{
LogUtil.info("Revice[" + add + "]:[" + msg + "] 未找到相关处理,preCmd=" + preCmd);
//OnCmdEnd?.Invoke(add, msg);
}
return false;
}
public static bool SendCmd(string robotIp, string moveCmd, string pName, string moveType = "L", int Speed = 10)
{
//取料get,根据尺寸7,11,13,15,分别为:p1,p2,p3,p4
//放料:大料架:cp,小料架:dp
string str = moveCmd + "," + pName + "," + moveType + "," + Speed + "";
UpdateAbbCmd(robotIp, str);
if (moveCmd.Equals(Cmd_moveget) || moveCmd.Equals(Cmd_movep) || moveCmd.Equals(Cmd_moveput) || moveCmd.Equals(Cmd_validateP))
{ }
else
{
LogUtil.error("Send [" + robotIp + "] : [" + str + "] 失败,命令无效");
return false;
}
Client client = null;
lock (LockObj)
{
if (ClientMap.ContainsKey(robotIp))
{
client = ClientMap[robotIp];
}
}
if (client != null && client.ClientSocket.Connected)
{
LastSendPoint = pName;
LogUtil.info("Send [" + robotIp + "] : [" + str + "]");
SaveLastCmd(robotIp, str);
byte[] sendBuffer = new byte[1024];
sendBuffer = Encoding.UTF8.GetBytes(str);
client.ClientSocket.Send(sendBuffer);
return true;
}
else
{
LogUtil.error("Send [" + robotIp + "] : [" + str + "] 失败,客户端未连接");
}
return false;
}
private static void UpdateAbbCmd(string robotIp, string moveCmd)
{
if (AbbCurrCmd.ContainsKey(robotIp))
{
AbbCurrCmd.Remove(robotIp);
}
if (System.String.IsNullOrEmpty(moveCmd).Equals(false))
{
AbbCurrCmd[robotIp] = moveCmd;
}
}
public static string GetAbbCmd(string robotIp)
{
if (AbbCurrCmd.ContainsKey(robotIp))
{
return AbbCurrCmd[robotIp];
}
return "";
}
public static bool IsConnected(string ip)
{
if (ClientMap.ContainsKey(ip) && ABBControl.GetRobotState(ip).Equals(ControllerState.MotorsOn))
{
if (ABBControl.GetSingalState(ip, ABBControl.Do_AutoOn).Equals(1))
return true;
}
return false;
}
#region 连续动作
public static bool IsAutoMove = false;
public static string LastAutoP = "";
public static void StartConMove(string ip, List<string> movePList, string mType = "L", string getp = "p1", int speed = 0)
{
if (speed <= 0)
{
speed = DefaultSpeed;
}
if (IsAutoMove)
{
return;
}
System.Threading.Tasks.Task.Factory.StartNew(delegate
{
IsAutoMove = true;
try
{
foreach (string currP in movePList)
{
LastAutoP = " " + getp + " -> " + currP + " 取料 ";
string result = "";
int timeOutMs = 600000;
DateTime startTime = DateTime.Now;
ABBControl.SendCmd(ip, ABBControl.Cmd_moveget, getp, "L", speed);
while (true)
{
Thread.Sleep(100);
TimeSpan span = DateTime.Now - startTime;
if (!IsAutoMove)
{
result = "手动停止";
break;
}
else if (span.TotalMilliseconds > timeOutMs)
{
result = getLastCmd(ip) + "超时";
break;
}
//判断Buzy及位置是否结束
else if (ABBControl.GetAbbCmd(ip).Equals(""))
{
result = "";
break;
}
}
if (result.Equals(""))
{
LogUtil.info("ABB " + ip + "连续运动[" + LastAutoP + "]结束:" + result);
}
else
{
LogUtil.error("ABB " + ip + "连续运动[" + LastAutoP + "]结束:" + result + ",结束连续运动");
break;
}
result = "";
startTime = DateTime.Now;
LastAutoP = " " + getp + " -> " + currP + " 放料 ";
ABBControl.SendCmd(ip, ABBControl.Cmd_moveput, currP, "L", speed);
while (true)
{
Thread.Sleep(50);
TimeSpan span = DateTime.Now - startTime;
if (!IsAutoMove)
{
result = "手动停止";
break;
}
else if (span.TotalMilliseconds > timeOutMs)
{
result = getLastCmd(ip) + "超时";
break;
}
//判断Buzy及位置是否结束
else if (ABBControl.GetAbbCmd(ip).Equals(""))
{
result = "";
break;
}
}
if (result.Equals(""))
{
LogUtil.info("ABB " + ip + "连续运动[" + LastAutoP + "]结束:" + result);
}
else
{
LogUtil.error("ABB " + ip + "连续运动[" + LastAutoP + "]结束:" + result + ",结束连续运动");
break;
}
}
}
catch (Exception ex)
{
LogUtil.error("ABB " + ip + "连续运动错误:" + ex.ToString());
}
finally { IsAutoMove = false; }
});
}
public static void StopConMove()
{
IsAutoMove = false;
}
private static Dictionary<string, string> lastCmdMap = new Dictionary<string, string>();
public static string getLastCmd(string ip)
{
if (lastCmdMap.ContainsKey(ip))
{
return lastCmdMap[ip];
}
return "";
}
private static void SaveLastCmd(string ip, string cmd)
{
try
{
if (lastCmdMap.ContainsKey(ip))
{
lastCmdMap[ip] = "[" + DateTime.Now.ToLongTimeString() + "]" + cmd;
}
else
{
lastCmdMap.Add(ip, "[" + DateTime.Now.ToLongTimeString() + "]" + cmd);
}
}
catch (Exception ex)
{
LogUtil.error("SaveLastCmd[" + ip + "][" + cmd + "]error :" + ex.ToString());
}
}
#endregion
}
#endregion
}