目前在编写一个串口数据采集的上位机,需要将读进来的数据解包,然后每多少帧取一个平均值然后保存到txt里,大致结构能用,但是在计算数据平均的时候会占用很多cpu资源,波特率时460800,连读带计算写入占用大概15%左右,能否将这个值降低到5%以下
private void serialPort_1for_Datareceived(object sender, SerialDataReceivedEventArgs e)
{
if (isStart == true && serialPort1.IsOpen)
{
int n = serialPort1.BytesToRead;//待读字节个数
byte[] buf = new byte[n];//创建n个字节的缓存
serialPort1.Read(buf, 0, n);//读到在数据存储到buf
BufA.AddRange(buf);
if (BufA.Count > 10000)
{
int i = 0;
while (i < BufA.Count)
{
if (BufA[i] == 235 && BufA[1 + i] == 144 && BufA[2 + i] == 74)
{
//acc
com_A_Data[1] = BitConverter.ToInt32(new byte[] { BufA[4 + i], BufA[5 + i], BufA[6 + i], BufA[7 + i] }, 0);
com_A_Data[2] = BitConverter.ToInt32(new byte[] { BufA[8 + i], BufA[9 + i], BufA[10 + i], BufA[11 + i] }, 0);
com_A_Data[3] = BitConverter.ToInt32(new byte[] { BufA[12 + i], BufA[13 + i], BufA[14 + i], BufA[15 + i] }, 0);
//gyro
com_A_Data[4] = BitConverter.ToInt32(new byte[] { BufA[16 + i], BufA[17 + i], BufA[18 + i], BufA[19 + i] }, 0);
com_A_Data[5] = BitConverter.ToInt32(new byte[] { BufA[20 + i], BufA[21 + i], BufA[22 + i], BufA[23 + i] }, 0);
com_A_Data[6] = BitConverter.ToInt32(new byte[] { BufA[24 + i], BufA[25 + i], BufA[26 + i], BufA[27 + i] }, 0);
//acc_temp
com_A_Data[7] = BitConverter.ToInt32(new byte[] { BufA[28 + i], BufA[29 + i], BufA[30 + i], BufA[31 + i] }, 0);
com_A_Data[8] = BitConverter.ToInt32(new byte[] { BufA[32 + i], BufA[33 + i], BufA[34 + i], BufA[35 + i] }, 0);
com_A_Data[9] = BitConverter.ToInt32(new byte[] { BufA[36 + i], BufA[37 + i], BufA[38 + i], BufA[39 + i] }, 0);
//fre
com_A_Data[10] = BitConverter.ToInt32(new byte[] { BufA[40 + i], BufA[41 + i], BufA[42 + i], BufA[43 + i] }, 0);
com_A_Data[11] = BitConverter.ToInt32(new byte[] { BufA[44 + i], BufA[45 + i], BufA[46 + i], BufA[47 + i] }, 0);
com_A_Data[12] = BitConverter.ToInt32(new byte[] { BufA[48 + i], BufA[49 + i], BufA[50 + i], BufA[51 + i] }, 0);
//amp
com_A_Data[13] = BitConverter.ToInt32(new byte[] { BufA[52 + i], BufA[53 + i], BufA[54 + i], BufA[55 + i] }, 0);
com_A_Data[14] = BitConverter.ToInt32(new byte[] { BufA[60 + i], BufA[61 + i], BufA[62 + i], BufA[63 + i] }, 0);
com_A_Data[15] = BitConverter.ToInt32(new byte[] { BufA[68 + i], BufA[69 + i], BufA[70 + i], BufA[71 + i] }, 0);
//gyro_qura
com_A_Data[19] = ((BufA[i + 58] & 0xc0) >> 6) + (BufA[i + 59] << 2);
com_A_Data[20] = ((BufA[i + 66] & 0xc0) >> 6) + (BufA[i + 67] << 2);
com_A_Data[21] = ((BufA[i + 74] & 0xc0) >> 6) + (BufA[i + 75] << 2);
//com_A_Data[19] = (BitConverter.ToInt32(new byte[] { BufA[40 + i], BufA[41 + i], BufA[42 + i], BufA[43 + i] }, 0)) >> 22;
//com_A_Data[21] = (BitConverter.ToInt32(new byte[] { BufA[44 + i], BufA[45 + i], BufA[46 + i], BufA[47 + i] }, 0)) >> 22;
//com_A_Data[23] = (BitConverter.ToInt32(new byte[] { BufA[48 + i], BufA[49 + i], BufA[50 + i], BufA[51 + i] }, 0)) >> 22;
////gyro_zj
com_A_Data[16] = ((BitConverter.ToInt32(new byte[] { BufA[56 + i], BufA[57 + i], BufA[58 + i], BufA[59 + i] }, 0)) << 10) >> 10;
com_A_Data[17] = ((BitConverter.ToInt32(new byte[] { BufA[64 + i], BufA[65 + i], BufA[66 + i], BufA[67 + i] }, 0)) << 10) >> 10;
com_A_Data[18] = ((BitConverter.ToInt32(new byte[] { BufA[72 + i], BufA[73 + i], BufA[74 + i], BufA[75 + i] }, 0)) << 10) >> 10;
com_A_Data[25] = BufA[78 + i];
if (na < Convert.ToInt32(textBox22.Text))//到达规定数据平均长度后求和
{
sumA[1] += com_A_Data[1];
sumA[2] += com_A_Data[2];
sumA[3] += com_A_Data[3];
sumA[4] += com_A_Data[4];
sumA[5] += com_A_Data[5];
sumA[6] += com_A_Data[6];
sumA[7] += com_A_Data[7];
sumA[8] += com_A_Data[8];
sumA[9] += com_A_Data[9];
sumA[10] += com_A_Data[10];
sumA[11] += com_A_Data[11];
sumA[12] += com_A_Data[12];
sumA[13] += com_A_Data[13];
sumA[14] += com_A_Data[14];
sumA[15] += com_A_Data[15];
sumA[16] += com_A_Data[16];
sumA[17] += com_A_Data[17];
sumA[18] += com_A_Data[18];
sumA[19] += com_A_Data[19];
sumA[20] += com_A_Data[20];
sumA[21] += com_A_Data[21];
na++;//计数器自加一
//求平均,保留有效位数,转字符串显示写入txt
if (na == Convert.ToInt32(textBox22.Text))
{
ComA_rec[1] = Convert.ToString(Math.Round(sumA[1] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[2] = Convert.ToString(Math.Round(sumA[2] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[3] = Convert.ToString(Math.Round(sumA[3] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[4] = Convert.ToString(Math.Round(sumA[4] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[5] = Convert.ToString(Math.Round(sumA[5] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[6] = Convert.ToString(Math.Round(sumA[6] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[7] = Convert.ToString(Math.Round(sumA[7] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[8] = Convert.ToString(Math.Round(sumA[8] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[9] = Convert.ToString(Math.Round(sumA[9] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[10] = Convert.ToString(Math.Round(sumA[10] / Convert.ToInt32(textBox22.Text) / 10000, 4));
ComA_rec[11] = Convert.ToString(Math.Round(sumA[11] / Convert.ToInt32(textBox22.Text) / 10000, 4));
ComA_rec[12] = Convert.ToString(Math.Round(sumA[12] / Convert.ToInt32(textBox22.Text) / 10000, 4));
ComA_rec[13] = Convert.ToString(Math.Round(sumA[13] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[14] = Convert.ToString(Math.Round(sumA[14] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[15] = Convert.ToString(Math.Round(sumA[15] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[16] = Convert.ToString(Math.Round(sumA[16] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[17] = Convert.ToString(Math.Round(sumA[17] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[18] = Convert.ToString(Math.Round(sumA[18] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[19] = Convert.ToString(Math.Round(sumA[19] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[20] = Convert.ToString(Math.Round(sumA[20] / Convert.ToInt32(textBox22.Text), 1));
ComA_rec[21] = Convert.ToString(Math.Round(sumA[21] / Convert.ToInt32(textBox22.Text), 1)) + "\r\n";
srA = new StreamWriter(fid_coma, true, Encoding.Default); // ture保留文件原来的内容
srA.WriteLine(ComA_rec[1] + "\t" + ComA_rec[2] + "\t" + ComA_rec[3]
+ "\t" + ComA_rec[4] + "\t" + ComA_rec[5] + "\t" + ComA_rec[6]
+ "\t" + ComA_rec[7] + "\t" + ComA_rec[8] + "\t" + ComA_rec[9]
+ "\t" + ComA_rec[10] + "\t" + ComA_rec[11] + "\t" + ComA_rec[12]
+ "\t" + ComA_rec[13] + "\t" + ComA_rec[14] + "\t" + ComA_rec[15]
+ "\t" + ComA_rec[16] + "\t" + ComA_rec[17] + "\t" + ComA_rec[18]
+ "\t" + ComA_rec[19] + "\t" + ComA_rec[20] + "\t" + ComA_rec[21]);
srA.Flush(); // 清空缓存
srA.Close(); // 关闭文件
//data.AddRange(ComA_rec);
na = 0;//计数器归零,求平均结束
Array.Clear(sumA, 0, sumA.Length);//求和循环归零
}
}
i = i + 80;//下一帧
if (i > BufA.Count - 80)//剩余最后一帧数据不解,链接下一包开头拼接
{
BufA.RemoveRange(0, i);//移除解包完毕的数据,
//srA = new StreamWriter(fid_coma, true, Encoding.Default); // ture保留文件原来的内容
//for (int x = 0; x < data.Count; x++) srA.Write(data[x] + "\t");
//data.RemoveRange(0, data.Count);
//srA.Flush(); // 清空缓存
//srA.Close(); // 关闭文件
break;
}
}
else
{
i++;
}
}
}
}
}
}