0
votes

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: Screenshot of IOException

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.

3
It doesn't appear that serialPort1.Open() is going to successfully succeed if you just try it again. Nothing is changing at all, why would you expect different results?offthat
Are you sure issue is with the finally block and not the code in that finally block?CookieOfFortune
Are you saying that when you call Open() the serial port doesn't actually open because of the error?user959729
Yes, that is exactly what I am saying.tmwoods
You can ignore exceptions, however you will still not have the serial port open. There are cases in which you would have to have nested try/catches, however this does not look to be the case. I would recommend trying to find an alternative way to access the serial port, you'll most likely have to do something lower level.James Oravec

3 Answers

5
votes

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).

0
votes

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 :)

0
votes

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);
}