Esempio n. 1
0
        public static WriteSingleCoil Create(byte slaveId, ushort startingAddress, ushort data)
        {
            WriteSingleCoil request = new WriteSingleCoil()
            {
                SlaveAddress    = slaveId,
                FunctionCode    = 5,
                StartingAddress = (ushort)(startingAddress),
                Data            = data,
                Protocol        = ProtocolType.RTU
            };

            byte[] encoded = request.Encode();
            return(WriteSingleCoil.Decode(encoded));
        }
Esempio n. 2
0
        public static ModbusMessage Create(byte unitId, ushort transactionId, ushort protocolId, MessageType type, ushort startingAddress, ushort data)
        {
            //5,6
            int num = (int)type;

            if (num == 5)
            {
                return(WriteSingleCoil.Create(unitId, transactionId, protocolId, startingAddress, data));
            }
            if (num == 6)
            {
                return(WriteSingleRegister.Create(unitId, transactionId, protocolId, startingAddress, data));
            }

            throw new ModbusFunctionCodeMismatchException("Message type out of range, not 5 or 6.");
        }
Esempio n. 3
0
        public static WriteSingleCoil Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, ushort data)
        {
            WriteSingleCoil request = new WriteSingleCoil()
            {
                Header = new MbapHeader()
                {
                    ProtocolId = protocolId, TransactionId = transactionId, UnitId = unitId
                },
                SlaveAddress    = unitId,
                FunctionCode    = 5,
                StartingAddress = (ushort)(startingAddress),
                Data            = data,
                Protocol        = ProtocolType.TCP
            };

            byte[] rtuEncoded = request.Encode();
            return(WriteSingleCoil.Decode(rtuEncoded));
        }
Esempio n. 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.");
        }