forked from NModbus4/NModbus4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.cs
379 lines (319 loc) · 12.9 KB
/
Driver.cs
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
using System;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
namespace MySample
{
/// <summary>
/// Demonstration of NModbus
/// </summary>
public class Driver
{
private static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
try
{
//ModbusTcpMasterReadInputs();
//SimplePerfTest();
//ModbusSerialRtuMasterWriteRegisters();
//ModbusSerialAsciiMasterReadRegisters();
//ModbusTcpMasterReadInputs();
//StartModbusAsciiSlave();
//ModbusTcpMasterReadInputsFromModbusSlave();
//ModbusSerialAsciiMasterReadRegistersFromModbusSlave();
//StartModbusTcpSlave();
//StartModbusUdpSlave();
//StartModbusAsciiSlave();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
/// <summary>
/// Simple Modbus serial RTU master write holding registers example.
/// </summary>
public static void ModbusSerialRtuMasterWriteRegisters()
{
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
byte slaveId = 1;
ushort startAddress = 100;
ushort[] registers = new ushort[] { 1, 2, 3 };
// write three registers
master.WriteMultipleRegisters(slaveId, startAddress, registers);
}
}
/// <summary>
/// Simple Modbus serial ASCII master read holding registers example.
/// </summary>
public static void ModbusSerialAsciiMasterReadRegisters()
{
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
byte slaveId = 1;
ushort startAddress = 1;
ushort numRegisters = 5;
// read five registers
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
{
Console.WriteLine($"Register {startAddress + i}={registers[i]}");
}
}
// output:
// Register 1=0
// Register 2=0
// Register 3=0
// Register 4=0
// Register 5=0
}
/// <summary>
/// Simple Modbus TCP master read inputs example.
/// </summary>
public static void ModbusTcpMasterReadInputs()
{
using (TcpClient client = new TcpClient("127.0.0.1", 502))
{
ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
// read five input values
ushort startAddress = 100;
ushort numInputs = 5;
bool[] inputs = master.ReadInputs(startAddress, numInputs);
for (int i = 0; i < numInputs; i++)
{
Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
}
}
// output:
// Input 100=0
// Input 101=0
// Input 102=0
// Input 103=0
// Input 104=0
}
/// <summary>
/// Simple Modbus UDP master write coils example.
/// </summary>
public static void ModbusUdpMasterWriteCoils()
{
using (UdpClient client = new UdpClient())
{
IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502);
client.Connect(endPoint);
ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(startAddress, new bool[] { true, false, true });
}
}
/// <summary>
/// Simple Modbus serial ASCII slave example.
/// </summary>
public static void StartModbusSerialAsciiSlave()
{
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
byte unitId = 1;
// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateAscii(unitId, slavePort);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
}
}
/// <summary>
/// Simple Modbus serial RTU slave example.
/// </summary>
public static void StartModbusSerialRtuSlave()
{
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
byte unitId = 1;
// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateRtu(unitId, slavePort);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
}
}
/// <summary>
/// Simple Modbus serial USB ASCII slave example.
/// </summary>
public static void StartModbusSerialUsbAsciiSlave()
{
// TODO
}
/// <summary>
/// Simple Modbus serial USB RTU slave example.
/// </summary>
public static void StartModbusSerialUsbRtuSlave()
{
// TODO
}
/// <summary>
/// Simple Modbus TCP slave example.
/// </summary>
public static void StartModbusTcpSlave()
{
byte slaveId = 1;
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
// create and start the TCP slave
TcpListener slaveTcpListener = new TcpListener(address, port);
slaveTcpListener.Start();
ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
// prevent the main thread from exiting
Thread.Sleep(Timeout.Infinite);
}
/// <summary>
/// Simple Modbus UDP slave example.
/// </summary>
public static void StartModbusUdpSlave()
{
using (UdpClient client = new UdpClient(502))
{
ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(client);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
// prevent the main thread from exiting
Thread.Sleep(Timeout.Infinite);
}
}
/// <summary>
/// Modbus TCP master and slave example.
/// </summary>
public static void ModbusTcpMasterReadInputsFromModbusSlave()
{
byte slaveId = 1;
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
// create and start the TCP slave
TcpListener slaveTcpListener = new TcpListener(address, port);
slaveTcpListener.Start();
ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener);
Thread slaveThread = new Thread(slave.Listen);
slaveThread.Start();
// create the master
TcpClient masterTcpClient = new TcpClient(address.ToString(), port);
ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);
ushort numInputs = 5;
ushort startAddress = 100;
// read five register values
ushort[] inputs = master.ReadInputRegisters(startAddress, numInputs);
for (int i = 0; i < numInputs; i++)
{
Console.WriteLine($"Register {(startAddress + i)}={(inputs[i])}");
}
// clean up
masterTcpClient.Close();
slaveTcpListener.Stop();
// output
// Register 100=0
// Register 101=0
// Register 102=0
// Register 103=0
// Register 104=0
}
/// <summary>
/// Modbus serial ASCII master and slave example.
/// </summary>
public static void ModbusSerialAsciiMasterReadRegistersFromModbusSlave()
{
using (SerialPort masterPort = new SerialPort("COM1"))
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial ports
masterPort.BaudRate = slavePort.BaudRate = 9600;
masterPort.DataBits = slavePort.DataBits = 8;
masterPort.Parity = slavePort.Parity = Parity.None;
masterPort.StopBits = slavePort.StopBits = StopBits.One;
masterPort.Open();
slavePort.Open();
// create modbus slave on seperate thread
byte slaveId = 1;
ModbusSlave slave = ModbusSerialSlave.CreateAscii(slaveId, slavePort);
Thread slaveThread = new Thread(new ThreadStart(slave.Listen));
slaveThread.Start();
// create modbus master
ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterPort);
master.Transport.Retries = 5;
ushort startAddress = 100;
ushort numRegisters = 5;
// read five register values
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
{
Console.WriteLine($"Register {(startAddress + i)}={registers[i]}");
}
}
// output
// Register 100=0
// Register 101=0
// Register 102=0
// Register 103=0
// Register 104=0
}
/// <summary>
/// Write a 32 bit value.
/// </summary>
public static void ReadWrite32BitValue()
{
using (SerialPort port = new SerialPort("COM1"))
{
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// create modbus master
ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
byte slaveId = 1;
ushort startAddress = 1008;
uint largeValue = UInt16.MaxValue + 5;
ushort lowOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 0);
ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 2);
// write large value in two 16 bit chunks
master.WriteMultipleRegisters(slaveId, startAddress, new ushort[] { lowOrderValue, highOrderValue });
// read large value in two 16 bit chunks and perform conversion
ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, 2);
uint value = ModbusUtility.GetUInt32(registers[1], registers[0]);
}
}
}
}