/// <summary>
        /// 使用指定的类型写入指定的节点数据
        /// </summary>
        /// <param name="address">节点地址数据</param>
        /// <param name="typeCode">类型代码,详细参见<see cref="AllenBradleyHelper"/>上的常用字段</param>
        /// <param name="value">实际的数据值</param>
        /// <param name="length">如果节点是数组,就是数组长度</param>
        /// <returns>是否写入成功</returns>
        public OperateResult WriteTag(string address, ushort typeCode, byte[] value, int length = 1)
        {
            OperateResult <byte[]> command = BuildWriteCommand(address, typeCode, value, length);

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

            // 核心交互
            OperateResult <byte[]> read = ReadFromCoreServer(command.Content);

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

            // 检查反馈
            OperateResult check = CheckResponse(read.Content);

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

            // 提取写入结果
            return(AllenBradleyHelper.ExtractActualData(read.Content, false));
        }
        /// <summary>
        /// 创建一个写入的报文指令
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="typeCode">类型数据</param>
        /// <param name="data">数据</param>
        /// <param name="length">如果是数组,就为数组长度</param>
        /// <returns>包含结果对象的报文信息</returns>
        public OperateResult <byte[]> BuildWriteCommand(string address, ushort typeCode, byte[] data, int length = 1)
        {
            byte[] cip = AllenBradleyHelper.PackRequestWrite(address, typeCode, data, length);
            byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cip);

            return(OperateResult.CreateSuccessResult(AllenBradleyHelper.PackRequestHeader(0x6F, SessionHandle, commandSpecificData)));
        }
        /// <summary>
        /// 批量读取数据信息,数据长度无效
        /// </summary>
        /// <param name="address">节点的地址格式</param>
        /// <param name="length">每个地址的数组长度</param>
        /// <returns>带有结果对象的结果数据</returns>
        public OperateResult <byte[]> Read(string[] address, int[] length)
        {
            // 指令生成
            OperateResult <byte[]> command = BuildReadCommand(address, length);

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

            // 核心交互
            OperateResult <byte[]> read = ReadFromCoreServer(command.Content);

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

            // 检查反馈
            OperateResult check = CheckResponse(read.Content);

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

            // 提取数据
            return(AllenBradleyHelper.ExtractActualData(read.Content, true));
        }
예제 #4
0
 public byte[] RegisterSessionHandle()
 {
     byte[] buffer1 = new byte[4];
     buffer1[0] = 1;
     byte[] commandSpecificData = buffer1;
     return(AllenBradleyHelper.PackRequestHeader(0x65, 0, commandSpecificData));
 }
예제 #5
0
 public OperateResult <byte[]> BuildReadCommand(string[] address, int[] length)
 {
     if ((address == null) || (length == null))
     {
         return(new OperateResult <byte[]>("address or length is null"));
     }
     if (address.Length != length.Length)
     {
         return(new OperateResult <byte[]>("address and length is not same array"));
     }
     try
     {
         List <byte[]> list = new List <byte[]>();
         for (int i = 0; i < address.Length; i++)
         {
             list.Add(AllenBradleyHelper.PackRequsetRead(address[i], length[i]));
         }
         byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, list.ToArray());
         return(OperateResult.CreateSuccessResult <byte[]>(AllenBradleyHelper.PackRequestHeader(0x6f, SessionHandle, commandSpecificData)));
     }
     catch (Exception exception)
     {
         return(new OperateResult <byte[]>("Address Wrong:" + exception.Message));
     }
 }
예제 #6
0
        private OperateResult <byte[]> ReadByCips(params byte[][] cips)
        {
            OperateResult <byte[]> result = ReadCipFromServer(cips);

            if (!result.IsSuccess)
            {
                return(result);
            }
            return(AllenBradleyHelper.ExtractActualData(result.Content, true));
        }
예제 #7
0
 public OperateResult <byte[]> BuildWriteCommand(string address, ushort typeCode, byte[] data, int length = 1)
 {
     try
     {
         byte[]   buffer = AllenBradleyHelper.PackRequestWrite(address, typeCode, data, length);
         byte[][] cips   = new byte[][] { buffer };
         byte[]   commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cips);
         return(OperateResult.CreateSuccessResult <byte[]>(AllenBradleyHelper.PackRequestHeader(0x6f, SessionHandle, commandSpecificData)));
     }
     catch (Exception exception)
     {
         return(new OperateResult <byte[]>("Address Wrong:" + exception.Message));
     }
 }
