110,499
社区成员
发帖
与我相关
我的任务
分享
class Program
{
[ThreadStatic]
private static int Index = 0;
SerialPort spSend; //spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据
static SerialPort spReceive; //spReceive接受数据
delegate void UpdateTextEventHandler(string text); //委托,此为重点
static UpdateTextEventHandler updateText;
static void Main(string[] args)
{
updateText = new UpdateTextEventHandler(UpdateTextBox); //实例化委托对象
//spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明
spReceive = new SerialPort("COM3", 9600, Parity.None);
spReceive.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.Open();
Console.ReadKey();
}
private static void spReceive_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
spReceive.Close();
updateText("无");
}
private static void UpdateTextBox(string text)
{
spReceive.Close();
throw new NotImplementedException();//只要被DataReceived事件处理方法直接调用的方法出现异常就会导致程序无限卡在抛异常的地方,调试时无法执行其它方法
}
}