private bool CheckWriteMultipleResponse(List <byte> response, MbFunctionCode fun, ushort address, int p) { if (response == null) { throw new ArgumentNullException(nameof(response)); } CheckFunction(response, fun); CheckModbusException(response); var responseAddr = (ushort)((response.ElementAt(1) << 8) | response.ElementAt(2)); if (address != responseAddr) { throw new ArgumentException( $"address in response ({responseAddr}) differs from address in request ({address})", nameof(address)); } var responseCount = (ushort)((response.ElementAt(3)) | response.ElementAt(4)); if (p != responseCount) { throw new ArgumentException( $"value in response ({responseCount}) differs from value in request ({p})", nameof(response)); } return(true); }
private bool CheckWriteSingleResponse(List <byte> response, MbFunctionCode fun, ushort address, ushort value) { if (response == null) { throw new ArgumentNullException(nameof(response)); } CheckFunction(response, fun); CheckModbusException(response); var responseAddr = (ushort)((response[1] << 8) | response[2]); if (address != responseAddr) { throw new ArgumentException( $"address in response ({responseAddr}) differs from address in request ({address})", nameof(address)); } var responseVal = (ushort)((response[3]) << 8 | response[4]); if (value != responseVal) { throw new ArgumentException( $"value in response ({responseVal}) differs from value in request ({value})", nameof(value)); } return(true); }
internal static List <byte> CreateReadPdu(MbFunctionCode fun, ushort address, ushort count) { if (count == 0) { throw new ArgumentOutOfRangeException("Cannot read 0 registers", nameof(count)); } if ((fun == MbFunctionCode.ReadCoils || fun == MbFunctionCode.ReadDiscreteInputs) && count > 1968) { throw new ArgumentOutOfRangeException("Cannot read more than 1968 registers in one query", nameof(count)); } if ((fun == MbFunctionCode.ReadHoldingRegisters || fun == MbFunctionCode.ReadInputRegisters) && count > 123) { throw new ArgumentOutOfRangeException("Cannot read more than 123 registers in one query", nameof(count)); } return(new List <byte>(5) { (byte)fun, (byte)(address >> 8), (byte)(address & 0x00ff), (byte)(count >> 8), (byte)(count & 0x00ff) }); }
private static void CheckReadResponsePduHeader(MbFunctionCode fun, int byteCountExpected, List <byte> response) { if (response == null) { throw new ArgumentNullException(nameof(response)); } if (response.Count == 0) { throw new ArgumentException("Won't send empty response", nameof(response)); } CheckFunction(response, fun); CheckModbusException(response); var byteCount = response[1]; if (byteCountExpected != byteCount) { throw new ArgumentException( $"number of bytes in response ({byteCount}) differs from expected ({byteCountExpected})", nameof(response)); } if (byteCount != response.Count - 2) { throw new ArgumentException( $"number of bytes in response ({response.Count - 1}) differs from declared ({byteCount})", nameof(response)); } }
private static void CheckFunction(List <byte> response, MbFunctionCode fun) { if ((response[0] & 0x7f) != (int)fun) { throw new ArgumentException( $"function code in response ({response[0]}) differs from function call ({(int)fun})", nameof(response)); } }
internal List <ushort> UnwrapAnalogPdu(MbFunctionCode fun, ushort count, List <byte> response) { var byteCountExpected = count * 2; CheckReadResponsePduHeader(fun, byteCountExpected, response); var ushorts = response.Skip(2).Chunk(2).Select(xs => (ushort)((xs.ElementAt(0) << 8) | xs.ElementAt(1))).ToList(); return(ushorts); }
internal List <bool> UnwrapDiscretePdu(MbFunctionCode fun, ushort count, List <byte> response) { var byteCountExpected = ((count / 8) + ((count % 8 != 0) ? 1 : 0)); CheckReadResponsePduHeader(fun, byteCountExpected, response); var bools = response.Skip(2).SelectMany(b => Enumerable.Range(0, 8).Select(n => (b & (1 << n)) != 0)).Take(count).ToList(); return(bools); }