#region【方法:解析数据线程】 private void DataParse() { int waittime = 0;//等待时间 bool isExit = false;//是否超时退出 byte[] headData = new byte[4];//命令头 byte[] cmdData = new byte[19];//命令 while (true) { try { if (m_RevQueue.Count > headData.Length) { isExit = false; while (m_RevQueue.Count < 4)//小于命令头长度,进入等待状态,2s超时退出 { waittime++; if (waittime > 100) { isExit = true; break; } Thread.Sleep(1); } headData = new byte[4];//命令头 while (true) { while (m_RevQueue.Count < 1)//小于命令头长度,进入等待状态,2s超时退出 { waittime++; if (waittime > 100) { isExit = true; break; } Thread.Sleep(1); } //如果获取字符与头字符不一致则每位向后移一位 if (headData[0] != 0xAA|| headData[1] != 0x01 || headData[2] != 0x01 || headData[3] != 0x01 ) { headData[0] = headData[1]; headData[1] = headData[2]; headData[2] = headData[3]; headData[3] = m_RevQueue.Dequeue(); continue; } break; } while (m_RevQueue.Count < 19)//小于命令长度,进入等待状态,2s超时退出 { waittime++; if (waittime > 100) { isExit = true; break; } Thread.Sleep(1); } waittime = 0; //Console.WriteLine(headData[3].ToString("X2")); if (isExit) continue; cmdData = new byte[23]; 取出后面的19位 for (int i = 0; i < 19; i++)//取出命令 { cmdData[i + 4] = m_RevQueue.Dequeue(); } //把头字符复制到cmdData; headData.CopyTo(cmdData, 0); GCByteToModel(m_DeviceModel, cmdData); } else { Thread.Sleep(10); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } #endregion