コード例 #1
0
ファイル: ReadDiscreteInputs.cs プロジェクト: skunklab/modbus
        public static ReadDiscreteInputs Create(byte slaveId, ushort startingAddress, ushort quantity)
        {
            ReadDiscreteInputs request = new ReadDiscreteInputs()
            {
                SlaveAddress     = slaveId,
                FunctionCode     = 2,
                StartingAddress  = (ushort)(startingAddress),
                QuantityOfInputs = quantity,
                Protocol         = ProtocolType.RTU
            };

            byte[] encoded = request.Encode();
            return(ReadDiscreteInputs.Decode(encoded));
        }
コード例 #2
0
ファイル: ReadDiscreteInputs.cs プロジェクト: skunklab/modbus
        public static ReadDiscreteInputs Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, ushort quantity)
        {
            ReadDiscreteInputs request = new ReadDiscreteInputs()
            {
                Header = new MbapHeader()
                {
                    ProtocolId = protocolId, TransactionId = transactionId, UnitId = unitId
                },
                SlaveAddress     = unitId,
                FunctionCode     = 2,
                StartingAddress  = (ushort)(startingAddress),
                QuantityOfInputs = quantity,
                Protocol         = ProtocolType.TCP
            };

            byte[] rtuEncoded = request.Encode();
            return(ReadDiscreteInputs.Decode(rtuEncoded));
        }
コード例 #3
0
        public static ModbusMessage Create(MessageType type, byte unitId, ushort transactionId, ushort protocolId, byte slaveId, ushort startingAddress, ushort quantity)
        {
            int num = (int)type;

            if (num == 1)
            {
                return(ReadCoils.Create(unitId, transactionId, protocolId, startingAddress, quantity));
            }
            if (num == 2)
            {
                return(ReadDiscreteInputs.Create(unitId, transactionId, protocolId, startingAddress, quantity));
            }
            if (num == 3)
            {
                return(ReadHoldingRegisters.Create(unitId, transactionId, protocolId, startingAddress, quantity));
            }
            if (num == 4)
            {
                return(ReadInputRegisters.Create(unitId, transactionId, protocolId, startingAddress, quantity));
            }

            throw new ModbusFunctionCodeMismatchException("Message type out of range, not 1-4.");
        }
コード例 #4
0
        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.");
        }