-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee28971
commit 5dee6ce
Showing
72 changed files
with
5,467 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
NModbus4.IntegrationTests/CustomMessages/CustomReadHoldingRegistersRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using Modbus.Message; | ||
|
||
namespace Modbus.IntegrationTests.CustomMessages | ||
{ | ||
public class CustomReadHoldingRegistersRequest : IModbusMessage | ||
{ | ||
private byte _functionCode; | ||
private byte _slaveAddress; | ||
private ushort _startAddress; | ||
private ushort _numberOfPoints; | ||
private ushort _transactionId; | ||
|
||
public CustomReadHoldingRegistersRequest(byte functionCode, byte slaveAddress, ushort startAddress, ushort numberOfPoints) | ||
{ | ||
_functionCode = functionCode; | ||
_slaveAddress = slaveAddress; | ||
_startAddress = startAddress; | ||
_numberOfPoints = numberOfPoints; | ||
} | ||
|
||
public byte[] MessageFrame | ||
{ | ||
get | ||
{ | ||
List<byte> frame = new List<byte>(); | ||
frame.Add(SlaveAddress); | ||
frame.AddRange(ProtocolDataUnit); | ||
|
||
return frame.ToArray(); | ||
} | ||
} | ||
|
||
public byte[] ProtocolDataUnit | ||
{ | ||
get | ||
{ | ||
List<byte> pdu = new List<byte>(); | ||
|
||
pdu.Add(FunctionCode); | ||
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress))); | ||
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints))); | ||
|
||
return pdu.ToArray(); | ||
} | ||
} | ||
|
||
public ushort TransactionId | ||
{ | ||
get { return _transactionId; } | ||
set { _transactionId = value; } | ||
} | ||
|
||
public byte FunctionCode | ||
{ | ||
get { return _functionCode; } | ||
set { _functionCode = value; } | ||
} | ||
|
||
public byte SlaveAddress | ||
{ | ||
get { return _slaveAddress; } | ||
set { _slaveAddress = value; } | ||
} | ||
|
||
public ushort StartAddress | ||
{ | ||
get { return _startAddress; } | ||
set { _startAddress = value; } | ||
} | ||
|
||
public ushort NumberOfPoints | ||
{ | ||
get { return _numberOfPoints; } | ||
set { _numberOfPoints = value; } | ||
} | ||
|
||
public void Initialize(byte[] frame) | ||
{ | ||
if (frame == null) | ||
{ | ||
throw new ArgumentNullException(nameof(frame)); | ||
} | ||
|
||
if (frame.Length != 6) | ||
{ | ||
throw new ArgumentException("Invalid frame.", nameof(frame)); | ||
} | ||
|
||
SlaveAddress = frame[0]; | ||
FunctionCode = frame[1]; | ||
StartAddress = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 2)); | ||
NumberOfPoints = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 4)); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
NModbus4.IntegrationTests/CustomMessages/CustomReadHoldingRegistersResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Modbus.Data; | ||
using Modbus.Message; | ||
|
||
namespace Modbus.IntegrationTests.CustomMessages | ||
{ | ||
public class CustomReadHoldingRegistersResponse : IModbusMessage | ||
{ | ||
private byte _functionCode; | ||
private byte _slaveAddress; | ||
private byte _byteCount; | ||
private ushort _transactionId; | ||
private RegisterCollection _data; | ||
|
||
public ushort[] Data => _data.ToArray(); | ||
|
||
public byte[] MessageFrame | ||
{ | ||
get | ||
{ | ||
List<byte> frame = new List<byte>(); | ||
frame.Add(SlaveAddress); | ||
frame.AddRange(ProtocolDataUnit); | ||
|
||
return frame.ToArray(); | ||
} | ||
} | ||
|
||
public byte[] ProtocolDataUnit | ||
{ | ||
get | ||
{ | ||
List<byte> pdu = new List<byte>(); | ||
|
||
pdu.Add(_functionCode); | ||
pdu.Add(ByteCount); | ||
pdu.AddRange(_data.NetworkBytes); | ||
|
||
return pdu.ToArray(); | ||
} | ||
} | ||
|
||
public ushort TransactionId | ||
{ | ||
get { return _transactionId; } | ||
set { _transactionId = value; } | ||
} | ||
|
||
public byte FunctionCode | ||
{ | ||
get { return _functionCode; } | ||
set { _functionCode = value; } | ||
} | ||
|
||
public byte SlaveAddress | ||
{ | ||
get { return _slaveAddress; } | ||
set { _slaveAddress = value; } | ||
} | ||
|
||
public byte ByteCount | ||
{ | ||
get { return _byteCount; } | ||
set { _byteCount = value; } | ||
} | ||
|
||
public void Initialize(byte[] frame) | ||
{ | ||
if (frame == null) | ||
{ | ||
throw new ArgumentNullException(nameof(frame)); | ||
} | ||
|
||
if (frame.Length < 3 || frame.Length < 3 + frame[2]) | ||
{ | ||
throw new ArgumentException("Message frame does not contain enough bytes.", nameof(frame)); | ||
} | ||
|
||
SlaveAddress = frame[0]; | ||
FunctionCode = frame[1]; | ||
ByteCount = frame[2]; | ||
_data = new RegisterCollection(frame.Skip(3).Take(ByteCount).ToArray()); | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
NModbus4.IntegrationTests/CustomMessages/CustomWriteMultipleRegistersRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using Modbus.Data; | ||
using Modbus.Message; | ||
|
||
namespace Modbus.IntegrationTests.CustomMessages | ||
{ | ||
public class CustomWriteMultipleRegistersRequest : IModbusMessage | ||
{ | ||
private byte _functionCode; | ||
private byte _slaveAddress; | ||
private byte _byteCount; | ||
private ushort _startAddress; | ||
private ushort _numberOfPoints; | ||
private ushort _transactionId; | ||
private RegisterCollection _data; | ||
|
||
public CustomWriteMultipleRegistersRequest(byte functionCode, byte slaveAddress, ushort startAddress, RegisterCollection data) | ||
{ | ||
_functionCode = functionCode; | ||
_slaveAddress = slaveAddress; | ||
_startAddress = startAddress; | ||
_numberOfPoints = (ushort)data.Count; | ||
_byteCount = data.ByteCount; | ||
_data = data; | ||
} | ||
|
||
public byte[] MessageFrame | ||
{ | ||
get | ||
{ | ||
List<byte> frame = new List<byte>(); | ||
frame.Add(SlaveAddress); | ||
frame.AddRange(ProtocolDataUnit); | ||
|
||
return frame.ToArray(); | ||
} | ||
} | ||
|
||
public byte[] ProtocolDataUnit | ||
{ | ||
get | ||
{ | ||
List<byte> pdu = new List<byte>(); | ||
|
||
pdu.Add(FunctionCode); | ||
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress))); | ||
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints))); | ||
pdu.Add(ByteCount); | ||
pdu.AddRange(Data.NetworkBytes); | ||
|
||
return pdu.ToArray(); | ||
} | ||
} | ||
|
||
public ushort TransactionId | ||
{ | ||
get { return _transactionId; } | ||
set { _transactionId = value; } | ||
} | ||
|
||
public byte FunctionCode | ||
{ | ||
get { return _functionCode; } | ||
set { _functionCode = value; } | ||
} | ||
|
||
public byte SlaveAddress | ||
{ | ||
get { return _slaveAddress; } | ||
set { _slaveAddress = value; } | ||
} | ||
|
||
public ushort StartAddress | ||
{ | ||
get { return _startAddress; } | ||
set { _startAddress = value; } | ||
} | ||
|
||
public ushort NumberOfPoints | ||
{ | ||
get { return _numberOfPoints; } | ||
set { _numberOfPoints = value; } | ||
} | ||
|
||
public byte ByteCount | ||
{ | ||
get { return _byteCount; } | ||
set { _byteCount = value; } | ||
} | ||
|
||
public RegisterCollection Data | ||
{ | ||
get { return _data; } | ||
set { _data = value; } | ||
} | ||
|
||
public void Initialize(byte[] frame) | ||
{ | ||
if (frame == null) | ||
{ | ||
throw new ArgumentNullException(nameof(frame)); | ||
} | ||
|
||
if (frame.Length < 7 || frame.Length < 7 + frame[6]) | ||
{ | ||
throw new FormatException("Message frame does not contain enough bytes."); | ||
} | ||
|
||
SlaveAddress = frame[0]; | ||
FunctionCode = frame[1]; | ||
StartAddress = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 2)); | ||
NumberOfPoints = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 4)); | ||
ByteCount = frame[6]; | ||
Data = new RegisterCollection(frame.Skip(7).Take(ByteCount).ToArray()); | ||
} | ||
} | ||
} |
Oops, something went wrong.