Esempio n. 1
0
        /// <summary>
        /// 读取数据的基础指令,需要指定指令码,地址,长度
        /// </summary>
        /// <param name="code"></param>
        /// <param name="address"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private OperateResult <byte[]> BuildReadCommandBase(byte code, string address, ushort count)
        {
            OperateResult <int> analysis = AnalysisAddress(address);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            ushort messageId = (ushort)softIncrementCount.GetCurrentValue( );

            byte[] buffer = new byte[12];
            buffer[0]  = (byte)(messageId / 256);
            buffer[1]  = (byte)(messageId % 256);
            buffer[2]  = 0x00;
            buffer[3]  = 0x00;
            buffer[4]  = 0x00;
            buffer[5]  = 0x06;
            buffer[6]  = station;
            buffer[7]  = code;
            buffer[8]  = (byte)(analysis.Content / 256);
            buffer[9]  = (byte)(analysis.Content % 256);
            buffer[10] = (byte)(count / 256);
            buffer[11] = (byte)(count % 256);

            return(OperateResult.CreateSuccessResult(buffer));
        }
Esempio n. 2
0
        /// <summary>
        /// 读取数据的基础指令,需要指定指令码,地址,长度
        /// </summary>
        /// <param name="code"></param>
        /// <param name="address"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private OperateResult <byte[]> BuildReadCommandBase(byte code, string address, ushort count)
        {
            var result = new OperateResult <byte[]>( );

            OperateResult <int> analysis = AnalysisAddress(address);

            if (!analysis.IsSuccess)
            {
                result.CopyErrorFromOther(analysis);
                return(result);
            }

            ushort messageId = (ushort)softIncrementCount.GetCurrentValue( );

            byte[] buffer = new byte[12];
            buffer[0]  = (byte)(messageId / 256);
            buffer[1]  = (byte)(messageId % 256);
            buffer[2]  = 0x00;
            buffer[3]  = 0x00;
            buffer[4]  = 0x00;
            buffer[5]  = 0x06;
            buffer[6]  = station;
            buffer[7]  = code;
            buffer[8]  = (byte)(analysis.Content / 256);
            buffer[9]  = (byte)(analysis.Content % 256);
            buffer[10] = (byte)(count / 256);
            buffer[11] = (byte)(count % 256);

            result.Content   = buffer;
            result.IsSuccess = true;
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// 生成一个读取线圈的指令头
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length">长度</param>
        /// <returns>包含结果对象的报文</returns>
        public OperateResult <byte[]> BuildReadCoilCommand(string address, ushort length)
        {
            // 解析富地址
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadCoil);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            // 获取消息号
            ushort messageId = (ushort)softIncrementCount.GetCurrentValue( );

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToTcp(analysis.Content.CreateReadCoils(station, length), messageId);
            return(OperateResult.CreateSuccessResult(buffer));
        }