/// <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 = 0, len, pEtx, totalLen; while ((pStx = data.IndexOf(RSTX, pStx)) != -1) { if (data.Count <= pStx + 1) { WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return null; } len = data[pStx + 1]; pEtx = pStx + len + 3; totalLen = len + 4; if (data.Count <= pEtx) { WriteLog.Console("Parse2", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return null; } if (data[pEtx] != RETX) { WriteLog.Console("Parse3", DataStatue.SETXFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); /*pStx += 1*/data.RemoveAt(0); continue; } if (data[pEtx - 1] != MakeCrc(data, pStx, totalLen - 2)) { WriteLog.Console("Parse4", DataStatue.CrcFail.ToString() + " " + ByteConverter.ToHexString(data)); pStx += 1; continue; } output.Add(data.GetRange(pStx, totalLen)); if (pStx != 0) { data.RemoveRange(0, pStx); WriteLog.Console("Parse5", DataStatue.DataFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); pStx = 0; } data.RemoveRange(pStx, totalLen); } return output; }