public static ReadInputRegisters Create(byte slaveAddress, ushort startingAddress, ushort quantity)
        {
            ReadInputRegisters request = new ReadInputRegisters()
            {
                SlaveAddress        = slaveAddress,
                FunctionCode        = 4,
                StartingAddress     = (ushort)(startingAddress),
                QuantityOfRegisters = quantity,
                Protocol            = ProtocolType.RTU
            };

            byte[] rtuEncoded = request.Encode();
            return(ReadInputRegisters.Decode(rtuEncoded));
        }
        public static ReadInputRegisters Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, ushort quantity)
        {
            ReadInputRegisters request = new ReadInputRegisters()
            {
                Header = new MbapHeader()
                {
                    ProtocolId = protocolId, TransactionId = transactionId, UnitId = unitId
                },
                SlaveAddress        = unitId,
                FunctionCode        = 4,
                StartingAddress     = (ushort)(startingAddress),
                QuantityOfRegisters = quantity,
                Protocol            = ProtocolType.TCP
            };

            byte[] encoded = request.Encode();
            return(ReadInputRegisters.Decode(encoded));
        }
        private static ModbusMessage GetDecodedMessage(byte code, byte[] message)
        {
            if (code == 1)
            {
                return(ReadCoils.Decode(message));
            }
            if (code == 2)
            {
                return(ReadDiscreteInputs.Decode(message));
            }
            if (code == 3)
            {
                return(ReadHoldingRegisters.Decode(message));
            }
            if (code == 4)
            {
                return(ReadInputRegisters.Decode(message));
            }
            if (code == 5)
            {
                return(WriteSingleCoil.Decode(message));
            }
            if (code == 6)
            {
                return(WriteSingleRegister.Decode(message));
            }
            if (code == 15)
            {
                return(WriteMultipleCoils.Decode(message));
            }
            if (code == 16)
            {
                return(WriteMultipleCoils.Decode(message));
            }

            throw new IndexOutOfRangeException("Function code out of range.");
        }