So I've been battling this issue for about a week now and think I know the issue but I don't want to report an answer there until I have it pegged down.
In a nutshell, I get an IOException when trying to use the SerialPort.Open()
command. As it turns out, this is very common and most terminal programs actually do as well, but they simply ignore it. Please read my post above for the full tale. Now what I want to do is ignore the IOException but still open the serial port. I cannot do this with try/catch, or at least I don't know how.
I was wondering is there a way to try something and somehow state that "I know an issue will be thrown, it is a safe problem and I am choosing to ignore the exception and carry on with the task". To be clear, I don't want to ignore the error then move on. I want to ignore the error and still complete the operation.
Below is my best guess, but it doesn't work.
try
{
serialPort1.Open();
}
catch (System.IO.IOException)
{
MessageBox.Show("An IO Exception occurred.");
}
finally
{
//SAFELY IGNORE ERROR AND DO TASK ANYWAY HERE
}
If anyone could help me out with this I'd be most appreciative.
EDIT: If I just add the code serialport1.Open
afterwards I get:

This is basically the same thing that would happen without the try/catch. What I want to do is say I'm trying to do this: I don't care that it throws an error, do it anyway.
In general, if you have a line of code causing an exception, and there's additional work to be done whether or not the exception occurred, you should just use a catch
, and put the stuff you want done anyway outside (after) the try
/catch
.
For your particular case (described in your other question), you are dealing with the horrible brokenness that is .NET System.IO.Ports.SerialPort
. Opening the serial port succeeded, but SerialPort.Open
is a complicated method that does a bunch of extra stuff, and insists on closing the port if any of that extra (and unnecessary) stuff fails. I've found other problems with System.IO.Ports.SerialPort
as well (e.g. inability to open ports by their internal device name). IMO, the sooner you get rid of System.IO.Ports.SerialPort
the better.
You can either p/invoke the Win32 Serial Port functions (CreateFile
and the rest are here) or find someone who already wrote a wrapper library (I did, but it isn't available publically).
serialPort1.Open();
is what is throwing the exception. Normally when you code, if an exception happens you do clean up of the connections.
As others mentioned, if you keep calling this same method, you'll keep getting exception unless things about the call change.
So understanding that, my suggestion would be that you look for other ways to open the serial port, other than the serialPort1.Open(); call. There may or may not be built in ways in C#. So you might have to be creative :)
From SerialPort.Open:
Only one open connection can exist per SerialPort object. The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
And
The port is in an invalid state.
- or -
An attempt to set the state of the underlying port failed. For example, the parameters passed from this SerialPort object were invalid.
I bet this is what is happening... your SerialPort was recently open and now you're trying to open it again before it's ready.
My advice is to write a loop, and sleep a bit between Opening attempts. Figure out how many times you're willing to try over how long of a period. break
out of the loop when you succeed.
foreach(int i in Enumerable.Range(1, numberOfAttempts)
{
try
{
serialPort1.Open();
break;
}
catch(Exception ex)
{
//log attempt #i failed because of ex
}
Thread.Sleep(millisecondsToWait);
}
来源:https://stackoverflow.com/questions/15014137/how-to-ignore-an-exception-and-complete-the-try