Reputation: 315
I am a bit new to C# and I recently ran into an issue receiving an "Invalid Operation Exception" when my serial connection is lost during a serial communication operation. I am trying to catch the error via private void port_ErrorReceived
(see below) but I keep receiving an error stating "does not contain a constructor that takes 1 arguments".
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);
}
}
Here is where I call my new event handler:
comport.ErrorReceived += new SerialErrorReceivedEventArgs(port_ErrorReceived);
I've seen some similar posts on StackOverflow but I wasn't quite sure what applied to this scenario. Any help would be appreciated. Thank you.
Upvotes: 0
Views: 2021
Reputation: 1062780
I think you mean simply:
comport.ErrorReceived += port_ErrorReceived;
or more verbosely:
comport.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
(but they are identical; there's no reason not to use the first version)
Upvotes: 1