Exemplo n.º 1
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">输入参数</param>
        /// <returns>格式化数据</returns>
        public override byte[] Format(IInputStruct message)
        {
            var          r_message         = (WriteRequestSiemensInputStruct)message;
            var          valueBytes        = BigEndianValueHelper.Instance.ObjectArrayToByteArray(r_message.WriteValue);
            var          slaveAddress      = r_message.SlaveAddress;
            var          masterAddress     = r_message.MasterAddress;
            const byte   protoId           = 0x32;
            const byte   rosctr            = 0x01;
            const ushort redId             = 0x0000;
            var          pduRef            = r_message.PduRef;
            const ushort parLg             = 14;                              // 参数字节数(2+12的倍数),目前仅为14
            var          datLg             = (ushort)(4 + valueBytes.Length); // 数据字节数
            const byte   serviceId         = 0x05;
            const byte   numberOfVariables = 1;
            const byte   variableSpec      = 0x12;
            const byte   vAddrLg           = 0x0A;
            const byte   syntaxId          = 0x10;
            const byte   typeR             = (byte)SiemensTypeCode.Byte;
            var          numberOfElements  = (ushort)valueBytes.Length;
            var          dbBlock           = r_message.DbBlock;
            var          area              = r_message.Area;
            var          offsetBit         = r_message.Offset * 8;
            var          offsetBitBytes    = BigEndianValueHelper.Instance.GetBytes(offsetBit);
            const byte   reserved          = 0x00;
            const byte   type              = (byte)SiemensDataType.OtherAccess;
            var          numberOfWriteBits = (ushort)(valueBytes.Length * 8);

            return(Format(new byte[4], slaveAddress, masterAddress, (byte)0x7c, protoId, rosctr, redId, pduRef, parLg,
                          datLg, serviceId, numberOfVariables
                          , variableSpec, vAddrLg, syntaxId, typeR, numberOfElements, dbBlock, area,
                          offsetBitBytes.Skip(1).ToArray(), reserved, type, numberOfWriteBits, valueBytes));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">输入参数</param>
        /// <returns>格式化数据</returns>
        public override byte[] Format(IInputStruct message)
        {
            var          r_message         = (ReadRequestSiemensInputStruct)message;
            var          slaveAddress      = r_message.SlaveAddress;
            var          masterAddress     = r_message.MasterAddress;
            const byte   protoId           = 0x32;
            const byte   rosctr            = 0x01;
            const ushort redId             = 0x0000;
            var          pduRef            = r_message.PduRef;
            const ushort parLg             = 14; // 参数字节数(2+12的倍数),目前仅为14
            const ushort datLg             = 0;  // 数据字节数
            const byte   serviceId         = 0x04;
            const byte   numberOfVariables = 1;
            const byte   variableSpec      = 0x12;
            const byte   vAddrLg           = 0x0A;
            const byte   syntaxId          = 0x10;
            var          type             = r_message.TypeCode;
            var          numberOfElements = r_message.NumberOfElements;
            var          dbBlock          = r_message.DbBlock;
            var          area             = r_message.Area;
            var          offsetBit        = r_message.Offset * 8;
            var          offsetBitBytes   = BigEndianValueHelper.Instance.GetBytes(offsetBit);

            return(Format(new byte[4], slaveAddress, masterAddress, (byte)0x6c, protoId, rosctr, redId, pduRef, parLg,
                          datLg, serviceId, numberOfVariables
                          , variableSpec, vAddrLg, syntaxId, type, numberOfElements, dbBlock, area,
                          offsetBitBytes.Skip(1).ToArray()));
        }
Exemplo n.º 3
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">写系统时间参数</param>
        /// <returns>写系统时间的核心</returns>
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (GetSystemTimeModbusInputStruct)message;

            return(Format(r_message.SlaveAddress, r_message.FunctionCode,
                          r_message.StartAddress, r_message.GetCount));
        }
Exemplo n.º 4
0
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (WriteRequestOpcInputStruct)message;
            var tag       = Encoding.UTF8.GetBytes(r_message.Tag);
            var fullName  = Encoding.UTF8.GetBytes(r_message.SetValue.GetType().FullName);

            return(Format((byte)0x01, tag, 0x00ffff00, fullName, 0x00ffff00, r_message.SetValue));
        }
Exemplo n.º 5
0
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (ComCreateReferenceSiemensInputStruct)message;
            var crc       = (r_message.SlaveAddress + r_message.MasterAddress + 0x49) % 256;

            return(Format((byte)0x10, r_message.SlaveAddress, r_message.MasterAddress, (byte)0x49, (byte)crc,
                          (byte)0x16));
        }
Exemplo n.º 6
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">输入参数</param>
        /// <returns>格式化数据</returns>
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (ComConfirmMessageSiemensInputStruct)message;
            var crc       = r_message.SlaveAddress + r_message.MasterAddress + 0x5c % 256;

            return(Format((byte)0x10, r_message.SlaveAddress, r_message.MasterAddress, (byte)0x5c, (byte)crc,
                          (byte)0x16));
        }
Exemplo n.º 7
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">写寄存器参数</param>
        /// <returns>写寄存器协议核心</returns>
        public override byte[] Format(IInputStruct message)
        {
            var r_message       = (WriteSingleDataModbusInputStruct)message;
            var dataValue       = Format(r_message.WriteValue);
            var formattingBytes = Format(r_message.SlaveAddress, r_message.FunctionCode,
                                         r_message.StartAddress, dataValue);

            return(formattingBytes);
        }
