Пример #1
0
        public static WriteMultipleRegisters Decode(byte[] message, ILogger logger = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                int        index  = 7;
                return(new WriteMultipleRegisters()
                {
                    Header = header,
                    SlaveAddress = header.UnitId,
                    FunctionCode = message[index++],
                    StartingAddress = (ushort)(message[index++] << 0x08 | message[index++]),
                    QuantityOfRegisters = (ushort)(message[index++] << 0x08 | message[index++]),
                    ByteCount = message[index++],
                    Data = GetValues(index, message[index - 1], message),
                    Protocol = ProtocolType.TCP
                });
            }
            catch (Exception ex)
            {
                logger?.LogDebug(ex, "Modbus TCP header read fault.");
                byte[] data = new byte[message.Length - 2];
                Buffer.BlockCopy(message, 0, data, 0, data.Length);
                byte[] checkSum = Crc.Compute(data);

                if (message[message.Length - 2] != checkSum[0] || message[message.Length - 1] != checkSum[1])
                {
                    throw new CheckSumMismatchException("Check sum mismatch.");
                }
                int index = 0;

                return(new WriteMultipleRegisters()
                {
                    SlaveAddress = message[index++],
                    FunctionCode = message[index++],
                    StartingAddress = (ushort)(message[index++] << 0x08 | message[index++]),
                    QuantityOfRegisters = (ushort)(message[index++] << 0x08 | message[index++]),
                    ByteCount = message[index++],
                    Data = GetValues(index, message[index - 1], message),
                    CheckSum = Convert.ToBase64String(checkSum),
                    Protocol = ProtocolType.RTU
                });
            }
        }
        public static ReadHoldingRegistersResponse Decode(byte[] message, ILogger logger = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                int        index  = 7;

                return(new ReadHoldingRegistersResponse()
                {
                    SlaveAddress = header.UnitId,
                    Protocol = ProtocolType.TCP,
                    Header = header,
                    FunctionCode = message[index++],
                    ByteCount = message[index++],
                    RegisterValues = GetValues(message, index++, message[index - 1])
                });
            }
            catch (ModbusTcpException ex)
            {
                logger?.LogDebug(ex, "Modbus TCP header read fault.");
                byte[] data = new byte[message.Length - 2];
                Buffer.BlockCopy(message, 0, data, 0, data.Length);
                byte[] checkSum = Crc.Compute(data);

                if (message[message.Length - 2] != checkSum[0] || message[message.Length - 1] != checkSum[1])
                {
                    throw new CheckSumMismatchException("Check sum mismatch.");
                }

                int index = 0;

                return(new ReadHoldingRegistersResponse()
                {
                    SlaveAddress = message[index++],
                    FunctionCode = message[index++],
                    ByteCount = message[index++],
                    RegisterValues = GetValues(message, index, message[index - 1]),
                    CheckSum = Convert.ToBase64String(checkSum),
                    Protocol = ProtocolType.RTU
                });
            }
        }
Пример #3
0
 public static ModbusMessage Decode(byte[] message)
 {
     try
     {
         MbapHeader header = MbapHeader.Decode(message);
         int        index  = 7;
         byte       code   = message[index++];
         return(GetDecodedMessage(code, message));
     }
     catch
     {
         int index = 0;
         index++;
         byte code = message[index++];
         return(GetDecodedMessage(code, message));
     }
 }
Пример #4
0
        public static ModbusError Decode(byte[] message, ILogger logger = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                MbapHeader header = MbapHeader.Decode(message);
                int        index  = 7;
                return(new ModbusError()
                {
                    Header = header,
                    SlaveAddress = header.UnitId,
                    FunctionCode = (byte)(message[index++] & 0x000F),
                    ErrorCode = (ModbusErrorCode)(message[index] & 0x000F),
                    Protocol = ProtocolType.TCP
                });
            }
            catch (Exception ex)
            {
                logger?.LogDebug(ex, "Modbus TCP header read fault.");
                byte[] data = new byte[message.Length - 2];
                Buffer.BlockCopy(message, 0, data, 0, data.Length);
                byte[] checkSum = Crc.Compute(data);

                if (message[message.Length - 2] != checkSum[0] || message[message.Length - 1] != checkSum[1])
                {
                    throw new CheckSumMismatchException("Check sum mismatch.");
                }
                int index = 0;

                return(new ModbusError()
                {
                    SlaveAddress = message[index++],
                    FunctionCode = (byte)(message[index++] & 0x000F),
                    ErrorCode = (ModbusErrorCode)(message[index] & 0x000F),
                    CheckSum = Convert.ToBase64String(checkSum),
                    Protocol = ProtocolType.RTU
                });
            }
        }