コード例 #1
0
        public override void FromNetworkBuffer(byte[] buffer)
        {
            var idx = 0;

            Header = ModbusHeader.FromNetworkBuffer(buffer);
            idx   += ModbusHeader.FixedLength;

            UnitIdentifier = buffer[idx++];
            FunctionCode   = buffer[idx++];
            Length         = buffer[idx++];

            Data = new byte[Length];
            Buffer.BlockCopy(buffer, idx, Data, 0, Length);
        }
コード例 #2
0
ファイル: ModbusClient.cs プロジェクト: panyonglin/ModbusTcp
        private T ReadResponse <T>() where T : ModbusResponseBase
        {
            var headerBytes = ReadFromBuffer(ModbusHeader.FixedLength);
            var header      = ModbusHeader.FromNetworkBuffer(headerBytes);

            var dataBytes = ReadFromBuffer(header.Length);

            var fullBuffer = headerBytes.Concat(dataBytes).ToArray();
            var response   = Activator.CreateInstance <T>();

            response.FromNetworkBuffer(fullBuffer);

            return(response);
        }
コード例 #3
0
        public override void FromNetworkBuffer(byte[] buffer)
        {
            var idx = 0;

            Header = ModbusHeader.FromNetworkBuffer(buffer);
            idx   += ModbusHeader.FixedLength;

            UnitIdentifier = buffer[idx++];
            FunctionCode   = buffer[idx++];

            ReferenceNumber = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, idx));
            idx            += 2;

            WordCount = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, idx));
            idx      += 2;
        }
コード例 #4
0
        protected int StandardResponseFromNetworkBuffer(byte[] buffer)
        {
            var idx = 0;

            Header = ModbusHeader.FromNetworkBuffer(buffer);
            idx   += ModbusHeader.FixedLength;

            UnitIdentifier = buffer[idx++];
            FunctionCode   = buffer[idx++];

            if (FunctionCode >= 0x80)
            {
                var exceptionCode       = buffer[idx];
                var orginalFunctionCode = (byte)(FunctionCode - ModbusErrorOffset);

                throw new ModbusReplyException(orginalFunctionCode, exceptionCode);
            }

            return(idx);
        }
コード例 #5
0
        private T ReadResponse <T>() where T : ModbusResponseBase
        {
            var headerBytes = ReadFromBuffer(ModbusHeader.FixedLength);
            var header      = ModbusHeader.FromNetworkBuffer(headerBytes);

            var dataBytes = ReadFromBuffer(header.Length - 1); //Ommit Unit Identifier from MBAP Header

            var response = Activator.CreateInstance <T>();

            try
            {
                response.FromNetworkBuffer(header, dataBytes);
            }
            catch (ModbusReplyException mre)
            {
                throw new ModbusReplyException(mre, $"{ ipAddress }:{ port }");
            }

            return(response);
        }