Example #1
0
        public static WriteSingleRegister Create(byte slaveId, ushort startingAddress, ushort data)
        {
            WriteSingleRegister request = new WriteSingleRegister()
            {
                SlaveAddress    = slaveId,
                FunctionCode    = 6,
                StartingAddress = (ushort)(startingAddress),
                Data            = data,
                Protocol        = ProtocolType.RTU
            };

            byte[] encoded = request.Encode();
            return(WriteSingleRegister.Decode(encoded));
        }
Example #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.");
        }
Example #3
0
        public static WriteSingleRegister Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, ushort data)
        {
            WriteSingleRegister request = new WriteSingleRegister()
            {
                Header = new MbapHeader()
                {
                    ProtocolId = protocolId, TransactionId = transactionId, UnitId = unitId
                },
                SlaveAddress    = unitId,
                FunctionCode    = 6,
                StartingAddress = (ushort)(startingAddress),
                Data            = data,
                Protocol        = ProtocolType.TCP
            };

            byte[] rtuEncoded = request.Encode();
            return(WriteSingleRegister.Decode(rtuEncoded));
        }
Example #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.");
        }