diff --git a/AMWD.Protocols.Modbus.Common/Extensions/ArrayExtensions.cs b/AMWD.Protocols.Modbus.Common/Extensions/ArrayExtensions.cs index d3e0f69..78623d7 100644 --- a/AMWD.Protocols.Modbus.Common/Extensions/ArrayExtensions.cs +++ b/AMWD.Protocols.Modbus.Common/Extensions/ArrayExtensions.cs @@ -6,23 +6,23 @@ namespace AMWD.Protocols.Modbus.Common [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static class ArrayExtensions { - public static void SwapNetworkOrder(this byte[] bytes) + public static void SwapBigEndian(this byte[] bytes) { if (BitConverter.IsLittleEndian) 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(); - b.SwapNetworkOrder(); + b.SwapBigEndian(); return BitConverter.ToUInt16(b, 0); } - public static byte[] ToNetworkBytes(this ushort value) + public static byte[] ToBigEndianBytes(this ushort value) { byte[] b = BitConverter.GetBytes(value); - b.SwapNetworkOrder(); + b.SwapBigEndian(); return b; } } diff --git a/AMWD.Protocols.Modbus.Common/Extensions/ModbusDecimalExtensions.cs b/AMWD.Protocols.Modbus.Common/Extensions/ModbusDecimalExtensions.cs index 19cac97..7ea8865 100644 --- a/AMWD.Protocols.Modbus.Common/Extensions/ModbusDecimalExtensions.cs +++ b/AMWD.Protocols.Modbus.Common/Extensions/ModbusDecimalExtensions.cs @@ -49,7 +49,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToSingle(blob, 0); } @@ -93,7 +93,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToDouble(blob, 0); } @@ -107,7 +107,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this float value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) @@ -135,7 +135,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this double value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) diff --git a/AMWD.Protocols.Modbus.Common/Extensions/ModbusSignedExtensions.cs b/AMWD.Protocols.Modbus.Common/Extensions/ModbusSignedExtensions.cs index d298d35..6621ddc 100644 --- a/AMWD.Protocols.Modbus.Common/Extensions/ModbusSignedExtensions.cs +++ b/AMWD.Protocols.Modbus.Common/Extensions/ModbusSignedExtensions.cs @@ -99,7 +99,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToInt32(blob, 0); } @@ -143,7 +143,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToInt64(blob, 0); } @@ -171,7 +171,7 @@ namespace AMWD.Protocols.Modbus.Common public static HoldingRegister ToRegister(this short value, ushort address) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return new HoldingRegister { @@ -191,7 +191,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this int value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) @@ -219,7 +219,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this long value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) diff --git a/AMWD.Protocols.Modbus.Common/Extensions/ModbusUnsignedExtensions.cs b/AMWD.Protocols.Modbus.Common/Extensions/ModbusUnsignedExtensions.cs index 72ce0bf..235007d 100644 --- a/AMWD.Protocols.Modbus.Common/Extensions/ModbusUnsignedExtensions.cs +++ b/AMWD.Protocols.Modbus.Common/Extensions/ModbusUnsignedExtensions.cs @@ -99,7 +99,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToUInt32(blob, 0); } @@ -143,7 +143,7 @@ namespace AMWD.Protocols.Modbus.Common blob[i * 2 + 1] = registers[i].LowByte; } - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToUInt64(blob, 0); } @@ -171,7 +171,7 @@ namespace AMWD.Protocols.Modbus.Common public static HoldingRegister ToRegister(this ushort value, ushort address) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return new HoldingRegister { @@ -191,7 +191,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this uint value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) @@ -219,7 +219,7 @@ namespace AMWD.Protocols.Modbus.Common public static IEnumerable ToRegister(this ulong value, ushort address, bool reverseRegisterOrder = false) { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); int numRegisters = blob.Length / 2; for (int i = 0; i < numRegisters; i++) diff --git a/AMWD.Protocols.Modbus.Common/Models/HoldingRegister.cs b/AMWD.Protocols.Modbus.Common/Models/HoldingRegister.cs index a95e375..67f9329 100644 --- a/AMWD.Protocols.Modbus.Common/Models/HoldingRegister.cs +++ b/AMWD.Protocols.Modbus.Common/Models/HoldingRegister.cs @@ -18,13 +18,13 @@ namespace AMWD.Protocols.Modbus.Common get { byte[] blob = [HighByte, LowByte]; - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToUInt16(blob, 0); } set { byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); HighByte = blob[0]; LowByte = blob[1]; diff --git a/AMWD.Protocols.Modbus.Common/Models/InputRegister.cs b/AMWD.Protocols.Modbus.Common/Models/InputRegister.cs index f967759..8b2a186 100644 --- a/AMWD.Protocols.Modbus.Common/Models/InputRegister.cs +++ b/AMWD.Protocols.Modbus.Common/Models/InputRegister.cs @@ -18,7 +18,7 @@ namespace AMWD.Protocols.Modbus.Common get { byte[] blob = [HighByte, LowByte]; - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return BitConverter.ToUInt16(blob, 0); } } diff --git a/AMWD.Protocols.Modbus.Common/Models/ModbusDevice.cs b/AMWD.Protocols.Modbus.Common/Models/ModbusDevice.cs index b7b23a2..97fc22e 100644 --- a/AMWD.Protocols.Modbus.Common/Models/ModbusDevice.cs +++ b/AMWD.Protocols.Modbus.Common/Models/ModbusDevice.cs @@ -130,7 +130,7 @@ namespace AMWD.Protocols.Modbus.Common.Models value = 0x0000; byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return new HoldingRegister { @@ -157,7 +157,7 @@ namespace AMWD.Protocols.Modbus.Common.Models } byte[] blob = [register.HighByte, register.LowByte]; - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); _holdingRegisters[register.Address] = BitConverter.ToUInt16(blob, 0); } } @@ -175,7 +175,7 @@ namespace AMWD.Protocols.Modbus.Common.Models value = 0x0000; byte[] blob = BitConverter.GetBytes(value); - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); return new InputRegister { @@ -202,7 +202,7 @@ namespace AMWD.Protocols.Modbus.Common.Models } byte[] blob = [register.HighByte, register.LowByte]; - blob.SwapNetworkOrder(); + blob.SwapBigEndian(); _inputRegisters[register.Address] = BitConverter.ToUInt16(blob, 0); } } diff --git a/AMWD.Protocols.Modbus.Common/Protocols/TcpProtocol.cs b/AMWD.Protocols.Modbus.Common/Protocols/TcpProtocol.cs index c87480c..e5d52d5 100644 --- a/AMWD.Protocols.Modbus.Common/Protocols/TcpProtocol.cs +++ b/AMWD.Protocols.Modbus.Common/Protocols/TcpProtocol.cs @@ -98,12 +98,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.ReadCoils; // Starting address - byte[] addrBytes = startAddress.ToNetworkBytes(); + byte[] addrBytes = startAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; // Quantity - byte[] countBytes = count.ToNetworkBytes(); + byte[] countBytes = count.ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -153,12 +153,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.ReadDiscreteInputs; // Starting address - byte[] addrBytes = startAddress.ToNetworkBytes(); + byte[] addrBytes = startAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; // Quantity - byte[] countBytes = count.ToNetworkBytes(); + byte[] countBytes = count.ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -208,12 +208,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.ReadHoldingRegisters; // Starting address - byte[] addrBytes = startAddress.ToNetworkBytes(); + byte[] addrBytes = startAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; // Quantity - byte[] countBytes = count.ToNetworkBytes(); + byte[] countBytes = count.ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -260,12 +260,12 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.ReadInputRegisters; // Starting address - byte[] addrBytes = startAddress.ToNetworkBytes(); + byte[] addrBytes = startAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; // Quantity - byte[] countBytes = count.ToNetworkBytes(); + byte[] countBytes = count.ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -371,7 +371,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols // Function code request[7] = (byte)ModbusFunctionCode.WriteSingleCoil; - byte[] addrBytes = coil.Address.ToNetworkBytes(); + byte[] addrBytes = coil.Address.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; @@ -386,7 +386,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols { return new Coil { - Address = response.ToArray().NetworkUInt16(8), + Address = response.ToArray().GetBigEndianUInt16(8), HighByte = response[10], LowByte = response[11] }; @@ -410,7 +410,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols // Function code request[7] = (byte)ModbusFunctionCode.WriteSingleRegister; - byte[] addrBytes = register.Address.ToNetworkBytes(); + byte[] addrBytes = register.Address.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; @@ -425,7 +425,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols { return new HoldingRegister { - Address = response.ToArray().NetworkUInt16(8), + Address = response.ToArray().GetBigEndianUInt16(8), HighByte = response[10], LowByte = response[11] }; @@ -463,11 +463,11 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.WriteMultipleCoils; - byte[] addrBytes = firstAddress.ToNetworkBytes(); + byte[] addrBytes = firstAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; - byte[] countBytes = ((ushort)orderedList.Count).ToNetworkBytes(); + byte[] countBytes = ((ushort)orderedList.Count).ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -492,8 +492,8 @@ namespace AMWD.Protocols.Modbus.Common.Protocols /// public (ushort FirstAddress, ushort NumberOfCoils) DeserializeWriteMultipleCoils(IReadOnlyList response) { - ushort firstAddress = response.ToArray().NetworkUInt16(8); - ushort numberOfCoils = response.ToArray().NetworkUInt16(10); + ushort firstAddress = response.ToArray().GetBigEndianUInt16(8); + ushort numberOfCoils = response.ToArray().GetBigEndianUInt16(10); return (firstAddress, numberOfCoils); } @@ -530,11 +530,11 @@ namespace AMWD.Protocols.Modbus.Common.Protocols request[7] = (byte)ModbusFunctionCode.WriteMultipleRegisters; - byte[] addrBytes = firstAddress.ToNetworkBytes(); + byte[] addrBytes = firstAddress.ToBigEndianBytes(); request[8] = addrBytes[0]; request[9] = addrBytes[1]; - byte[] countBytes = ((ushort)orderedList.Count).ToNetworkBytes(); + byte[] countBytes = ((ushort)orderedList.Count).ToBigEndianBytes(); request[10] = countBytes[0]; request[11] = countBytes[1]; @@ -553,8 +553,8 @@ namespace AMWD.Protocols.Modbus.Common.Protocols /// public (ushort FirstAddress, ushort NumberOfRegisters) DeserializeWriteMultipleHoldingRegisters(IReadOnlyList response) { - ushort firstAddress = response.ToArray().NetworkUInt16(8); - ushort numberOfRegisters = response.ToArray().NetworkUInt16(10); + ushort firstAddress = response.ToArray().GetBigEndianUInt16(8); + ushort numberOfRegisters = response.ToArray().GetBigEndianUInt16(10); return (firstAddress, numberOfRegisters); } @@ -572,7 +572,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols if (responseBytes.Count < 6) return false; - ushort followingBytes = responseBytes.ToArray().NetworkUInt16(4); + ushort followingBytes = responseBytes.ToArray().GetBigEndianUInt16(4); if (responseBytes.Count < followingBytes + 6) return false; @@ -591,7 +591,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols if (request[2] != response[2] || request[3] != response[3]) 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) throw new ModbusException("Number of following bytes does not match."); @@ -645,7 +645,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols // Transaction id ushort txId = GetNextTransacitonId(); - byte[] txBytes = txId.ToNetworkBytes(); + byte[] txBytes = txId.ToBigEndianBytes(); header[0] = txBytes[0]; header[1] = txBytes[1]; @@ -654,7 +654,7 @@ namespace AMWD.Protocols.Modbus.Common.Protocols header[3] = 0x00; // Number of following bytes - byte[] countBytes = ((ushort)followingBytes).ToNetworkBytes(); + byte[] countBytes = ((ushort)followingBytes).ToBigEndianBytes(); header[4] = countBytes[0]; header[5] = countBytes[1]; diff --git a/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs b/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs index 8cc66a1..5ddca31 100644 --- a/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs +++ b/AMWD.Protocols.Modbus.Tcp/ModbusTcpServer.cs @@ -256,7 +256,7 @@ namespace AMWD.Protocols.Modbus.Tcp requestBytes.AddRange(headerBytes); byte[] followingCountBytes = headerBytes.Skip(4).Take(2).ToArray(); - followingCountBytes.SwapNetworkOrder(); + followingCountBytes.SwapBigEndian(); int followingCount = BitConverter.ToUInt16(followingCountBytes, 0); byte[] bodyBytes = await stream.ReadExpectedBytesAsync(followingCount, cts.Token).ConfigureAwait(false); @@ -351,8 +351,8 @@ namespace AMWD.Protocols.Modbus.Tcp var responseBytes = new List(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); 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(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); 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(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); 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(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); 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(); responseBytes.AddRange(requestBytes.Take(8)); - ushort address = requestBytes.NetworkUInt16(8); + ushort address = requestBytes.GetBigEndianUInt16(8); if (requestBytes[10] != 0x00 && requestBytes[10] != 0xFF) { @@ -608,8 +608,8 @@ namespace AMWD.Protocols.Modbus.Tcp var responseBytes = new List(); responseBytes.AddRange(requestBytes.Take(8)); - ushort address = requestBytes.NetworkUInt16(8); - ushort value = requestBytes.NetworkUInt16(10); + ushort address = requestBytes.GetBigEndianUInt16(8); + ushort value = requestBytes.GetBigEndianUInt16(10); try { @@ -660,8 +660,8 @@ namespace AMWD.Protocols.Modbus.Tcp var responseBytes = new List(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); int byteCount = (int)Math.Ceiling(count / 8.0); if (requestBytes.Length < 13 + byteCount) @@ -726,8 +726,8 @@ namespace AMWD.Protocols.Modbus.Tcp var responseBytes = new List(); responseBytes.AddRange(requestBytes.Take(8)); - ushort firstAddress = requestBytes.NetworkUInt16(8); - ushort count = requestBytes.NetworkUInt16(10); + ushort firstAddress = requestBytes.GetBigEndianUInt16(8); + ushort count = requestBytes.GetBigEndianUInt16(10); int byteCount = count * 2; if (requestBytes.Length < 13 + byteCount) @@ -760,7 +760,7 @@ namespace AMWD.Protocols.Modbus.Tcp { UnitId = device.Id, Address = address, - Value = requestBytes.NetworkUInt16(baseOffset + i * 2), + Value = requestBytes.GetBigEndianUInt16(baseOffset + i * 2), HighByte = requestBytes[baseOffset + i * 2], LowByte = requestBytes[baseOffset + i * 2 + 1] });