void Analysize_Data()
{
float CurrentTimeCounter = 0;
while (true)
{
bool data_1_catched = false;//缓存记录数据是否捕获到
//int CurrentTimeCounter = 0;
int buffer_len = buffer.Count;//获取buffer长度
while (buffer.Count >= 5)//至少要包含头(2字节) +功能字(1字节) +长度(1字节) +校验(1字节)
{
//1 查找数据头
if (buffer[0] == 0xAA && buffer[1] == 0xAA)//检测是否是包头
{
int len = buffer[3];//数据长度
//数据不够时移除数据
if (buffer.Count < len + 5)
{
buffer.RemoveRange(0, buffer.Count - 1);
break;
}
if (buffer[3] == 0x06) //该帧长度16字节//数据位0x06
{
Ctrl_Roll = (float)(((short)((buffer[4] << 8) | buffer[5])) / 100.0);//解析协议的,提取数据并将16进制转换为10进制,
Ctrl_Pitch = (float)(((short)((buffer[6] << 8) | buffer[7])) / 100.0);
Ctrl_Yaw = (float)(((short)((buffer[8] << 8) | buffer[9])) / 10.0);
data_1_catched = true;
buffer.RemoveRange(0, len + 4);//正确分析一条数据,从缓存中移除数据。数据移除
if (data_1_catched)
{
//更新界面
this.Invoke((EventHandler)(delegate//对监控的数据的实时曲线进行更新
{
chart1.Series[0].Points.AddXY(CurrentTimeCounter, Ctrl_Roll);
chart1.Series[1].Points.AddXY(CurrentTimeCounter, Ctrl_Pitch);
chart1.Series[2].Points.AddXY(CurrentTimeCounter, Ctrl_Yaw);
}));
}
}
else
{
buffer.RemoveAt(0);
break;
}
}
else
{ //这里是很重要的,如果数据开始不是头,则删除数据
buffer.RemoveAt(0);
}
}
}
}