Renamed array-extension to "BigEndian"

This commit is contained in:
2024-03-26 20:23:35 +01:00
parent d6bc5f1a4a
commit 7d5f3194c2
9 changed files with 67 additions and 67 deletions

View File

@@ -6,23 +6,23 @@ namespace AMWD.Protocols.Modbus.Common
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal static class ArrayExtensions internal static class ArrayExtensions
{ {
public static void SwapNetworkOrder(this byte[] bytes) public static void SwapBigEndian(this byte[] bytes)
{ {
if (BitConverter.IsLittleEndian) if (BitConverter.IsLittleEndian)
Array.Reverse(bytes); Array.Reverse(bytes);
} }
public static ushort NetworkUInt16(this byte[] bytes, int offset = 0) public static ushort GetBigEndianUInt16(this byte[] bytes, int offset = 0)
{ {
byte[] b = bytes.Skip(offset).Take(2).ToArray(); byte[] b = bytes.Skip(offset).Take(2).ToArray();
b.SwapNetworkOrder(); b.SwapBigEndian();
return BitConverter.ToUInt16(b, 0); return BitConverter.ToUInt16(b, 0);
} }
public static byte[] ToNetworkBytes(this ushort value) public static byte[] ToBigEndianBytes(this ushort value)
{ {
byte[] b = BitConverter.GetBytes(value); byte[] b = BitConverter.GetBytes(value);
b.SwapNetworkOrder(); b.SwapBigEndian();
return b; return b;
} }
} }

View File

@@ -49,7 +49,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToSingle(blob, 0); return BitConverter.ToSingle(blob, 0);
} }
@@ -93,7 +93,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToDouble(blob, 0); return BitConverter.ToDouble(blob, 0);
} }
@@ -107,7 +107,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this float value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this float value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)
@@ -135,7 +135,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this double value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this double value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)

View File

@@ -99,7 +99,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToInt32(blob, 0); return BitConverter.ToInt32(blob, 0);
} }
@@ -143,7 +143,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToInt64(blob, 0); return BitConverter.ToInt64(blob, 0);
} }
@@ -171,7 +171,7 @@ namespace AMWD.Protocols.Modbus.Common
public static HoldingRegister ToRegister(this short value, ushort address) public static HoldingRegister ToRegister(this short value, ushort address)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return new HoldingRegister return new HoldingRegister
{ {
@@ -191,7 +191,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this int value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this int value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)
@@ -219,7 +219,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this long value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this long value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)

View File

