日期:2014-05-17 浏览次数:20917 次
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{//串口的波特率为9600
List<byte> received=new List<byte>() ;
int loopCount=0;//检测共分几次读取串口
while (serialPort1.BytesToRead > 0)
{
loopCount++;
int len = serialPort1.BytesToRead;
byte[] temp = new byte[len];
serialPort1.Read(temp, 0, len);
received.AddRange(temp);
Thread.Sleep(10);//延迟10ms,等待串口的数据缓冲好
}
//其余处理代码
}
serialPort = new System.IO.Ports.SerialPort();
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived);
..........
serialPort.Open();
void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal;//调高优先级,保证线程不会被其它线程阻塞。
if (e.EventType == System.IO.Ports.SerialData.Chars)
{
while (serialPort.BytesToRead