Exemplo n.º 8
0
        /// <summary>
        ///     格式化
        /// </summary>
        /// <param name="message">写系统时间的参数</param>
        /// <returns>写系统时间的核心</returns>
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (SetSystemTimeModbusInputStruct)message;

            return(Format(r_message.SlaveAddress, r_message.FunctionCode,
                          r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.Year,
                          r_message.Day,
                          r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond));
        }
Exemplo n.º 9
0
        /// <summary>
        ///     从对象的参数数组格式化
        /// </summary>
        /// <param name="message">非结构化的输入数据</param>
        /// <returns>格式化后的字节流</returns>
        public override OpcParamIn Format(IInputStruct message)
        {
            var r_message = (ReadRequestOpcInputStruct)message;

            return(new OpcParamIn
            {
                IsRead = true,
                Tag = r_message.Tag,
                Split = r_message.Split
            });
        }
Exemplo n.º 10
0
        /// <summary>
        ///     从对象的参数数组格式化
        /// </summary>
        /// <param name="message">非结构化的输入数据</param>
        /// <returns>格式化后的字节流</returns>
        public override OpcParamIn Format(IInputStruct message)
        {
            var r_message = (WriteRequestOpcInputStruct)message;

            return(new OpcParamIn
            {
                IsRead = false,
                Tag = r_message.Tag,
                Split = r_message.Split,
                SetValue = r_message.SetValue
            });
        }
Exemplo n.º 11
0
        public override byte[] Format(IInputStruct message)
        {
            var          r_message  = (EstablishAssociationSiemensInputStruct)message;
            const byte   protoId    = 0x32;
            const byte   rosctr     = 0x01;
            const ushort redId      = 0x0000;
            var          pduRef     = r_message.PduRef;
            const ushort parLg      = 0x0008;
            const ushort datLg      = 0x0000;
            const byte   serviceId  = 0xf0;
            const byte   reserved   = 0x00;
            var          maxCalling = r_message.MaxCalling;
            var          maxCalled  = r_message.MaxCalled;
            var          maxPdu     = r_message.MaxPdu;

            return(Format(new byte[7], protoId, rosctr, redId, pduRef, parLg, datLg, serviceId, reserved, maxCalling,
                          maxCalled, maxPdu));
        }
Exemplo n.º 12
0
        public override byte[] Format(IInputStruct message)
        {
            var          r_message       = (CreateReferenceSiemensInputStruct)message;
            const ushort head            = 0x0300;
            const ushort len             = 0x0016;
            const byte   contentLen      = 0x11;
            const byte   typeCode        = 0xe0;
            const ushort dstRef          = 0x0000;
            const ushort srcRef          = 0x000c;
            const byte   reserved        = 0x00;
            const ushort tdpuSizeCode    = 0xc001;
            var          tdpuSizeContent = r_message.TdpuSize;
            const ushort srcTsapCode     = 0xc102;
            var          srcTsapContent  = r_message.TsapSrc;
            const ushort dstTsapCode     = 0xc202;
            var          dstTsapContent  = r_message.TsapDst;

            return(Format(head, len, contentLen, typeCode, dstRef, srcRef, reserved, tdpuSizeCode, tdpuSizeContent,
                          srcTsapCode, srcTsapContent, dstTsapCode, dstTsapContent));
        }
Exemplo n.º 13
0
        /// <summary>
        ///     发送协议,通过传入需要使用的协议内容和输入结构
        /// </summary>
        /// <param name="unit">协议的实例</param>
        /// <param name="content">输入信息的结构化描述</param>
        /// <returns>输出信息的结构化描述</returns>
        /// <typeparam name="T">IOutputStruct的具体类型</typeparam>
        public virtual async Task <T> SendReceiveAsync <T>(ProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct
        {
            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 <T>(receiveContent, ref t));
                }
            }
            return(null);
        }
Exemplo n.º 14
0
        public override byte[] Format(IInputStruct message)
        {
            var r_message = (ReadRequestOpcInputStruct)message;

            return(Format((byte)0x00, Encoding.UTF8.GetBytes(r_message.Tag)));
        }
Exemplo n.º 15
0
 /// <summary>
 ///     从输入结构格式化
 /// </summary>
 /// <param name="message">结构化的输入数据</param>
 /// <returns>格式化后的字节流</returns>
 public abstract TParamIn Format(IInputStruct message);
Exemplo n.º 16
0
 /// <summary>
 ///     发送数据并接收
 /// </summary>
 /// <param name="unit">发送的数据</param>
 /// <param name="content">协议的参数</param>
 /// <returns>返回的数据</returns>
 public override async Task <IOutputStruct> SendReceiveAsync(ProtocalUnit unit, IInputStruct content)
 {
     if (ProtocalLinker != null && ProtocalLinker.IsConnected)
     {
         return(await base.SendReceiveAsync(unit, content));
     }
     if (_connectTryCount > 10)
     {
         return(null);
     }
     return
         (await
          await
          ConnectAsync()
          .ContinueWith(answer => answer.Result ? base.SendReceiveAsync(unit, content) : null));
 }
Exemplo n.º 17
0
 /// <summary>
 ///     从输入结构格式化
 /// </summary>s
 /// <param name="message">结构化的输入数据</param>
 /// <returns>格式化后的字节流</returns>
 public abstract byte[] Format(IInputStruct message);
Exemplo n.º 18
0
 private async Task <IOutputStruct> ForceSendReceiveAsync(ProtocalUnit unit, IInputStruct content)
 {
     return(await base.SendReceiveAsync(unit, content));
 }
Exemplo n.º 19
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));
 }
Exemplo n.º 20
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)));
 }
Exemplo n.º 21
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)));
 }