Esempio n. 1
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));
        }
Esempio n. 2
0
        /// <summary>
        /// 读取服务器的数据,需要指定不同的功能码
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length">长度</param>
        /// <returns>带是否成功的结果数据</returns>
        private OperateResult <byte[]> ReadModBusBase(ModbusAddress address, ushort length)
        {
            OperateResult <byte[]> command = BuildReadRegisterCommand(address, length);

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

            OperateResult <byte[]> resultBytes = CheckModbusTcpResponse(command.Content);

            if (resultBytes.IsSuccess)
            {
                // 二次数据处理
                if (resultBytes.Content?.Length >= 9)
                {
                    byte[] buffer = new byte[resultBytes.Content.Length - 9];
                    Array.Copy(resultBytes.Content, 9, buffer, 0, buffer.Length);
                    resultBytes.Content = buffer;
                }
            }
            return(resultBytes);
        }
Esempio n. 3
0
 /// <summary>
 /// 分析Modbus协议的地址信息,该地址适应于tcp及rtu模式
 /// </summary>
 /// <param name="address">带格式的地址,比如"100","x=4;100","s=1;100","s=1;x=4;100"</param>
 /// <param name="isStartWithZero">起始地址是否从0开始</param>
 /// <returns>转换后的地址信息</returns>
 public static OperateResult <ModbusAddress> AnalysisReadAddress(string address, bool isStartWithZero)
 {
     try
     {
         ModbusAddress mAddress = new ModbusAddress(address);
         if (!isStartWithZero)
         {
             if (mAddress.Address < 1)
             {
                 throw new Exception("地址值在起始地址为1的情况下,必须大于1");
             }
             mAddress.Address = (ushort)(mAddress.Address - 1);
         }
         return(OperateResult.CreateSuccessResult(mAddress));
     }
     catch (Exception ex)
     {
         return(new OperateResult <ModbusAddress>( )
         {
             Message = ex.Message
         });
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 分析Modbus协议的地址信息,该地址适应于tcp及rtu模式
 /// </summary>
 /// <param name="address">带格式的地址,比如"100","x=4;100","s=1;100","s=1;x=4;100"</param>
 /// <param name="isStartWithZero">起始地址是否从0开始</param>
 /// <returns>转换后的地址信息</returns>
 public static OperateResult <ModbusAddress> AnalysisReadAddress(string address, bool isStartWithZero)
 {
     try
     {
         ModbusAddress mAddress = new ModbusAddress(address, ModbusInfo.ReadRegister);
         if (!isStartWithZero)
         {
             if (mAddress.Address < 1)
             {
                 throw new Exception(StringResources.Language.ModbusAddressMustMoreThanOne);
             }
             mAddress.Address = (ushort)(mAddress.Address - 1);
         }
         return(OperateResult.CreateSuccessResult(mAddress));
     }
     catch (Exception ex)
     {
         return(new OperateResult <ModbusAddress>( )
         {
             Message = ex.Message
         });
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 生成一个读取寄存器的指令头
 /// </summary>
 /// <param name="address">地址</param>
 /// <param name="length">长度</param>
 /// <returns>携带有命令字节</returns>
 private OperateResult <byte[]> BuildReadRegisterCommand(ModbusAddress address, ushort length)
 {
     // 生成最终tcp指令
     byte[] buffer = ModbusInfo.PackCommandToRtu(address.CreateReadRegister(station, length));
     return(OperateResult.CreateSuccessResult(buffer));
 }