ModbusAddress.cs
10.4 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
using HslCommunication.BasicFramework;
using HslCommunication.ModBus;
using System;
using System.Text;
namespace HslCommunication.Core.Address
{
/// <summary>
/// Modbus协议地址格式,可以携带站号,功能码,地址信息
/// </summary>
public class ModbusAddress : DeviceAddressBase
{
#region Constructor
/// <summary>
/// 实例化一个默认的对象
/// </summary>
public ModbusAddress()
{
Station = -1;
Function = -1;
Address = 0;
}
/// <summary>
/// 实例化一个默认的对象,使用默认的地址初始化
/// </summary>
/// <param name="address">传入的地址信息,支持富地址,例如s=2;x=3;100</param>
public ModbusAddress(string address)
{
Station = -1;
Function = -1;
Address = 0;
Parse(address);
}
/// <summary>
/// 实例化一个默认的对象,使用默认的地址初始化
/// </summary>
/// <param name="address">传入的地址信息,支持富地址,例如s=2;x=3;100</param>
/// <param name="function">默认的功能码信息</param>
public ModbusAddress(string address, byte function)
{
Station = -1;
Function = function;
Address = 0;
Parse(address);
}
/// <summary>
/// 实例化一个默认的对象,使用默认的地址初始化
/// </summary>
/// <param name="station">站号信息</param>
/// <param name="function">功能码信息</param>
/// <param name="address">地址信息</param>
public ModbusAddress(byte station, byte function, ushort address)
{
Station = -1;
Function = function;
Address = 0;
}
#endregion
#region Public Properties
/// <summary>
/// 站号信息
/// </summary>
public int Station { get; set; }
/// <summary>
/// 功能码
/// </summary>
public int Function { get; set; }
#endregion
#region Analysis Address
/// <summary>
/// 解析Modbus的地址码
/// </summary>
/// <param name="address">地址数据信息</param>
public override void Parse(string address)
{
if (address.IndexOf(';') < 0)
{
// 正常地址,功能码03
Address = ushort.Parse(address);
}
else
{
// 带功能码的地址
string[] list = address.Split(';');
for (int i = 0; i < list.Length; i++)
{
if (list[i][0] == 's' || list[i][0] == 'S')
{
// 站号信息
this.Station = byte.Parse(list[i].Substring(2));
}
else if (list[i][0] == 'x' || list[i][0] == 'X')
{
this.Function = byte.Parse(list[i].Substring(2));
}
else
{
this.Address = ushort.Parse(list[i]);
}
}
}
}
#endregion
#region Create Read Write Command
/// <summary>
/// 创建一个读取线圈的字节对象
/// </summary>
/// <param name="station">读取的站号</param>
/// <param name="length">读取数据的长度</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateReadCoils(byte station, ushort length)
{
byte[] buffer = new byte[6];
buffer[0] = Station < 0 ? station : (byte)this.Station;
buffer[1] = Function < 0 ? ModbusInfo.ReadCoil : (byte)Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = BitConverter.GetBytes(length)[1];
buffer[5] = BitConverter.GetBytes(length)[0];
return buffer;
}
/// <summary>
/// 创建一个读取离散输入的字节对象
/// </summary>
/// <param name="station">读取的站号</param>
/// <param name="length">读取数据的长度</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateReadDiscrete(byte station, ushort length)
{
byte[] buffer = new byte[6];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = Function < 0 ? ModbusInfo.ReadDiscrete : (byte)Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = BitConverter.GetBytes(length)[1];
buffer[5] = BitConverter.GetBytes(length)[0];
return buffer;
}
/// <summary>
/// 创建一个读取寄存器的字节对象
/// </summary>
/// <param name="station">读取的站号</param>
/// <param name="length">读取数据的长度</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateReadRegister(byte station, ushort length)
{
byte[] buffer = new byte[6];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = this.Function < 1 ? ModbusInfo.ReadRegister : (byte)this.Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = BitConverter.GetBytes(length)[1];
buffer[5] = BitConverter.GetBytes(length)[0];
return buffer;
}
/// <summary>
/// 创建一个写入单个线圈的指令
/// </summary>
/// <param name="station">站号</param>
/// <param name="value">值</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateWriteOneCoil(byte station, bool value)
{
byte[] buffer = new byte[6];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = Function < 0 ? ModbusInfo.WriteOneCoil : (byte)Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = (byte)(value ? 0xFF : 0x00);
buffer[5] = 0x00;
return buffer;
}
/// <summary>
/// 创建一个写入单个寄存器的指令
/// </summary>
/// <param name="station">站号</param>
/// <param name="values">值</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateWriteOneRegister(byte station, byte[] values)
{
byte[] buffer = new byte[6];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = Function < 0 ? ModbusInfo.WriteOneRegister : (byte)Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = values[0];
buffer[5] = values[1];
return buffer;
}
/// <summary>
/// 创建一个写入批量线圈的指令
/// </summary>
/// <param name="station">站号</param>
/// <param name="values">值</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateWriteCoil(byte station, bool[] values)
{
byte[] data = SoftBasic.BoolArrayToByte(values);
byte[] buffer = new byte[7 + data.Length];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = Function < 0 ? ModbusInfo.WriteCoil : (byte)Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = (byte)(values.Length / 256);
buffer[5] = (byte)(values.Length % 256);
buffer[6] = (byte)(data.Length);
data.CopyTo(buffer, 7);
return buffer;
}
/// <summary>
/// 创建一个写入批量寄存器的指令
/// </summary>
/// <param name="station">站号</param>
/// <param name="values">值</param>
/// <returns>原始的modbus指令</returns>
public byte[] CreateWriteRegister(byte station, byte[] values)
{
byte[] buffer = new byte[7 + values.Length];
buffer[0] = this.Station < 0 ? station : (byte)this.Station;
buffer[1] = this.Function < 1 ? ModbusInfo.WriteRegister : (byte)this.Function;
buffer[2] = BitConverter.GetBytes(this.Address)[1];
buffer[3] = BitConverter.GetBytes(this.Address)[0];
buffer[4] = (byte)(values.Length / 2 / 256);
buffer[5] = (byte)(values.Length / 2 % 256);
buffer[6] = (byte)(values.Length);
values.CopyTo(buffer, 7);
return buffer;
}
#endregion
#region Address Operate
/// <summary>
/// 地址新增指定的数
/// </summary>
/// <param name="value">数据值信息</param>
/// <returns>新增后的地址信息</returns>
public ModbusAddress AddressAdd(int value)
{
return new ModbusAddress()
{
Station = this.Station,
Function = this.Function,
Address = (ushort)(this.Address + value),
};
}
/// <summary>
/// 地址新增1
/// </summary>
/// <returns>新增后的地址信息</returns>
public ModbusAddress AddressAdd()
{
return AddressAdd(1);
}
#endregion
#region Object Override
/// <summary>
/// 返回表示当前对象的字符串
/// </summary>
/// <returns>地址表示形式</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (Station >= 0) sb.Append("s=" + Station + ";");
if (Function >= 1) sb.Append("x=" + Function + ";");
sb.Append(Address.ToString());
return sb.ToString();
}
#endregion
}
}