我为从串行端口接收数据制作了一个事件处理程序,只有当我放置断点并且当我删除它时,它才能完美工作,当我删除它时,函数不会调用这是我的代码我已经将端口声明为 SerialPort 的对象
private static void DataReceivedHandler( object sender,SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
// sp.Open();
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
if (indata == "kitchen_light_on\r\n")
f1.update_flag("living_light", 1);
else if(indata == "kitchen_light_off\r\n")
f1.update_flag("living_light", 0);
}
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);