示例#1
0
        /// <summary>
        /// 检查当前的Modbus-Ascii响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        protected override OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            // 转ascii
            byte[] modbus_ascii = ModbusInfo.TransRtuToAsciiPackCommand(send);

            // 核心交互
            OperateResult <byte[]> result = ReadBase(modbus_ascii);

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

            // 还原modbus报文
            OperateResult <byte[]> modbus_core = ModbusInfo.TransAsciiPackCommandToRtu(result.Content);

            if (!modbus_core.IsSuccess)
            {
                return(modbus_core);
            }

            // 发生了错误
            if ((send[1] + 0x80) == modbus_core.Content[1])
            {
                return(new OperateResult <byte[]>(modbus_core.Content[2], ModbusInfo.GetDescriptionByErrorCode(modbus_core.Content[2])));
            }

            // 成功的消息
            return(OperateResult.CreateSuccessResult(modbus_core.Content));
        }
示例#2
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="YumpooDrive_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.AnalysisReadAddress(address, isAddressStartWithZero);

            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( )));
        }
示例#3
0
        /// <summary>
        /// 生成一个读取寄存器的指令头
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length">长度</param>
        /// <returns>包含结果对象的报文</returns>
        private OperateResult <byte[]> BuildReadRegisterCommand(ModbusAddress address, ushort length)
        {
            // 获取消息号
            ushort messageId = (ushort)softIncrementCount.GetCurrentValue( );

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToTcp(address.CreateReadRegister(station, length), messageId);
            return(OperateResult.CreateSuccessResult(buffer));
        }
示例#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.AnalysisReadAddress(address, isAddressStartWithZero);

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

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

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

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToRtu(analysis.Content.CreateReadDiscrete(station, length));
            return(OperateResult.CreateSuccessResult(buffer));
        }
示例#6
0
        /// <summary>
        /// 检查当前的Modbus-Tcp响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        private OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            OperateResult <byte[]> resultBytes = ReadFromCoreServer(send);

            if (resultBytes.IsSuccess)
            {
                if ((send[7] + 0x80) == resultBytes.Content[7])
                {
                    // 发生了错误
                    resultBytes.IsSuccess = false;
                    resultBytes.Message   = ModbusInfo.GetDescriptionByErrorCode(resultBytes.Content[8]);
                    resultBytes.ErrorCode = resultBytes.Content[8];
                }
            }
            return(resultBytes);
        }
示例#7
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.AnalysisReadAddress(address, isAddressStartWithZero);

            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));
        }
示例#8
0
        /// <summary>
        /// 检查当前的Modbus-Rtu响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        protected virtual OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            // 核心交互
            OperateResult <byte[]> result = ReadBase(send);

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

            // 长度校验
            if (result.Content.Length < 5)
            {
                return(new OperateResult <byte[]>(StringResources.Language.ReceiveDataLengthTooShort + "5"));
            }

            // 检查crc
            if (!SoftCRC16.CheckCRC16(result.Content))
            {
                return(new OperateResult <byte[]>(StringResources.Language.ModbusCRCCheckFailed));
            }

            // 发生了错误
            if ((send[1] + 0x80) == result.Content[1])
            {
                return(new OperateResult <byte[]>(result.Content[2], ModbusInfo.GetDescriptionByErrorCode(result.Content[2])));
            }

            if (send[1] != result.Content[1])
            {
                return(new OperateResult <byte[]>(result.Content[1], $"Receive Command Check Failed: "));
            }

            // 移除CRC校验
            byte[] buffer = new byte[result.Content.Length - 2];
            Array.Copy(result.Content, 0, buffer, 0, buffer.Length);
            return(OperateResult.CreateSuccessResult(buffer));
        }
示例#9
0
 /// <summary>
 /// 生成一个读取寄存器的指令头
 /// </summary>
 /// <param name="address">地址</param>
 /// <param name="length">长度</param>
 /// <returns>携带有命令字节</returns>
 private OperateResult <byte[]> BuildReadRegisterCommand(ModbusAddress address, ushort length)
 {
     // 生成最终rtu指令
     byte[] buffer = ModbusInfo.PackCommandToRtu(address.CreateReadRegister(station, length));
     return(OperateResult.CreateSuccessResult(buffer));
 }