【发布时间】:2014-02-04 18:01:46
【问题描述】:
我对 C# 有点陌生,当我的串行连接在串行通信操作期间丢失时,我最近遇到了一个接收“无效操作异常”的问题。我试图通过private void port_ErrorReceived(见下文)捕获错误,但我不断收到错误消息,指出“不包含带 1 个参数的构造函数”。
private void port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
bool error = false;
// Check if the comport is closed
if (!comport.IsOpen)
{
try
{
// Try to open the port
comport.Open();
}
catch (UnauthorizedAccessException) { error = true; }
catch (IOException) { error = true; }
catch (ArgumentException) { error = true; }
catch (InvalidOperationException) { error = true; }
if (error) MessageBox.Show(this, "No serial port identified. Please check your connection.", "Serial Connection Lost", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
这里是我调用我的新事件处理程序的地方:
comport.ErrorReceived += new SerialErrorReceivedEventArgs(port_ErrorReceived);
我在 StackOverflow 上看到过一些类似的帖子,但我不太确定适用于这种情况的内容。任何帮助,将不胜感激。谢谢。
【问题讨论】:
标签: c# exception-handling invalidoperationexception