Ejemplo n.º 1
0
        public static WriteMultipleCoils Decode(string message)
        {
            WriteMultipleCoils response = JsonConvert.DeserializeObject <WriteMultipleCoils>(message);

            byte[] msg = response.Encode();
            return(Decode(msg));
        }
Ejemplo n.º 2
0
        public static WriteMultipleCoils Create(byte slaveId, ushort startingAddress, BitArray coilValues)
        {
            WriteMultipleCoils request = new WriteMultipleCoils()
            {
                SlaveAddress    = slaveId,
                FunctionCode    = 15,
                StartingAddress = startingAddress,
                QuantityOfCoils = (ushort)coilValues.Length,
                ByteCount       = (coilValues.Length % 8) == 0 ? (byte)(coilValues.Length / 8) : (byte)((coilValues.Length / 8) + 1),
                Data            = coilValues,
                Protocol        = ProtocolType.RTU
            };

            byte[] rtuEncoded = request.Encode();
            return(WriteMultipleCoils.Decode(rtuEncoded));
        }
Ejemplo n.º 3
0
        public static WriteMultipleCoils Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, BitArray coilValues)
        {
            WriteMultipleCoils request = new WriteMultipleCoils()
            {
                Header = new MbapHeader()
                {
                    ProtocolId = protocolId, TransactionId = transactionId, UnitId = unitId
                },
                SlaveAddress    = unitId,
                FunctionCode    = 15,
                StartingAddress = startingAddress,
                QuantityOfCoils = (ushort)coilValues.Length,
                ByteCount       = (coilValues.Length % 8) == 0 ? (byte)(coilValues.Length / 8) : (byte)((coilValues.Length / 8) + 1),
                Data            = coilValues,
                Protocol        = ProtocolType.TCP
            };

            byte[] encoded = request.Encode();
            return(WriteMultipleCoils.Decode(encoded));
        }
Ejemplo 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.");
        }
Ejemplo n.º 5
0
 public static ModbusMessage Create(byte unitId, ushort transactionId, ushort protocolId, ushort startingAddress, BitArray coilValues)
 {
     return(WriteMultipleCoils.Create(unitId, transactionId, protocolId, startingAddress, coilValues));
 }
Ejemplo n.º 6
0
 public static ModbusMessage Create(byte slaveId, ushort startingAddress, BitArray coilValues)
 {
     //15
     return(WriteMultipleCoils.Create(slaveId, startingAddress, coilValues));
 }