C#中serialPort的疑问

来源:百度知道 编辑:UC知道 时间:2024/06/21 20:29:57
c#中serialPort控件的那个DataReceived有点怪啊...
它好像执行了好几次...但怎么代码中赋值好像影响都没有...
请问这是为什么?那位大侠能给我仔细讲解下...
例如:
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] data = new Byte[serialPort.BytesToRead];
serialPort.Read(data, 0, data.Length);
string read = System.Text.Encoding.ASCII.GetString(data);
}
这个方法好像被执行了几次...但是read的值好像是累加了一样...难解啊...
求高人赐教...
就是在写串行通信时遇到的问题

你要设置一下ReceivedBytesThreshold

ReceivedBytesThreshold默认为1,表示串口收到一个字节就引发DataReceived事件,

因此假如你已经知道这次需要收到多少个字节数据的话,可以将ReceivedBytesThreshold设置成需要的值