Esempio n. 1
0
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            List <byte> frames = new List <byte>();

            frames.Add(FunctionCode);
            frames.Add((byte)((StartingAddress >> 8) & 0x00FF));
            frames.Add((byte)(StartingAddress & 0x00FF));
            frames.Add((byte)((QuantityOfRegisters >> 8) & 0x00FF));
            frames.Add((byte)(QuantityOfRegisters & 0x00FF));
            frames.Add(ByteCount);

            for (int i = 0; i < Data.Length; i++)
            {
                frames.Add((byte)((Data[i] >> 8) & 0x00FF));
                frames.Add((byte)(Data[i] & 0x00FF));
            }

            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(frames.Count + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + frames.Count];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(frames.ToArray(), 0, message, headerBytes.Length, frames.Count);

            return(message);
        }
Esempio n. 2
0
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            List <byte> frames = new List <byte>();

            frames.Add(FunctionCode);
            frames.Add((byte)((StartingAddress >> 8) & 0x00FF));
            frames.Add((byte)(StartingAddress & 0x00FF));
            frames.Add((byte)((QuantityOfCoils >> 8) & 0x00FF));
            frames.Add((byte)(QuantityOfCoils & 0x00FF));
            frames.Add(ByteCount);

            byte[] data = new byte[ByteCount];
            Data.CopyTo(data, 0);

            foreach (var item in data)
            {
                frames.Add(item);
            }

            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(frames.Count + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + frames.Count];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(frames.ToArray(), 0, message, headerBytes.Length, frames.Count);

            return(message);
        }
Esempio n. 3
0
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            List <byte> list = new List <byte>();

            list.Add(FunctionCode);
            list.Add(ByteCount);

            byte[] data = new byte[ByteCount];
            Data.CopyTo(data, 0);

            foreach (var item in data)
            {
                list.Add(item);
            }

            //foreach(var item in BitBlock)
            //{
            //    list.Add(item);
            //}

            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(list.Count + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + list.Count];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(list.ToArray(), 0, message, headerBytes.Length, list.Count);
            return(message);
        }
Esempio n. 4
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
                });
            }
        }
Esempio n. 6
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));
     }
 }
Esempio n. 7
0
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            int index = 0;

            byte[] frames = new byte[2];
            frames[index++] = (byte)((1 << 7) | FunctionCode);
            frames[index++] = (byte)Convert.ToByte(ErrorCode);
            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(frames.Length + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + frames.Length];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(frames, 0, message, headerBytes.Length, frames.Length);
            return(message);
        }
Esempio n. 8
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
                });
            }
        }
Esempio n. 9
0
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            int index = 0;

            byte[] frames = new byte[5];
            frames[index++] = FunctionCode;
            frames[index++] = (byte)((StartingAddress >> 8) & 0x00FF);     //MSB
            frames[index++] = (byte)(StartingAddress & 0x00FF);            //LSB
            frames[index++] = (byte)((QuantityOfRegisters >> 8) & 0x00FF); //MSB
            frames[index++] = (byte)(QuantityOfRegisters & 0x00FF);        //LSB
            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(frames.Length + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + frames.Length];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(frames, 0, message, headerBytes.Length, frames.Length);
            return(message);
        }
Esempio n. 10
0
        public static MbapHeader Decode(byte[] message)
        {
            if (message.Length < 7)
            {
                throw new InvalidMbapHeaderException("MBAP header length less than 7 bytes.");
            }

            MbapHeader header = new MbapHeader();

            int index = 0;

            header.TransactionId = (ushort)(message[index++] << 0x08 | message[index++]);
            header.ProtocolId    = (ushort)(message[index++] << 0x08 | message[index++]);
            header.Length        = (ushort)(message[index++] << 0x08 | message[index++]);
            header.UnitId        = Convert.ToByte(message[index]);

            if (message.Length - 6 != header.Length)
            {
                throw new MessageLengthMismatchException("MBAP header has invalid message length.");
            }

            return(header);
        }
        public override byte[] ConvertToTcp(byte unitId, ushort transactionId, ushort protocolId = 0)
        {
            List <byte> list = new List <byte>();

            list.Add(FunctionCode);
            list.Add(ByteCount);
            foreach (var item in RegisterValues)
            {
                list.Add((byte)((item >> 8) & 0x00FF));
                list.Add((byte)(item & 0x00FF));
            }

            MbapHeader header = new MbapHeader()
            {
                UnitId = unitId, TransactionId = transactionId, ProtocolId = protocolId, Length = (ushort)(list.Count + 1)
            };

            byte[] headerBytes = header.Encode();
            byte[] message     = new byte[headerBytes.Length + list.Count];
            Buffer.BlockCopy(headerBytes, 0, message, 0, headerBytes.Length);
            Buffer.BlockCopy(list.ToArray(), 0, message, headerBytes.Length, list.Count);
            return(message);
        }