internal static RTUResponse CreateNew(Memory <byte> message, RTURequest request) { if (message.Length < 2) { throw new RTUException("The RTU Response Message Length was too short"); } RTUResponse response = new RTUResponse(); byte[] command = message.Slice(0, 2).ToArray(); if (ValidateFunctionCode(command[0]) == false) { throw new RTUException("Invalid Function Code '" + command[0].ToString() + "'"); } response.FunctionCode = command[0]; throwIfResponseError(command[0], command[1]); if (response.FunctionCode != request.FunctionCode) { throw new RTUException("Unexpected Function Code '" + Enum.GetName(typeof(enFunctionCode), response.FunctionCode) + "' - Expecting '" + Enum.GetName(typeof(enFunctionCode), request.FunctionCode) + "'"); } response.Data = message.Length > 1 ? message.Slice(1, message.Length - 1).ToArray() : new byte[0]; return(response); }
internal static void Validate(WriteHoldingRegistersRequest request, RTUResponse response) { if (response.Data.Length < 4) { throw new RTUException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '4'"); } Memory <byte> bytes = response.Data.AsMemory(); Span <byte> addressBytes = bytes.Slice(0, 2).Span; addressBytes.Reverse(); ushort address = BitConverter.ToUInt16(addressBytes); if (address != request.Address) { throw new RTUException("The Response Address of '" + address.ToString() + "' did not match the Expected Address '" + request.Address.ToString() + "'"); } Span <byte> lengthBytes = bytes.Slice(2, 2).Span; lengthBytes.Reverse(); ushort length = BitConverter.ToUInt16(lengthBytes); if (length != request.Values.Length) { throw new RTUException("The Response Values Length of '" + length.ToString() + "' did not match the Expected Values Length '" + request.Values.Length.ToString() + "'"); } }
internal static void Validate(WriteHoldingCoilRequest request, RTUResponse response) { if (response.Data.Length < 4) { throw new RTUException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '4'"); } Memory <byte> bytes = response.Data.AsMemory(); Span <byte> addressBytes = bytes.Slice(0, 2).Span; addressBytes.Reverse(); ushort address = BitConverter.ToUInt16(addressBytes); if (address != request.Address) { throw new RTUException("The Response Address of '" + address.ToString() + "' did not match the Expected Address '" + request.Address.ToString() + "'"); } Span <byte> valueBytes = bytes.Slice(2, 2).Span; valueBytes.Reverse(); ushort value = BitConverter.ToUInt16(valueBytes); ushort expectedValue = (ushort)(request.Value ? 0xFF00 : 0x0000); if (value != expectedValue) { throw new RTUException("The Response Coil Value of '" + value.ToString() + "' did not match the Expected Coil Value '" + expectedValue.ToString() + "'"); } }
internal static bool[] ExtractValues(ReadHoldingCoilsRequest request, RTUResponse response) { if (response.Data.Length < 1) { throw new RTUException("The Response Data Length was too short to extract the Byte Count"); } int expectedByteCount = request.Length > 0 ? request.Length / 8 : 0; if (request.Length % 8 != 0) { expectedByteCount += 1; } if (response.Data[0] != expectedByteCount) { throw new RTUException("The Response Byte Count '" + response.Data[0].ToString() + "' did not match the Expected Byte Count '" + expectedByteCount.ToString() + "'"); } if (response.Data.Length < expectedByteCount + 1) { throw new RTUException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (expectedByteCount + 1).ToString() + "'"); } if (request.Length == 0) { return(new bool[0]); } BitArray bitArray = new BitArray(response.Data.Skip(1).ToArray()); bool[] values = new bool[request.Length]; for (int i = 0; i < values.Length; i++) { values[i] = bitArray.Get(i); } return(values); }
internal static short[] ExtractValues(ReadHoldingRegistersRequest request, RTUResponse response) { if (response.Data.Length < 1) { throw new RTUException("The Response Data Length was too short to extract the Byte Count"); } int expectedByteCount = request.Length * 2; if (response.Data[0] != expectedByteCount) { throw new RTUException("The Response Byte Count '" + response.Data[0].ToString() + "' did not match the Expected Byte Count '" + expectedByteCount.ToString() + "'"); } if (response.Data.Length < expectedByteCount + 1) { throw new RTUException("The Response Data Length of '" + response.Data.Length.ToString() + "' was too short - Expecting a Length of '" + (expectedByteCount + 1).ToString() + "'"); } if (request.Length == 0) { return(new short[0]); } Memory <byte> bytes = response.Data.AsMemory().Slice(1, expectedByteCount); List <short> values = new List <short>(); for (int i = 0; i < bytes.Length; i += 2) { Span <byte> valueBytes = bytes.Slice(i, 2).Span; valueBytes.Reverse(); values.Add(BitConverter.ToInt16(valueBytes)); } return(values.ToArray()); }