C#Datareceived问题 20
这是我根据网上编写的串口接收数据的程序片段,为什么接收不了呢。我设置断点逐步运行后,为什么不运行voidserialPort1_DataReceived(objectse...
这是我根据网上编写的串口接收数据的程序片段,为什么接收不了呢。我设置断点逐步运行后,为什么不运行 void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 方法呢?
private void button2_Click(object sender, EventArgs e)
{
//接收串口数据
if (isOpen)
{
try
{
//sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
//sp.DataReceived+=new SerialDataReceivedEventHandler(sp_DataReceived);
//txtRecvData.Text = sp.ReadLine() ;
sp.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
catch (Exception)
{
MessageBox.Show("读取串口数据错误");
return;
}
}
else
{
MessageBox.Show("串口未打开");
return;
}
}
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string a;
a = sp.ReadExisting();
MessageBox.Show(a);
} 展开
private void button2_Click(object sender, EventArgs e)
{
//接收串口数据
if (isOpen)
{
try
{
//sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(sp_DataReceived);
//sp.DataReceived+=new SerialDataReceivedEventHandler(sp_DataReceived);
//txtRecvData.Text = sp.ReadLine() ;
sp.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
catch (Exception)
{
MessageBox.Show("读取串口数据错误");
return;
}
}
else
{
MessageBox.Show("串口未打开");
return;
}
}
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string a;
a = sp.ReadExisting();
MessageBox.Show(a);
} 展开
2个回答
2014-06-18 · 知道合伙人数码行家
关注
展开全部
注意ReceivedBytesThreshold属性,默认值是1,表示接收缓冲区每有一个字节就促发 DataReceived事件。如果该属性数值过大,那边缓冲区的字节数需达到这个值才会促发DataReceived事件。
如果串口打开并且确认另一端串口已发送数据。且ReceivedBytesThreshold为合理的值。就会促发DataReceived事件。
如有疑问,继续追问。
如果串口打开并且确认另一端串口已发送数据。且ReceivedBytesThreshold为合理的值。就会促发DataReceived事件。
如有疑问,继续追问。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试先监听事件,再调用串口的Open
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询