我用serialPort写了一个简单的串口调试程序,可以发送数据,但是每次一有数据回来就出现“读取超时”异常,下面是我写的代码
[AppleScript] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//加载窗体时对串口初始化
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.StopBits = System.IO.Ports.StopBits.One;
serialPort1.Parity = System.IO.Ports.Parity.None;
serialPort1.DataBits = 8;
serialPort1.Open();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.WriteLine( textBox1.Text);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.textBox2.Invoke(new MethodInvoker( delegate { textBox2.Text += serialPort1.ReadLine(); }));
}
}
}
我在属性中设置了serialPort的读取超时为500毫秒,下面是截图,
求高手指教
|