C#winform串口接收高频数据时并刷新UI非常慢,但是用XCOM串口助手却能很快的实时刷新,请问有那位大佬能帮忙解决一下吗?


```我用32单片机串口很快的向上发送数据,用WINFORM串口接收后进行处理并更新,结果发现当单片机发送的数据非常快时(用示波器测得1.5ms一帧数据)winform的串口接收就不行了,刷新非常慢,实时的数据存在缓冲区,过很久才刷新出来,但是我用正点原子的XCOM串口助手接收数据,虽然很快,却能实时刷新,本人学生党,希望哪位大佬能帮帮忙,感谢!

winform串口接收代码如下:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string str;
f2.BeginInvoke((EventHandler)(delegate //this.Invoke是跨线程访问ui的方法,也是文本的范例
{
str = f2.serialPort1.ReadTo("A");
Data_Deal(str);//数据处理 显示并画图
// f2.serialPort1.DiscardInBuffer();
}));
}
```private void Data_Deal(String str)
{
Double data1;
Double data2;
if (str[0] == 'a')
{
if (str[1] == 43)
{
data1 = 1 * ((str[2] - 48) * 10 + (str[3] - 48) + (str[4] - 48) * 0.1);
f2.label1.Text = "+" + str[2] + str[3] + "." + str[4] + "dBm";
}
else
{
f2.label1.Text = "-" + str.Substring(2, 2) + "." + str[4] + "dBm";
data1 = -1 * ((str[2] - 48) * 10 + (str[3] - 48) + (str[4] - 48) * 0.1);
}
label21.Text = f2.label1.Text;
data2 = 1 * ((str[5] - 48) * 100 + (str[6] - 48) * 10 + (str[7] - 48) + (str[8] - 48) * 0.1 + (str[9] - 48) * 0.01);//56789
if (str[10] == 'u')
f2.label2.Text = Convert.ToString(data2) + "uW";
else if (str[10] == 'm')
f2.label2.Text = Convert.ToString(data2) + "mW";
else if (str[10] == 'w')
f2.label2.Text = Convert.ToString(data2) + "W";
label22.Text = f2.label2.Text;
f2.chart1.Series[0].Points.AddXY(x, data1);

            if (Max_Data1 == 0)
            {
                Max_Data1 = data1;
                f2.label7.Text = f2.label1.Text;
                label23.Text = f2.label7.Text;
            }
            else
            {
                if (data1 > Max_Data1)
                {
                    Max_Data1 = data1;
                    f2.label7.Text = f2.label1.Text;
                    label23.Text = f2.label7.Text;
                }
            }
            if (Max_Data2 == 0)
            {

                Max_Data2 = data2;
                f2.label8.Text = f2.label2.Text;
                label24.Text = f2.label8.Text;
            }
            else
            {
                if (str[10] == 'm')
                    data2 *= 1000;
                else if (str[10] == 'w')
                    data2 *= 1000000;
                if (data2 > Max_Data2)
                {
                    Max_Data2 = data2;
                    f2.label8.Text = f2.label2.Text;
                    label24.Text = f2.label8.Text;
                }
            }
            x++;
            if (x == 42)
            {
                x = 0;
                f2.chart1.Series[0].Points.Clear();
            }
            textBox21.Text = (str + "A");
        }
        else if (str[0] == 'R')
        {
            textBox1.Text = str.Substring(1, 4);
            textBox2.Text = str.Substring(5, 5);

            textBox3.Text = str.Substring(10, 4);
            textBox4.Text = str.Substring(14, 5);

            textBox5.Text = str.Substring(19, 4);
            textBox6.Text = str.Substring(23, 5);

            textBox7.Text = str.Substring(28, 4);
            textBox8.Text = str.Substring(32, 5);

            textBox9.Text = str.Substring(37, 4);
            textBox10.Text = str.Substring(41, 5);

            textBox11.Text = str.Substring(46, 4);
            textBox12.Text = str.Substring(50, 5);

            textBox13.Text = str.Substring(55, 4);
            textBox14.Text = str.Substring(59, 5);

            textBox15.Text = str.Substring(64, 4);
            textBox16.Text = str.Substring(68, 5);

            textBox17.Text = str.Substring(73, 4);
            textBox18.Text = str.Substring(77, 5);
            textBox19.AppendText(str);

        }
    }

查看全部
qq_40934715
司徒顾旋
2020/08/27 20:06
  • c#
  • visual studio
  • 点赞
  • 收藏
  • 回答
    私信

2个回复