Example #1
0
        /// <summary>
        /// 从Modbus服务器批量读取寄存器的信息,需要指定起始地址,读取长度
        /// </summary>
        /// <param name="address">起始地址,格式为"1234",或者是带功能码格式x=3;1234</param>
        /// <param name="length">读取的数量</param>
        /// <returns>带有成功标志的字节信息</returns>
        /// <remarks>
        /// 富地址格式,支持携带站号信息,功能码信息,具体参照类的示例代码
        /// </remarks>
        /// <example>
        /// 此处演示批量读取的示例
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\Modbus.cs" region="ReadExample1" title="Read示例" />
        /// </example>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadRegister);

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

            List <byte> lists           = new List <byte>( );
            ushort      alreadyFinished = 0;

            while (alreadyFinished < length)
            {
                ushort lengthTmp            = (ushort)Math.Min((length - alreadyFinished), 120);
                OperateResult <byte[]> read = ReadModBusBase(analysis.Content.AddressAdd(alreadyFinished), lengthTmp);
                if (!read.IsSuccess)
                {
                    return(OperateResult.CreateFailedResult <byte[]>(read));
                }

                lists.AddRange(read.Content);
                alreadyFinished += lengthTmp;
            }
            return(OperateResult.CreateSuccessResult(lists.ToArray( )));
        }
        /// <summary>
        /// 生成批量写入寄存器的报文信息
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="values">实际值</param>
        /// <returns>包含结果对象的报文</returns>
        public OperateResult <byte[]> BuildWriteRegisterCommand(string address, byte[] values)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.WriteRegister);

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

            // 生成最终rtu指令
            byte[] buffer = ModbusInfo.PackCommandToRtu(analysis.Content.CreateWriteRegister(station, values));
            return(OperateResult.CreateSuccessResult(buffer));
        }
        /// <summary>
        /// 生成一个读取离散信息的指令头
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length">长度</param>
        /// <returns>携带有命令字节</returns>
        public OperateResult <byte[]> BuildReadDiscreteCommand(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadDiscrete);

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

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToRtu(analysis.Content.CreateReadDiscrete(station, length));
            return(OperateResult.CreateSuccessResult(buffer));
        }
Example #4
0
        /// <summary>
        /// 生成批量写入寄存器的报文信息
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="values">实际值</param>
        /// <returns>包含结果对象的报文</returns>
        public OperateResult <byte[]> BuildWriteRegisterCommand(string address, byte[] values)
        {
            // 解析富地址
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.WriteRegister);

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

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

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToTcp(analysis.Content.CreateWriteRegister(station, values), messageId);
            return(OperateResult.CreateSuccessResult(buffer));
        }
Example #5
0
        /// <summary>
        /// 批量读取线圈或是离散的数据信息,需要指定地址和长度,具体的结果取决于实现
        /// </summary>
        /// <param name="address">数据地址</param>
        /// <param name="length">数据长度</param>
        /// <returns>带有成功标识的bool[]数组</returns>
        public override OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadCoil);

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

            var read = ReadModBusBase((byte)analysis.Content.Function, address, length);

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

            return(OperateResult.CreateSuccessResult(SoftBasic.ByteToBoolArray(read.Content, length)));
        }
Example #6
0
        /// <summary>
        /// 读取自定义的寄存器的值。按照字为单位
        /// </summary>
        /// <param name="address">起始地址,示例:"100","x=4;100"</param>
        /// <param name="length">数据长度</param>
        /// <exception cref="IndexOutOfRangeException"></exception>
        /// <returns>byte数组值</returns>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, true, ModbusInfo.ReadRegister);

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

            if (analysis.Content.Function == ModbusInfo.ReadRegister)
            {
                return(OperateResult.CreateSuccessResult(registerBuffer.GetBytes(analysis.Content.Address * 2, length * 2)));
            }
            else if (analysis.Content.Function == ModbusInfo.ReadInputRegister)
            {
                return(OperateResult.CreateSuccessResult(inputRegisterBuffer.GetBytes(analysis.Content.Address * 2, length * 2)));
            }
            else
            {
                return(new OperateResult <byte[]>(StringResources.Language.NotSupportedDataType));
            }
        }