/// <summary> /// Gets expected response size for request in bytes /// </summary> /// <param name="request">Request</param> /// <returns>Expected response size for request in bytes. /// Returns 0 if no answer required. /// Returns MaxPacketSize if size of answer is not predicatble. /// </returns> internal static int GetResponseSize(Packet request) { int result; Converter<byte[], bool> check; if (ResponseSizeCheckData.TryGetValue(request.FunctionNumber, out check)) { if (check(request._packetBytes)) { if (request.Device == 0) result = 0; else { Converter<byte[], int> convert; if (ResponseSizeConvertData.TryGetValue(request.FunctionNumber, out convert)) result = convert(request._packetBytes); else throw new NotImplementedException(); } } else throw new ArgumentException(nameof(request)); } else throw new ArgumentException(nameof(request)); return result; }
/// <summary> /// Creates custom modbus packet /// </summary> /// <remarks>No checks</remarks> /// <param name="device">Slave address</param> /// <param name="function">Function number</param> /// <param name="data">Function data</param> /// <returns>Modbus packet</returns> public static Packet CreateCustomPacket(byte device, byte function, IEnumerable<byte> data) { if (data == null) throw new ArgumentNullException(nameof(data)); var result = new Packet(data.Count() + 2) { Device = device, FunctionNumber = (byte)function }; result.CopyData(data); return result; }