SerialPort的Close函数是否可能引发异常。如何安全关闭串口?
try
{
serial.Open();
serial.DataReceived += ReceiveHex;
// do sth
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
// is it possible that Exception is raised here?
serial.DataReceived -= ReceiveHex;
serial.Close();
}
答案 0 :(得分:0)
是SerialPort.Close()可以抛出IOException。请查看此处的文档:https://msdn.microsoft.com/de-de/library/system.io.ports.serialport.close%28v=vs.110%29.aspx
但我从来没有这样的事情。我认为你的代码是安全的。