-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathModbusTcpClient.cs
More file actions
682 lines (554 loc) · 18.1 KB
/
ModbusTcpClient.cs
File metadata and controls
682 lines (554 loc) · 18.1 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using Waher.Networking.Modbus.Exceptions;
using Waher.Networking.Sniffers;
using Waher.Runtime.Threading;
using Waher.Security;
namespace Waher.Networking.Modbus
{
/// <summary>
/// Modbus over TCP client
///
/// References:
/// https://waher.se/Downloads/modbus_tcp_specification.pdf
/// https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf
/// </summary>
public class ModbusTcpClient : CommunicationLayer, IDisposable
{
/// <summary>
/// Default Modbus port (502)
/// </summary>
public const int DefaultPort = 502;
private readonly Dictionary<ushort, Transaction> transactions = new Dictionary<ushort, Transaction>();
private readonly BinaryTcpClient tcpClient;
private readonly MultiReadSingleWriteObject synchObject;
private ushort transactionId = 0;
private int timeoutMs = 10000;
private bool connected = false;
private int state = 0;
/// <summary>
/// Modbus over TCP client
/// </summary>
/// <param name="Sniffers">Sniffers</param>
private ModbusTcpClient(params ISniffer[] Sniffers)
: base(false, Sniffers)
{
this.synchObject = new MultiReadSingleWriteObject(this);
this.tcpClient = new BinaryTcpClient(false);
}
/// <summary>
/// Connects to an Modbus TCP/IP Gateway
/// </summary>
/// <param name="Host">Host name or IP Address of gateway.</param>
/// <param name="Port">Port number to connect to.</param>
/// <param name="Sniffers">Sniffers</param>
/// <returns>Connection objcet</returns>
/// <exception cref="Exception">If conncetion could not be established.</exception>
public static async Task<ModbusTcpClient> Connect(string Host, int Port, params ISniffer[] Sniffers)
{
#if !WINDOWS_UWP
return await Connect(Host, Port, false, Sniffers);
}
/// <summary>
/// Connects to an Modbus TCP/IP Gateway
/// </summary>
/// <param name="Host">Host name or IP Address of gateway.</param>
/// <param name="Port">Port number to connect to.</param>
/// <param name="Tls">If connction is protected by TLS.</param>
/// <param name="Sniffers">Sniffers</param>
/// <returns>Connection objcet</returns>
/// <exception cref="Exception">If conncetion could not be established.</exception>
public static async Task<ModbusTcpClient> Connect(string Host, int Port, bool Tls, params ISniffer[] Sniffers)
{
#endif
ModbusTcpClient Result = new ModbusTcpClient(Sniffers);
Result.tcpClient.OnDisconnected += Result.TcpClient_OnDisconnected;
Result.tcpClient.OnError += Result.TcpClient_OnError;
Result.tcpClient.OnInformation += Result.TcpClient_OnInformation;
Result.tcpClient.OnWarning += Result.TcpClient_OnWarning;
Result.tcpClient.OnReceived += Result.TcpClient_OnReceived;
if (!await Result.tcpClient.ConnectAsync(Host, Port))
throw new IOException("Unable to connect to " + Host + ":" + Port);
#if !WINDOWS_UWP
if (Tls)
{
try
{
await Result.tcpClient.UpgradeToTlsAsClient(Crypto.SecureTls);
}
catch (Exception ex)
{
Result.Dispose();
ExceptionDispatchInfo.Capture(ex).Throw();
}
}
#endif
Result.connected = true;
return Result;
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
this.tcpClient?.DisposeWhenDone();
this.synchObject?.Dispose();
Transaction[] ToClose;
lock (this.transactions)
{
if (this.transactions.Count == 0)
ToClose = null;
else
{
ToClose = new Transaction[this.transactions.Count];
this.transactions.Values.CopyTo(ToClose, 0);
this.transactions.Clear();
}
}
if (!(ToClose is null))
{
foreach (Transaction T in ToClose)
T.Handle.TrySetException(new IOException("Connection closed by owner."));
}
}
/// <summary>
/// If the client is currently connected to the Modbus gateway.
/// </summary>
public bool Connected => this.connected;
/// <summary>
/// Timeout, in milliseconds.
/// </summary>
public int TimeoutMs
{
get => this.timeoutMs;
set
{
if (value <= 0)
throw new ArgumentException("Timeout must be positive.", nameof(this.TimeoutMs));
this.timeoutMs = value;
}
}
private Task TcpClient_OnDisconnected(object Sender, EventArgs e)
{
this.connected = false;
return Task.CompletedTask;
}
private Task TcpClient_OnError(object Sender, Exception Exception)
{
this.Error(Exception.Message);
return Task.CompletedTask;
}
private string TcpClient_OnWarning(string Text)
{
this.Warning(Text);
return Text;
}
private string TcpClient_OnInformation(string Text)
{
this.Information(Text);
return Text;
}
private Task<bool> TcpClient_OnReceived(object Sender, bool ConstantBuffer, byte[] Buffer, int Offset, int Count)
{
if (this.HasSniffers)
this.ReceiveBinary(ConstantBuffer, Buffer, Offset, Count);
int i;
byte b;
for (i = 0; i < Count; i++)
{
b = Buffer[Offset++];
switch (this.state)
{
case 0: // Transaction ID (Hi)
this.rxTransactionId = b;
this.state++;
break;
case 1: // Transaction ID (Lo)
this.rxTransactionId <<= 8;
this.rxTransactionId |= b;
this.state++;
break;
case 2: // Protocol (Hi)
this.rxProtocol = b;
this.state++;
break;
case 3: // Protocol (Lo)
this.rxProtocol <<= 8;
this.rxProtocol |= b;
this.state++;
break;
case 4: // Length (Hi)
this.rxLen = b;
this.state++;
break;
case 5: // Length (Lo)
this.rxLen <<= 8;
this.rxLen |= b;
this.state++;
break;
case 6: // Unit Address
this.rxUnitAddress = b;
this.state++;
this.rxLen--;
if (this.rxLen == 0)
this.ProcessIncomingPacket();
break;
case 7: // Expecting function code
this.rxFunctionCode = b;
this.rxLen--;
this.state++;
if (this.rxLen == 0)
this.ProcessIncomingPacket();
break;
case 8: // Data
this.rx.Add(b);
this.rxLen--;
if (this.rxLen == 0)
this.ProcessIncomingPacket();
break;
}
}
return Task.FromResult(true);
}
private void ProcessIncomingPacket()
{
ModbusResponse Response = new ModbusResponse()
{
TransactionId = this.rxTransactionId,
Protocol = this.rxProtocol,
UnitAddress = this.rxUnitAddress,
FunctionCode = this.rxFunctionCode,
Data = this.rx.ToArray()
};
this.state = 0;
this.rx.Clear();
Transaction Request;
lock (this.transactions)
{
if (!this.transactions.TryGetValue(Response.TransactionId, out Request))
return;
this.transactions.Remove(Response.TransactionId);
}
Request.Handle.TrySetResult(Response);
}
private ushort rxTransactionId = 0;
private ushort rxProtocol = 0;
private ushort rxLen = 0;
private byte rxUnitAddress = 0;
private byte rxFunctionCode = 0;
private Transaction lastTransaction = null;
private readonly List<byte> rx = new List<byte>();
private Transaction PrepareRequest(byte UnitAddress, byte FunctionCode, params byte[] Data)
{
Transaction Result;
lock (this.transactions)
{
Result = new Transaction()
{
TransactionId = this.transactionId
};
this.transactions[this.transactionId++] = Result;
}
Result.Request.WriteByte((byte)(Result.TransactionId >> 8));
Result.Request.WriteByte((byte)Result.TransactionId);
Result.Request.WriteByte(0); // Protocol Identitfier (Hi)
Result.Request.WriteByte(0); // Protocol Identitfier (Lo)
int Len = Data?.Length ?? 0;
ushort Length = (ushort)(Len + 2);
Result.Request.WriteByte((byte)(Length >> 8));
Result.Request.WriteByte((byte)Length);
Result.Request.WriteByte(UnitAddress);
Result.Request.WriteByte(FunctionCode);
if (Len > 0)
Result.Request.Write(Data, 0, Len);
this.lastTransaction = Result;
return Result;
}
private class Transaction
{
public ushort TransactionId;
public TaskCompletionSource<ModbusResponse> Handle = new TaskCompletionSource<ModbusResponse>();
public MemoryStream Request = new MemoryStream();
public MemoryStream Response = new MemoryStream();
}
private class ModbusResponse
{
public ushort TransactionId;
public ushort Protocol;
public byte UnitAddress;
public byte FunctionCode;
public byte[] Data;
}
private async Task<ModbusResponse> Request(byte UnitAddress, byte FunctionCode, params byte[] Data)
{
Transaction Req = this.PrepareRequest(UnitAddress, FunctionCode, Data);
try
{
byte[] Bin = Req.Request.ToArray();
await this.tcpClient.SendAsync(true, Bin);
this.TransmitBinary(true, Bin);
Task<ModbusResponse> Result = Req.Handle.Task;
Task Timeout = Task.Delay(this.timeoutMs);
Task T = await Task.WhenAny(Result, Timeout);
if (Result.IsCompleted)
return Result.Result;
else
throw new TimeoutException("No response returned in time.");
}
finally
{
lock (this.transactions)
{
this.transactions.Remove(Req.TransactionId);
}
if (this.lastTransaction == Req)
this.lastTransaction = null;
}
}
private Exception GetException(ModbusResponse Response)
{
if ((Response.FunctionCode & 0x80) != 0 && Response.Data.Length > 0)
{
switch (Response.Data[0])
{
case 0x01: return new IllegalFunctionException(Response.FunctionCode, Response.Data);
case 0x02: return new IllegalDataAddressException(Response.FunctionCode, Response.Data);
case 0x03: return new IllegalDataValueException(Response.FunctionCode, Response.Data);
case 0x04: return new IllegalResponseLengthException(Response.FunctionCode, Response.Data);
case 0x05: return new AcknowledgeException(Response.FunctionCode, Response.Data);
case 0x06: return new SlaveDeviceBusyException(Response.FunctionCode, Response.Data);
case 0x07: return new NegativeAcknowledgeException(Response.FunctionCode, Response.Data);
case 0x08: return new MemoryParityException(Response.FunctionCode, Response.Data);
case 0x0a: return new GatewayPathUnavailableException(Response.FunctionCode, Response.Data);
case 0x0b: return new GatewayTargetDeviceFailedToRespondException(Response.FunctionCode, Response.Data);
}
}
StringBuilder sb = new StringBuilder();
sb.Append("Modbus Exception: ");
sb.Append(Response.FunctionCode.ToString("X2"));
foreach (byte b in Response.Data)
{
sb.Append(' ');
sb.Append(b.ToString("X2"));
}
return new IOException(sb.ToString());
}
/// <summary>
/// Reads multiple registers from a Modbus unit.
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="NrWords">Number of words.</param>
/// <returns>Words read.</returns>
public async Task<ushort[]> ReadMultipleRegisters(byte UnitAddress, ushort ReferenceNumber, ushort NrWords)
{
if (NrWords < 1 || NrWords > 125)
throw new ArgumentOutOfRangeException(nameof(NrWords), "1 <= NrWords <= 125");
ModbusResponse Response = await this.Request(UnitAddress, 0x03,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(NrWords >> 8), (byte)NrWords);
if (Response.FunctionCode != 0x03)
throw this.GetException(Response);
int i = 0;
int c = Response.Data.Length;
if (c == 0)
throw UnexpectedEndOfResponse();
byte ByteCount = Response.Data[i++];
if (i + ByteCount > c)
throw UnexpectedEndOfResponse();
int j = 0;
int d = ByteCount / 2;
ushort[] Words = new ushort[d];
ushort w;
while (j < d)
{
w = Response.Data[i++];
w <<= 8;
w |= Response.Data[i++];
Words[j++] = w;
}
return Words;
}
private static Exception UnexpectedEndOfResponse()
{
return new IOException("Unexpected end of response.");
}
/// <summary>
/// Writes multiple registers to a Modbus unit.
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="Words">Words to write.</param>
public async Task WriteMultipleRegisters(byte UnitAddress, ushort ReferenceNumber, params ushort[] Words)
{
int WordCount = Words.Length;
if (WordCount <= 0 || WordCount > 100)
throw new ArgumentOutOfRangeException(nameof(Words), "Can only write 1-100 words at a time.");
MemoryStream Data = new MemoryStream();
int i;
Data.WriteByte((byte)(ReferenceNumber >> 8));
Data.WriteByte((byte)ReferenceNumber);
Data.WriteByte((byte)(WordCount >> 8));
Data.WriteByte((byte)WordCount);
Data.WriteByte((byte)(WordCount << 1));
for (i = 0; i < WordCount; i++)
{
Data.WriteByte((byte)(Words[i] >> 8));
Data.WriteByte((byte)Words[i]);
}
ModbusResponse Response = await this.Request(UnitAddress, 0x10, Data.ToArray());
if (Response.FunctionCode != 0x10)
throw this.GetException(Response);
}
/// <summary>
/// Reads coils from a Modbus unit.
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="NrBits">Number of bits.</param>
/// <returns>Coils read.</returns>
public async Task<BitArray> ReadCoils(byte UnitAddress, ushort ReferenceNumber, ushort NrBits)
{
if (NrBits < 1 || NrBits > 2000)
throw new ArgumentOutOfRangeException(nameof(NrBits), "1 <= NrBits <= 2000");
ModbusResponse Response = await this.Request(UnitAddress, 0x01,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(NrBits >> 8), (byte)NrBits);
if (Response.FunctionCode != 0x01)
throw this.GetException(Response);
int i = 0;
int c = Response.Data.Length;
if (c == 0)
throw UnexpectedEndOfResponse();
byte ByteCount = Response.Data[i++];
if (i + ByteCount > c)
throw UnexpectedEndOfResponse();
byte[] Bytes = new byte[ByteCount];
Buffer.BlockCopy(Response.Data, i, Bytes, 0, ByteCount);
return new BitArray(Bytes);
}
/// <summary>
/// Reads input discretes from a Modbus unit.
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="NrBits">Number of bits.</param>
/// <returns>Input discretes read.</returns>
public async Task<BitArray> ReadInputDiscretes(byte UnitAddress, ushort ReferenceNumber, ushort NrBits)
{
if (NrBits < 1 || NrBits > 2000)
throw new ArgumentOutOfRangeException(nameof(NrBits), "1 <= NrBits <= 2000");
ModbusResponse Response = await this.Request(UnitAddress, 0x02,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(NrBits >> 8), (byte)NrBits);
if (Response.FunctionCode != 0x02)
throw this.GetException(Response);
int i = 0;
int c = Response.Data.Length;
if (c == 0)
throw UnexpectedEndOfResponse();
byte ByteCount = Response.Data[i++];
if (i + ByteCount > c)
throw UnexpectedEndOfResponse();
byte[] Bytes = new byte[ByteCount];
Buffer.BlockCopy(Response.Data, i, Bytes, 0, ByteCount);
return new BitArray(Bytes);
}
/// <summary>
/// Reads input registers from a Modbus unit.
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="NrWords">Number of words.</param>
/// <returns>Words read.</returns>
public async Task<ushort[]> ReadInputRegisters(byte UnitAddress, ushort ReferenceNumber, ushort NrWords)
{
if (NrWords < 1 || NrWords > 125)
throw new ArgumentOutOfRangeException(nameof(NrWords), "1 <= NrWords <= 125");
ModbusResponse Response = await this.Request(UnitAddress, 0x04,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(NrWords >> 8), (byte)NrWords);
if (Response.FunctionCode != 0x04)
throw this.GetException(Response);
int i = 0;
int c = Response.Data.Length;
if (c == 0)
throw UnexpectedEndOfResponse();
byte ByteCount = Response.Data[i++];
if (i + ByteCount > c)
throw UnexpectedEndOfResponse();
int j = 0;
int d = ByteCount / 2;
ushort[] Words = new ushort[d];
ushort w;
while (j < d)
{
w = Response.Data[i++];
w <<= 8;
w |= Response.Data[i++];
Words[j++] = w;
}
return Words;
}
/// <summary>
/// Write to a single coil
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="Value">Value to write.</param>
/// <returns>Coil output.</returns>
public async Task<bool> WriteCoil(byte UnitAddress, ushort ReferenceNumber, bool Value)
{
ModbusResponse Response = await this.Request(UnitAddress, 0x05,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(Value ? 0xff : 0x00), 0x00);
if (Response.FunctionCode != 0x05)
throw this.GetException(Response);
int c = Response.Data.Length;
if (c < 3)
throw UnexpectedEndOfResponse();
return Response.Data[2] != 0;
}
/// <summary>
/// Write to a single register
/// </summary>
/// <param name="UnitAddress">Unit Address</param>
/// <param name="ReferenceNumber">Reference Number</param>
/// <param name="Value">Value to write.</param>
/// <returns>Register value.</returns>
public async Task<ushort> WriteRegister(byte UnitAddress, ushort ReferenceNumber, ushort Value)
{
ModbusResponse Response = await this.Request(UnitAddress, 0x06,
(byte)(ReferenceNumber >> 8), (byte)ReferenceNumber,
(byte)(Value >> 8), (byte)Value);
if (Response.FunctionCode != 0x06)
throw this.GetException(Response);
int c = Response.Data.Length;
if (c < 4)
throw UnexpectedEndOfResponse();
Value = Response.Data[2];
Value <<= 8;
Value |= Response.Data[3];
return Value;
}
/// <summary>
/// Enters unique access to the TCP client. Must be followed by exactly one <see cref="Leave"/> call.
/// </summary>
public Task Enter()
{
return this.synchObject.BeginWrite();
}
/// <summary>
/// Leaves unique access to the TCP client. Must be called exactly one for each call to <see cref="Enter"/>.
/// </summary>
public Task Leave()
{
return this.synchObject.EndWrite();
}
}
}