예제 #8
0
        public OperateResult <byte[]> ReadCipFromServer(params byte[][] cips)
        {
            byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cips);
            byte[] send = AllenBradleyHelper.PackRequestHeader(0x6f, SessionHandle, commandSpecificData);
            OperateResult <byte[]> result = base.ReadFromCoreServer(send);

            if (!result.IsSuccess)
            {
                return(result);
            }
            OperateResult result2 = CheckResponse(result.Content);

            if (!result2.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(result2));
            }
            return(OperateResult.CreateSuccessResult <byte[]>(result.Content));
        }
        /// <summary>
        /// 创建一个读取的报文指令
        /// </summary>
        /// <param name="address">tag名的地址</param>
        /// <param name="length">数组信息,如果不是数组,就都为1</param>
        /// <returns>包含结果对象的报文信息</returns>
        public OperateResult <byte[]> BuildReadCommand(string[] address, int[] length)
        {
            if (address == null || length == null)
            {
                return(new OperateResult <byte[]>("address or length is null"));
            }
            if (address.Length != length.Length)
            {
                return(new OperateResult <byte[]>("address and length is not same array"));
            }

            List <byte[]> cips = new List <byte[]>( );

            for (int i = 0; i < address.Length; i++)
            {
                cips.Add(AllenBradleyHelper.PackRequsetRead(address[i], length[i]));
            }
            byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(Slot, cips.ToArray( ));

            return(OperateResult.CreateSuccessResult(AllenBradleyHelper.PackRequestHeader(0x6F, SessionHandle, commandSpecificData)));
        }
예제 #10
0
        public OperateResult WriteTag(string address, ushort typeCode, byte[] value, int length = 1)
        {
            OperateResult <byte[]> result = BuildWriteCommand(address, typeCode, value, length);

            if (!result.IsSuccess)
            {
                return(result);
            }
            OperateResult <byte[]> result2 = ReadFromCoreServer(result.Content);

            if (!result2.IsSuccess)
            {
                return(result2);
            }
            OperateResult result3 = CheckResponse(result2.Content);

            if (!result3.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(result3));
            }
            return(AllenBradleyHelper.ExtractActualData(result2.Content, false));
        }
예제 #11
0
        public OperateResult <byte[]> Read(string[] address, int[] length)
        {
            OperateResult <byte[]> result = BuildReadCommand(address, length);

            if (!result.IsSuccess)
            {
                return(result);
            }
            OperateResult <byte[]> result2 = base.ReadFromCoreServer(result.Content);

            if (!result2.IsSuccess)
            {
                return(result2);
            }
            OperateResult result3 = CheckResponse(result2.Content);

            if (!result3.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(result3));
            }
            return(AllenBradleyHelper.ExtractActualData(result2.Content, true));
        }
예제 #12
0
 public OperateResult <byte[]> ReadSegment(string address, int startIndex, int length)
 {
     try
     {
         ushort      num2;
         List <byte> list = new List <byte>();
         for (ushort i = 0; i < length; i = (ushort)(i + num2))
         {
             num2 = (ushort)Math.Min(length - i, 100);
             byte[][] cips = new byte[][] { AllenBradleyHelper.PackRequestReadSegment(address, startIndex + i, num2) };
             OperateResult <byte[]> result = ReadByCips(cips);
             if (!result.IsSuccess)
             {
                 return(result);
             }
             list.AddRange(result.Content);
         }
         return(OperateResult.CreateSuccessResult <byte[]>(list.ToArray()));
     }
     catch (Exception exception)
     {
         return(new OperateResult <byte[]>("Address Wrong:" + exception.Message));
     }
 }
 /// <summary>
 /// 获取卸载一个已注册的会话的报文
 /// </summary>
 /// <returns>字节报文信息</returns>
 public byte[] UnRegisterSessionHandle( )
 {
     return(AllenBradleyHelper.PackRequestHeader(0x66, SessionHandle, new byte[0]));
 }
 /// <summary>
 /// 向PLC注册会话ID的报文
 /// </summary>
 /// <returns>报文信息</returns>
 public byte[] RegisterSessionHandle( )
 {
     byte[] commandSpecificData = new byte[] { 0x01, 0x00, 0x00, 0x00, };
     return(AllenBradleyHelper.PackRequestHeader(0x65, 0, commandSpecificData));
 }