Example #1
0
        /// <summary>
        /// 切割資料
        /// </summary>
        /// <param name="data">待切割資料</param>
        /// <param name="output">切割後的資料</param>
        /// <returns>資料狀態</returns>
        public static List <List <byte> > ParseData(List <byte> data)
        {
            if (data == null)
            {
                return(null);
            }
            List <List <byte> > output = new List <List <byte> >();

            int pStx, pEtx, totalLen;

            while ((pStx = data.IndexOf(RSTX)) != -1)
            {
                totalLen = 0;
                if (data.Count <= pStx + 3)
                {
                    WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                pEtx = pStx + 2;
                while ((pEtx = data.IndexOf(RETX, pEtx + 1)) != -1)
                {
                    totalLen = pEtx - pStx + 1;
                    if (data[pEtx - 1] != MakeCrcInRange(data, pStx, totalLen - 2))
                    {
                        WriteLog.Console("CRC Fail", data.GetRange(pStx, totalLen));
                        data.RemoveRange(0, pStx + 1);
                    }
                    else
                    {
                        break;
                    }
                }

                if (totalLen == 0 || totalLen > 30 || pEtx == -1)
                {
                    continue;
                }
                output.Add(data.GetRange(pStx, totalLen));

                data.RemoveRange(pStx, totalLen);
            }

            while ((pStx = data.IndexOf(KRSTX)) != -1)
            {
                totalLen = 0;
                if (data.Count <= pStx + 3)
                {
                    WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                pEtx = pStx + 2;
                while ((pEtx = data.IndexOf(RETX, pEtx + 1)) != -1)
                {
                    totalLen = pEtx - pStx + 1;
                    if (data[pEtx - 1] != MakeCrcInRange(data, pStx, totalLen - 2))
                    {
                        WriteLog.Console("CRC Fail", data.GetRange(pStx, totalLen));
                        data.RemoveRange(0, pStx + 1);
                    }
                    else
                    {
                        break;
                    }
                }

                if (totalLen == 0 || totalLen > 30 || pEtx == -1)
                {
                    continue;
                }
                output.Add(data.GetRange(pStx, totalLen));

                data.RemoveRange(pStx, totalLen);
            }
            return(output);
        }