Beispiel #1
0
 /// <summary>
 ///     注册一个协议
 /// </summary>
 /// <param name="linkProtocal">需要注册的协议</param>
 protected void Register(ProtocalUnit linkProtocal)
 {
     if (linkProtocal == null)
     {
         return;
     }
     Protocals.Add(linkProtocal.GetType().FullName, linkProtocal);
 }
Beispiel #2
0
 /// <summary>
 ///     发送协议内容并接收,一般方法
 /// </summary>
 /// <param name="content">写入的内容,使用对象数组描述</param>
 /// <returns>从设备获取的字节流</returns>
 public virtual async Task <byte[]> SendReceiveAsync(params object[] content)
 {
     if (ProtocalLinker == null || !ProtocalLinker.IsConnected)
     {
         await ConnectAsync();
     }
     if (ProtocalLinker != null)
     {
         return(await ProtocalLinker.SendReceiveAsync(ProtocalUnit.TranslateContent(Endian, content)));
     }
     return(null);
 }
Beispiel #3
0
        /// <summary>
        ///     发送协议,通过传入需要使用的协议内容和输入结构
        /// </summary>
        /// <param name="unit">协议的实例</param>
        /// <param name="content">输入信息的结构化描述</param>
        /// <returns>输出信息的结构化描述</returns>
        public virtual async Task <IOutputStruct> SendReceiveAsync(ProtocalUnit unit, IInputStruct content)
        {
            var t             = 0;
            var formatContent = unit.Format(content);

            if (formatContent != null)
            {
                byte[] receiveContent;
                //如果为特别处理协议的话,跳过协议扩展收缩
                if (unit is ISpecialProtocalUnit)
                {
                    receiveContent = await ProtocalLinker.SendReceiveWithoutExtAndDecAsync(formatContent);
                }
                else
                {
                    receiveContent = await ProtocalLinker.SendReceiveAsync(formatContent);
                }
                if (receiveContent != null)
                {
                    return(unit.Unformat(receiveContent, ref t));
                }
            }
            return(null);
        }
Beispiel #4
0
 /// <summary>
 ///     发送协议,通过传入需要使用的协议内容和输入结构
 /// </summary>
 /// <param name="unit">协议的实例</param>
 /// <param name="content">输入信息的结构化描述</param>
 /// <returns>输出信息的结构化描述</returns>
 public virtual IOutputStruct SendReceive(ProtocalUnit unit, IInputStruct content)
 {
     return(AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)));
 }
Beispiel #5
0
 /// <summary>
 ///     发送协议,通过传入需要使用的协议内容和输入结构
 /// </summary>
 /// <param name="unit">协议的实例</param>
 /// <param name="content">输入信息的结构化描述</param>
 /// <returns>输出信息的结构化描述</returns>
 public virtual async Task <IOutputStruct> SendReceiveAsync(ProtocalUnit unit, IInputStruct content)
 {
     return(await SendReceiveAsync <IOutputStruct>(unit, content));
 }
Beispiel #6
0
 /// <summary>
 ///     发送协议,通过传入需要使用的协议内容和输入结构
 /// </summary>
 /// <param name="unit">协议的实例</param>
 /// <param name="content">输入信息的结构化描述</param>
 /// <returns>输出信息的结构化描述</returns>
 /// <typeparam name="T">IOutputStruct的具体类型</typeparam>
 public virtual T SendReceive <T>(ProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct
 {
     return(AsyncHelper.RunSync(() => SendReceiveAsync <T>(unit, content)));
 }