日期:2014-05-17  浏览次数:20994 次

C#串口接收大量数据,事件触发设置
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)  
        {
               int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致  
                byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据  
                received_count += n;//增加接收计数  
                comm.Read(buf, 0, n);//读取缓冲数据  
                builder.Clear();//清除字符串构造器的内容  
                //因为要访问ui资源,所以需要使用invoke方式同步ui。  
                this.Invoke((EventHandler)(delegate  
                {  
                    //直接按ASCII规则转换成字符串  
                        builder.Append(Encoding.ASCII.GetString(buf));  
                   
                    //追加的形式添加到文本框末端,并滚动到最后。  
                    this.txGet.AppendText(builder.ToString()); 
                  this.txGet.AppendText(“\n”); 
                    //修改接收计数  
                    labelGetCount.Text = "Get:" + received_count.ToString();  
                }));
}


串口的数据是以20hz频率发送形如#HEADINGA,COM1,0,19.5,FINESTEERING,1751,41243.050,00000000,9722,10985;SOL_COMPUTED,NARROW_INT,4.269080639,247.762725830,2.489746809,0.0,0.068533905,0.116395399,"AAAA",14,14,14,13,0,01,0,33*1788d10a