@@ -99,7 +99,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToUInt32(blob, 0); return BitConverter.ToUInt32(blob, 0);
} }
@@ -143,7 +143,7 @@ namespace AMWD.Protocols.Modbus.Common
blob[i * 2 + 1] = registers[i].LowByte; blob[i * 2 + 1] = registers[i].LowByte;
} }
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToUInt64(blob, 0); return BitConverter.ToUInt64(blob, 0);
} }
@@ -171,7 +171,7 @@ namespace AMWD.Protocols.Modbus.Common
public static HoldingRegister ToRegister(this ushort value, ushort address) public static HoldingRegister ToRegister(this ushort value, ushort address)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return new HoldingRegister return new HoldingRegister
{ {
@@ -191,7 +191,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this uint value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this uint value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)
@@ -219,7 +219,7 @@ namespace AMWD.Protocols.Modbus.Common
public static IEnumerable<HoldingRegister> ToRegister(this ulong value, ushort address, bool reverseRegisterOrder = false) public static IEnumerable<HoldingRegister> ToRegister(this ulong value, ushort address, bool reverseRegisterOrder = false)
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
int numRegisters = blob.Length / 2; int numRegisters = blob.Length / 2;
for (int i = 0; i < numRegisters; i++) for (int i = 0; i < numRegisters; i++)

View File

@@ -18,13 +18,13 @@ namespace AMWD.Protocols.Modbus.Common
get get
{ {
byte[] blob = [HighByte, LowByte]; byte[] blob = [HighByte, LowByte];
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToUInt16(blob, 0); return BitConverter.ToUInt16(blob, 0);
} }
set set
{ {
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
HighByte = blob[0]; HighByte = blob[0];
LowByte = blob[1]; LowByte = blob[1];

View File

@@ -18,7 +18,7 @@ namespace AMWD.Protocols.Modbus.Common
get get
{ {
byte[] blob = [HighByte, LowByte]; byte[] blob = [HighByte, LowByte];
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return BitConverter.ToUInt16(blob, 0); return BitConverter.ToUInt16(blob, 0);
} }
} }

View File

@@ -130,7 +130,7 @@ namespace AMWD.Protocols.Modbus.Common.Models
value = 0x0000; value = 0x0000;
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return new HoldingRegister return new HoldingRegister
{ {
@@ -157,7 +157,7 @@ namespace AMWD.Protocols.Modbus.Common.Models
} }
byte[] blob = [register.HighByte, register.LowByte]; byte[] blob = [register.HighByte, register.LowByte];
blob.SwapNetworkOrder(); blob.SwapBigEndian();
_holdingRegisters[register.Address] = BitConverter.ToUInt16(blob, 0); _holdingRegisters[register.Address] = BitConverter.ToUInt16(blob, 0);
} }
} }
@@ -175,7 +175,7 @@ namespace AMWD.Protocols.Modbus.Common.Models
value = 0x0000; value = 0x0000;
byte[] blob = BitConverter.GetBytes(value); byte[] blob = BitConverter.GetBytes(value);
blob.SwapNetworkOrder(); blob.SwapBigEndian();
return new InputRegister return new InputRegister
{ {
@@ -202,7 +202,7 @@ namespace AMWD.Protocols.Modbus.Common.Models
} }
byte[] blob = [register.HighByte, register.LowByte]; byte[] blob = [register.HighByte, register.LowByte];
blob.SwapNetworkOrder(); blob.SwapBigEndian();
_inputRegisters[register.Address] = BitConverter.ToUInt16(blob, 0); _inputRegisters[register.Address] = BitConverter.ToUInt16(blob, 0);
} }
} }

View File

