예제 #1
0
 /// <summary>
 /// 创建协议包
 /// </summary>
 /// <param name="cmd">命令</param>
 /// <param name="sendData">发送数据</param>
 /// <param name="aimType">类型</param>
 /// <param name="moduleAddr">模块地址</param>
 /// <returns></returns>
 public static byte[] BuildParamPackage(
     byte cmd,
     List <SmSend> sendData,
     byte aimType           = (byte)SmFrame.IndustryAimType,
     List <byte> moduleAddr = null)
 {
     if (moduleAddr == null)
     {
         moduleAddr = new List <byte>(8)
         {
             0, 0, 0, 0, 0, 0, 0, 0
         };
     }
     return(SmPackage.BuildParamPackage(moduleAddr, cmd, sendData, aimType));
 }
예제 #2
0
파일: SmAnalysis.cs 프로젝트: icprog/HmiPro
        /// <summary>
        /// 缓存数据==>可用的包
        /// 对ProcessBuffer函数进行了极大的优化
        /// 目前效率 250次循环 61ms 50个单元单元测试包
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private Dictionary <SmPackageType, List <byte[]> > processBuffer2(byte[] buffer, int offset, int count)
        {
            //解密
            buffer = SmEncrypt.DecodeArray(buffer, offset, count);

            Dictionary <SmPackageType, List <byte[]> > packageDictionary = new Dictionary <SmPackageType, List <byte[]> >();

            try {
                //fixed: 2018-01-04
                // 某些机台这里会抛异常
                socketBuffer.WriteBuffer(buffer, offset, count);
            } catch (Exception e) {
                //Console.WriteLine("DynamicBuffer WriteBuffer 异常" + e.Message);
                Logger.Error($"DynamicBuffer WriteBuffer 异常: DataCount: {socketBuffer.GetDataCount()},BufferSize: {socketBuffer.BufferSize}", e);
                //清空缓存
                socketBuffer.Clear();
            }
            List <byte[]> normalPackages         = new List <byte[]>();
            List <byte[]> heartbeatPackages      = new List <byte[]>();
            List <byte[]> clientReplyCmdPackages = new List <byte[]>();
            SmPackageType smPackageType          = SmPackageType.ErrorPackage;
            int           startFrameCount        = 0;

            for (int i = offset; i < offset + socketBuffer.GetDataCount(); ++i)
            {
                //检验帧头
                if (socketBuffer[i] == (byte)SmFrame.Start)
                {
                    ++startFrameCount;
                    //找到长度帧
                    byte[] datalenBytes = { socketBuffer[i + (int)SmIndex.TotalLenStart], socketBuffer[(int)SmIndex.TotalLenStart + 1 + i] };
                    datalenBytes = datalenBytes.Reverse().ToArray();
                    //数据长度(该位后面(不包括结束符,CRC)的长度)
                    Int16 propLen = BitConverter.ToInt16(datalenBytes, 0);
                    //整个包长度
                    int packageLen =
                        +(int)SmIndex.TotalLenStart
                        + (int)SmIndex.TotalLenCount
                        + propLen
                        + 3;
                    int index = i + packageLen - 1;
                    //加上长度是否为结束帧
                    if (propLen > 0 && index < socketBuffer.GetDataCount() &&
                        socketBuffer[index] == (byte)SmFrame.End)
                    {
                        //获取包类型
                        smPackageType = SmPackage.GetPackageType(socketBuffer.Buffer, i, packageLen);
                        if (smPackageType != SmPackageType.ErrorPackage)
                        {
                            byte[] bytes = copyPackageAndClearOrigin2(ref i, packageLen);
                            if (smPackageType == SmPackageType.ParamPackage)
                            {
                                normalPackages.Add(bytes);
                            }
                            else if (smPackageType == SmPackageType.HeartbeatPackage)
                            {
                                heartbeatPackages.Add(bytes);
                            }
                            else if (smPackageType == SmPackageType.ClientReplyCmd)
                            {
                                clientReplyCmdPackages.Add(bytes);
                            }
                        }
                    }
                    else
                    {
                        ////缓存中出现了超过1000个字节
                        ////全删掉
                        if (startFrameCount >= 1000)
                        {
                            //socketBuffer.Clear(0, i);
                            Console.WriteLine("缓存中超过1000个坏包");
                        }
                    }
                }
            }
            //存包
            packageDictionary[SmPackageType.ParamPackage]     = normalPackages;
            packageDictionary[SmPackageType.HeartbeatPackage] = heartbeatPackages;
            packageDictionary[SmPackageType.ClientReplyCmd]   = clientReplyCmdPackages;
            return(packageDictionary);
        }
예제 #3
0
 /// <summary>
 /// 检查包是否满足协议
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 public static bool AsserIsPackage(byte[] buffer, int offset, int count)
 {
     return(SmPackage.AsserIsPackage(buffer, offset, count));
 }
예제 #4
0
 /// <summary>
 /// 获取包类型
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="offset"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 public static SmPackageType GetPackageType(byte[] buffer, int offset, int count)
 {
     return(SmPackage.GetPackageType(buffer, offset, count));
 }
예제 #5
0
 /// <summary>
 /// 构建报警命令报警包
 /// </summary>
 /// <param name="addr"></param>
 /// <param name="operate">可设置打开或者关闭报警</param>
 /// <param name="aimType"></param>
 /// <returns></returns>
 public static byte[] BuildAlarmPackage(List <byte> addr, SmAction operate,
                                        byte aimType = (byte)SmFrame.IndustryAimType)
 {
     return(SmPackage.BuildAlarmPackage(addr, operate, aimType));
 }