我是用SerialPort的DataReceived事件:
public void spReceive_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 这样读取的:
int readLength = this.serialPort1.BytesToRead; 
int offset = 0;
byte[] readBuffer = new byte[readLength];
this.serialPort1.Read(readBuffer, offset, readLength); 但是经常发现readBuffe里边不只一帧数据,有时候下位机发的多了有好多帧数据到串口缓冲区里。我还设置了serialPort1.ReceivedBytesThreshold = 5;但是这个好像只有第一次触发函数的时候有效,后边就不起作用了。请问:怎么才能弄成串口缓冲区每收到10个数据就自动触发函数呢?

解决方案 »

  1.   

    最简单的方法就是不用DataReceived事件,直接开一个新线程while(true)
    {
         this.serialPort1.Read(readBuffer, offset, readLength);
         //...........
    }  DataReceived本来是提供方便的,却因为不当使用带来更多麻烦了。
      

  2.   

    while(true)
    {
         this.serialPort1.Read(readBuffer, offset, readLength);
         //...........
    }  
    不停的读数?这个是多少字节读一次数呢
      

  3.   

    我要实现的是这样一个事情:
    我给下位机一个起始指令,然后下位机开始不断的发数据给上位机,但是我每次读到的串口buffer都有很多帧数据。怎么处理呢?
    我想能不能每收到一帧数据就读出来处理?
    不想针对一大块数据进行处理。
      

  4.   

    看我博客,有介绍。
    http://blog.csdn.net/wuyazhe/category/695097.aspx