KukaAvarProxyCommunication.cs
5.05 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
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.Models;
using HslCommunication;
using HslCommunication.Robot.KUKA;
using System.Diagnostics;
using System.Text;
namespace DataAcquisition.Common.Communications
{
/// <summary>
/// Kuka机器人通信
/// </summary>
public class KukaAvarProxyCommunication : ICommunication
{
public readonly KukaAvarProxyNet KukaAvar = null!;
private readonly SystemLog systemLog = SystemLog.Instance;
public string IpAddress => KukaAvar.IpAddress;
public int CommunicationId { get; set; }
private KukaAvarProxyCommunication() { }
public KukaAvarProxyCommunication(int communicationId, string ipAddress, int port = 7000)
{
CommunicationId = communicationId;
KukaAvar = new KukaAvarProxyNet(ipAddress, port);
KukaAvar.SetPersistentConnection();
}
public OperateResult ConnectServer() => KukaAvar.ConnectServer();
public OperateResult ConnectClose() => KukaAvar.ConnectClose();
public void Read(IEnumerable<EquipmentProperty> equipmentProperties)
{
try
{
var stopwatch = Stopwatch.StartNew();
foreach (var item in equipmentProperties)
{
var nodes = item.DataAddress?.Split(';');
if (item.Code == RobotProps.Work_Mode.ToString() && nodes!.Length > 1)
{
foreach (var node in nodes)
{
var nodeResult = KukaAvar.Read(node);
if (!nodeResult.IsSuccess)
{
systemLog.LogError($"读取Kuka机器人[{KukaAvar.IpAddress}]地址{item.DataAddress}数据失败:{nodeResult.Message}");
return;
}
_ = bool.TryParse(Encoding.Default.GetString(nodeResult.Content), out var val);
if (val)
{
item.Value = $"{Array.IndexOf(nodes, node) + 1}";
item.UpdateTime = DateTime.Now;
break;
}
}
}
else
{
var result = KukaAvar.Read(item.DataAddress);
if (!result.IsSuccess)
{
item.Value = string.Empty;
item.UpdateTime = DateTime.Now;
systemLog.LogError($"读取Kuka机器人[{KukaAvar.IpAddress}]地址{item.DataAddress}数据失败:{result.Message}");
return;
}
var val = Encoding.Default.GetString(result.Content);
if (item.Code == RobotProps.Weld_V.ToString())
{
_ = float.TryParse(val, out var v);
item.Value = v > 0 ? $"{v / 32767 * 107}" : "0";
}
else if (item.DataType == DataTypeConst.Bool)
{
item.Value = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(val.ToLower());
}
else
{
item.Value = val;
}
}
item.UpdateTime = DateTime.Now;
}
stopwatch.Stop();
systemLog.LogInfo($"读取Kuka变量[{equipmentProperties.FirstOrDefault()?.Equipment.CommunicationConfig?.IpAddress}]地址数量:{equipmentProperties.Count()},耗时:{stopwatch.Elapsed}");
}
catch (Exception ex)
{
systemLog.LogError($"读取Kuka机器人[{KukaAvar.IpAddress}]数据失败:{ex.Message}");
}
}
public void Read(EquipmentProperty equipmentProperty)
{
Read([equipmentProperty]);
}
public void Write(IEnumerable<EquipmentProperty> equipmentProperties)
{
try
{
foreach (var item in equipmentProperties)
{
var result = KukaAvar.Write(item.DataAddress, item.Value);
if (!result.IsSuccess)
{
systemLog.LogError($"写入Kuka机器人[{KukaAvar.IpAddress}]_变量地址{item.DataAddress}失败:{result.Message}");
return;
}
}
}
catch (Exception ex)
{
systemLog.LogError($"写入Kuka机器人[{KukaAvar.IpAddress}]数据失败:{ex.Message}");
}
}
public void Write(EquipmentProperty equipmentProperty)
{
Write([equipmentProperty]);
}
}
}