ModbusTcpCommunication.cs
8.41 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
using HHECS.BllModel;
using HHECS.EquipmentModel;
using HslCommunication;
using HslCommunication.ModBus;
namespace HHECS.DAQClient.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<DataItem> dataItems)
{
try
{
foreach (var item in dataItems)
{
switch (item.DataType)
{
case EquipmentDataType.REAL:
var realResult = client.ReadFloat(item.DataAddress);
if (!realResult.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{realResult.Message}");
}
item.Value = realResult.Content.ToString();
break;
case EquipmentDataType.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($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{boolBufferResult.Message}");
}
item.Value = ((boolBufferResult.Content.Max() & (1 << index)) > 0).ToString();
break;
case EquipmentDataType.BYTE:
break;
case EquipmentDataType.WORD:
var int16Result = client.ReadInt16(item.DataAddress);
if (!int16Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{int16Result.Message}");
}
item.Value = int16Result.Content.ToString();
break;
case EquipmentDataType.DWORD:
var uInt16Result = client.ReadUInt16(item.DataAddress);
if (!uInt16Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt16Result.Message}");
}
item.Value = uInt16Result.Content.ToString();
break;
case EquipmentDataType.INT:
var int32Result = client.ReadInt32(item.DataAddress);
if (!int32Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{int32Result.Message}");
}
item.Value = int32Result.Content.ToString();
break;
case EquipmentDataType.DINT:
var uInt32Result = client.ReadUInt32(item.DataAddress);
if (!uInt32Result.IsSuccess)
{
item.Value = string.Empty;
return BllResultFactory.Error($"读取设备[{client.IpAddress}]地址{item.DataAddress}出现异常:{uInt32Result.Message}");
}
item.Value = uInt32Result.Content.ToString();
break;
case EquipmentDataType.CHAR:
break;
default:
break;
}
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"读取设备[{IpAddress}]数据失败:{ex.Message}");
}
}
public BllResult Read(DataItem dataItem)
{
return Read(new List<DataItem> { dataItem });
}
public BllResult Write(IEnumerable<DataItem> dataItems)
{
try
{
var result = new OperateResult();
foreach (var item in dataItems)
{
switch (item.DataType)
{
case EquipmentDataType.REAL:
_ = float.TryParse(item.Value, out var v1);
result = client.Write(item.DataAddress, v1);
break;
case EquipmentDataType.BOOL:
_ = bool.TryParse(item.Value, out var v2);
result = client.Write(item.DataAddress, v2);
break;
case EquipmentDataType.BYTE:
break;
case EquipmentDataType.WORD:
_ = short.TryParse(item.Value, out var v3);
result = client.Write(item.DataAddress, v3);
break;
case EquipmentDataType.DWORD:
_ = ushort.TryParse(item.Value, out var v4);
result = client.Write(item.DataAddress, v4);
break;
case EquipmentDataType.INT:
_ = int.TryParse(item.Value, out var v5);
result = client.Write(item.DataAddress, v5);
break;
case EquipmentDataType.DINT:
_ = uint.TryParse(item.Value, out var v6);
result = client.Write(item.DataAddress, v6);
break;
case EquipmentDataType.CHAR:
break;
default:
break;
}
if (!result.IsSuccess)
{
return BllResultFactory.Success($"写入设备[{IpAddress}]数据失败,{result.Message}");
}
}
return BllResultFactory.Success($"写入设备[{IpAddress}]数据成功,数量:{dataItems.Count()}");
}
catch (Exception ex)
{
return BllResultFactory.Error($"写入设备[{IpAddress}]数据失败:{ex.Message}");
}
}
public BllResult Write(DataItem dataItem)
{
return Write(new List<DataItem> { dataItem });
}
}
}