ModbusTcpCommunication.cs
9.21 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
using DataAcquisition.Common.Enums;
using DataAcquisition.Models;
using HHECS.BllModel;
using HslCommunication;
using HslCommunication.Core;
using HslCommunication.ModBus;
namespace DataAcquisition.Common.Communications
{
public class ModbusTcpCommunication : ICommunication
{
public int CommunicationId { get; set; }
public string IpAddress => client.IpAddress;
private readonly ModbusTcpNet client = null!;
private ModbusTcpCommunication() { }
public ModbusTcpCommunication(int communicationId, string ipAddress, int port)
{
CommunicationId = communicationId;
client = new ModbusTcpNet(ipAddress, port);
client.DataFormat = DataFormat.CDAB;
client.SetPersistentConnection();
}
public BllResult ConnectServer()
{
try
{
var result = client.ConnectServer();
if (!result.IsSuccess)
{
return BllResultFactory.Error(result.Message);
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
public BllResult ConnectClose()
{
client.ConnectClose();
return BllResultFactory.Success();
}
public BllResult Read(IEnumerable<EquipmentProperty> equipmentProperties)
{
try
{
foreach (var item in equipmentProperties)
{
switch (item.DataType)
{
case DataTypeConst.Float:
var realResult = client.ReadFloat(item.DataAddress);
if (!realResult.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{realResult.Message}");
}
item.Value = realResult.Content.ToString();
break;
case DataTypeConst.Bool:
var addressArray = item.DataAddress.Split('.');
var address = addressArray[0];
_ = int.TryParse(addressArray.Length >= 2 ? addressArray[1] : "0", out var index);
var boolBufferResult = client.Read(address, 1);
if (!boolBufferResult.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{boolBufferResult.Message}");
}
item.Value = ((boolBufferResult.Content[1] & (1 << index)) > 0).ToString();
break;
case DataTypeConst.Byte:
break;
case DataTypeConst.Int16:
var int16Result = client.ReadInt16(item.DataAddress);
if (!int16Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{int16Result.Message}");
}
item.Value = int16Result.Content.ToString();
break;
case DataTypeConst.UInt16:
var uInt16Result = client.ReadInt16(item.DataAddress);
if (!uInt16Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt16Result.Message}");
}
item.Value = uInt16Result.Content.ToString();
break;
case DataTypeConst.Int32:
var int32Result = client.ReadInt16(item.DataAddress);
if (!int32Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{int32Result.Message}");
}
item.Value = int32Result.Content.ToString();
break;
case DataTypeConst.UInt32:
var uInt32Result = client.ReadInt16(item.DataAddress);
if (!uInt32Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取PLC[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt32Result.Message}");
}
item.Value = uInt32Result.Content.ToString();
break;
case DataTypeConst.Int64:
break;
case DataTypeConst.UInt64:
break;
case DataTypeConst.String:
break;
default:
break;
}
item.UpdateTime = DateTime.Now;
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"读取设备[{IpAddress}]数据失败:{ex.Message}");
}
}
public BllResult Read(EquipmentProperty equipmentProperty)
{
return Read(new List<EquipmentProperty> { equipmentProperty });
}
public BllResult Write(IEnumerable<EquipmentProperty> equipmentProperties)
{
try
{
var result = new OperateResult();
foreach (var item in equipmentProperties)
{
switch (item.DataType)
{
case DataTypeConst.Float:
_ = float.TryParse(item.Value, out var v1);
result = client.Write(item.DataAddress, v1);
break;
case DataTypeConst.Bool:
_ = bool.TryParse(item.Value, out var v2);
result = client.Write(item.DataAddress, v2);
break;
case DataTypeConst.Byte:
break;
case DataTypeConst.Int16:
_ = short.TryParse(item.Value, out var v3);
result = client.Write(item.DataAddress, v3);
break;
case DataTypeConst.UInt16:
_ = ushort.TryParse(item.Value, out var v4);
result = client.Write(item.DataAddress, v4);
break;
case DataTypeConst.Int32:
_ = int.TryParse(item.Value, out var v5);
result = client.Write(item.DataAddress, v5);
break;
case DataTypeConst.UInt32:
_ = uint.TryParse(item.Value, out var v6);
result = client.Write(item.DataAddress, v6);
break;
case DataTypeConst.Int64:
_ = long.TryParse(item.Value, out var v7);
result = client.Write(item.DataAddress, v7);
break;
case DataTypeConst.UInt64:
_ = ulong.TryParse(item.Value, out var v8);
result = client.Write(item.DataAddress, v8);
break;
case DataTypeConst.String:
break;
default:
break;
}
if (!result.IsSuccess)
{
return BllResultFactory.Success($"写入设备[{IpAddress}]数据失败,{result.Message}");
}
}
return BllResultFactory.Success($"写入设备[{IpAddress}]数据成功,数量:{equipmentProperties.Count()}");
}
catch (Exception ex)
{
return BllResultFactory.Error($"写入设备[{IpAddress}]数据失败:{ex.Message}");
}
}
public BllResult Write(EquipmentProperty equipmentProperty)
{
return Write(new List<EquipmentProperty> { equipmentProperty });
}
}
}