@@ -98,12 +98,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.ReadCoils; request[7] = (byte)ModbusFunctionCode.ReadCoils;
// Starting address // Starting address
byte[] addrBytes = startAddress.ToNetworkBytes(); byte[] addrBytes = startAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
// Quantity // Quantity
byte[] countBytes = count.ToNetworkBytes(); byte[] countBytes = count.ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -153,12 +153,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.ReadDiscreteInputs; request[7] = (byte)ModbusFunctionCode.ReadDiscreteInputs;
// Starting address // Starting address
byte[] addrBytes = startAddress.ToNetworkBytes(); byte[] addrBytes = startAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
// Quantity // Quantity
byte[] countBytes = count.ToNetworkBytes(); byte[] countBytes = count.ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -208,12 +208,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.ReadHoldingRegisters; request[7] = (byte)ModbusFunctionCode.ReadHoldingRegisters;
// Starting address // Starting address
byte[] addrBytes = startAddress.ToNetworkBytes(); byte[] addrBytes = startAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
// Quantity // Quantity
byte[] countBytes = count.ToNetworkBytes(); byte[] countBytes = count.ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -260,12 +260,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.ReadInputRegisters; request[7] = (byte)ModbusFunctionCode.ReadInputRegisters;
// Starting address // Starting address
byte[] addrBytes = startAddress.ToNetworkBytes(); byte[] addrBytes = startAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
// Quantity // Quantity
byte[] countBytes = count.ToNetworkBytes(); byte[] countBytes = count.ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -371,7 +371,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
// Function code // Function code
request[7] = (byte)ModbusFunctionCode.WriteSingleCoil; request[7] = (byte)ModbusFunctionCode.WriteSingleCoil;
byte[] addrBytes = coil.Address.ToNetworkBytes(); byte[] addrBytes = coil.Address.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
@@ -386,7 +386,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
{ {
return new Coil return new Coil
{ {
Address = response.ToArray().NetworkUInt16(8), Address = response.ToArray().GetBigEndianUInt16(8),
HighByte = response[10], HighByte = response[10],
LowByte = response[11] LowByte = response[11]
}; };
@@ -410,7 +410,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
// Function code // Function code
request[7] = (byte)ModbusFunctionCode.WriteSingleRegister; request[7] = (byte)ModbusFunctionCode.WriteSingleRegister;
byte[] addrBytes = register.Address.ToNetworkBytes(); byte[] addrBytes = register.Address.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
@@ -425,7 +425,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
{ {
return new HoldingRegister return new HoldingRegister
{ {
Address = response.ToArray().NetworkUInt16(8), Address = response.ToArray().GetBigEndianUInt16(8),
HighByte = response[10], HighByte = response[10],
LowByte = response[11] LowByte = response[11]
}; };
@@ -463,11 +463,11 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.WriteMultipleCoils; request[7] = (byte)ModbusFunctionCode.WriteMultipleCoils;
byte[] addrBytes = firstAddress.ToNetworkBytes(); byte[] addrBytes = firstAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
byte[] countBytes = ((ushort)orderedList.Count).ToNetworkBytes(); byte[] countBytes = ((ushort)orderedList.Count).ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -492,8 +492,8 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
/// <inheritdoc/> /// <inheritdoc/>
public (ushort FirstAddress, ushort NumberOfCoils) DeserializeWriteMultipleCoils(IReadOnlyList<byte> response) public (ushort FirstAddress, ushort NumberOfCoils) DeserializeWriteMultipleCoils(IReadOnlyList<byte> response)
{ {
ushort firstAddress = response.ToArray().NetworkUInt16(8); ushort firstAddress = response.ToArray().GetBigEndianUInt16(8);
ushort numberOfCoils = response.ToArray().NetworkUInt16(10); ushort numberOfCoils = response.ToArray().GetBigEndianUInt16(10);
return (firstAddress, numberOfCoils); return (firstAddress, numberOfCoils);
} }
@@ -530,11 +530,11 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
request[7] = (byte)ModbusFunctionCode.WriteMultipleRegisters; request[7] = (byte)ModbusFunctionCode.WriteMultipleRegisters;
byte[] addrBytes = firstAddress.ToNetworkBytes(); byte[] addrBytes = firstAddress.ToBigEndianBytes();
request[8] = addrBytes[0]; request[8] = addrBytes[0];
request[9] = addrBytes[1]; request[9] = addrBytes[1];
byte[] countBytes = ((ushort)orderedList.Count).ToNetworkBytes(); byte[] countBytes = ((ushort)orderedList.Count).ToBigEndianBytes();
request[10] = countBytes[0]; request[10] = countBytes[0];
request[11] = countBytes[1]; request[11] = countBytes[1];
@@ -553,8 +553,8 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
/// <inheritdoc/> /// <inheritdoc/>
public (ushort FirstAddress, ushort NumberOfRegisters) DeserializeWriteMultipleHoldingRegisters(IReadOnlyList<byte> response) public (ushort FirstAddress, ushort NumberOfRegisters) DeserializeWriteMultipleHoldingRegisters(IReadOnlyList<byte> response)
{ {
ushort firstAddress = response.ToArray().NetworkUInt16(8); ushort firstAddress = response.ToArray().GetBigEndianUInt16(8);
ushort numberOfRegisters = response.ToArray().NetworkUInt16(10); ushort numberOfRegisters = response.ToArray().GetBigEndianUInt16(10);
return (firstAddress, numberOfRegisters); return (firstAddress, numberOfRegisters);
} }
@@ -572,7 +572,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
if (responseBytes.Count < 6) if (responseBytes.Count < 6)
return false; return false;
ushort followingBytes = responseBytes.ToArray().NetworkUInt16(4); ushort followingBytes = responseBytes.ToArray().GetBigEndianUInt16(4);
if (responseBytes.Count < followingBytes + 6) if (responseBytes.Count < followingBytes + 6)
return false; return false;
@@ -591,7 +591,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
if (request[2] != response[2] || request[3] != response[3]) if (request[2] != response[2] || request[3] != response[3])
throw new ModbusException("Protocol Identifier does not match."); throw new ModbusException("Protocol Identifier does not match.");
ushort count = response.ToArray().NetworkUInt16(4); ushort count = response.ToArray().GetBigEndianUInt16(4);
if (count != response.Count - 6) if (count != response.Count - 6)
throw new ModbusException("Number of following bytes does not match."); throw new ModbusException("Number of following bytes does not match.");
@@ -645,7 +645,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
// Transaction id // Transaction id
ushort txId = GetNextTransacitonId(); ushort txId = GetNextTransacitonId();
byte[] txBytes = txId.ToNetworkBytes(); byte[] txBytes = txId.ToBigEndianBytes();
header[0] = txBytes[0]; header[0] = txBytes[0];
header[1] = txBytes[1]; header[1] = txBytes[1];
@@ -654,7 +654,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols
header[3] = 0x00; header[3] = 0x00;
// Number of following bytes // Number of following bytes
byte[] countBytes = ((ushort)followingBytes).ToNetworkBytes(); byte[] countBytes = ((ushort)followingBytes).ToBigEndianBytes();
header[4] = countBytes[0]; header[4] = countBytes[0];
header[5] = countBytes[1]; header[5] = countBytes[1];

View File

@@ -256,7 +256,7 @@ namespace AMWD.Protocols.Modbus.Tcp
requestBytes.AddRange(headerBytes); requestBytes.AddRange(headerBytes);
byte[] followingCountBytes = headerBytes.Skip(4).Take(2).ToArray(); byte[] followingCountBytes = headerBytes.Skip(4).Take(2).ToArray();
followingCountBytes.SwapNetworkOrder(); followingCountBytes.SwapBigEndian();
int followingCount = BitConverter.ToUInt16(followingCountBytes, 0); int followingCount = BitConverter.ToUInt16(followingCountBytes, 0);
byte[] bodyBytes = await stream.ReadExpectedBytesAsync(followingCount, cts.Token).ConfigureAwait(false); byte[] bodyBytes = await stream.ReadExpectedBytesAsync(followingCount, cts.Token).ConfigureAwait(false);
@@ -351,8 +351,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT)
{ {
@@ -403,8 +403,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT) if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_DISCRETE_READ_COUNT)
{ {
@@ -455,8 +455,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT)
{ {
@@ -504,8 +504,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT) if (TcpProtocol.MIN_READ_COUNT < count || count < TcpProtocol.MAX_REGISTER_READ_COUNT)
{ {
@@ -553,7 +553,7 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort address = requestBytes.NetworkUInt16(8); ushort address = requestBytes.GetBigEndianUInt16(8);
if (requestBytes[10] != 0x00 && requestBytes[10] != 0xFF) if (requestBytes[10] != 0x00 && requestBytes[10] != 0xFF)
{ {
@@ -608,8 +608,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort address = requestBytes.NetworkUInt16(8); ushort address = requestBytes.GetBigEndianUInt16(8);
ushort value = requestBytes.NetworkUInt16(10); ushort value = requestBytes.GetBigEndianUInt16(10);
try try
{ {
@@ -660,8 +660,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
int byteCount = (int)Math.Ceiling(count / 8.0); int byteCount = (int)Math.Ceiling(count / 8.0);
if (requestBytes.Length < 13 + byteCount) if (requestBytes.Length < 13 + byteCount)
@@ -726,8 +726,8 @@ namespace AMWD.Protocols.Modbus.Tcp
var responseBytes = new List<byte>(); var responseBytes = new List<byte>();
responseBytes.AddRange(requestBytes.Take(8)); responseBytes.AddRange(requestBytes.Take(8));
ushort firstAddress = requestBytes.NetworkUInt16(8); ushort firstAddress = requestBytes.GetBigEndianUInt16(8);
ushort count = requestBytes.NetworkUInt16(10); ushort count = requestBytes.GetBigEndianUInt16(10);
int byteCount = count * 2; int byteCount = count * 2;
if (requestBytes.Length < 13 + byteCount) if (requestBytes.Length < 13 + byteCount)
@@ -760,7 +760,7 @@ namespace AMWD.Protocols.Modbus.Tcp
{ {
UnitId = device.Id, UnitId = device.Id,
Address = address, Address = address,
Value = requestBytes.NetworkUInt16(baseOffset + i * 2), Value = requestBytes.GetBigEndianUInt16(baseOffset + i * 2),
HighByte = requestBytes[baseOffset + i * 2], HighByte = requestBytes[baseOffset + i * 2],
LowByte = requestBytes[baseOffset + i * 2 + 1] LowByte = requestBytes[baseOffset + i * 2 + 1]
}); });