Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing up Serial Port properly

I'm writing an app in c# that utilizes the SerialPort class to communicate with a few devices. Now the big problem I've been encountering all the time is how to properly free up resources there, since you immediately get an exception when trying to use a serial port that is already being used.

Since normally the GC should take care of most of the work I'm kinda out of ideas what else to try...

Mainly I tried 2 things which (in my logic) should do the work. I use session based communication, so I call an OpenPort and a ClosePort method before and after every communication - so the port should be closed. Also I've tried setting my object containing the port to null afterwards - but still I get UnauthorizedAccessExceptions all the time - although I am 100 percent sure that the SerialPort.Close() method has been called.

Do you guys know any better ways of freeing up the ports so I stop getting that exception?

EDIT: Thanks for the answers but the Dispose() stuff isn't working - I had tried that before - maybe I'm doing something wrong though so here's an example what my code looks like:

It's actually quite like Øyvind suggested, though I just added the IDisposable - but doesn't work either:

So this would be my wrapper class:

class clsRS232 : IDisposable
{
  public void clsRS232()
  {
    Serialport port = new Serialport("COM1",9600,Parity.none,8,Stopbits.one);
  }
  public void openPort()
  {
     port.Open();
  }
  public void sendfunc(string str)
  {
    port.Write(str);
  }
  public string readfunc()
  {
    port.ReadTo("\n");
  }

  public void Dispose()
  {
     port.Dispose();
  }

}

Now whenever I need rs232 communication I call a new instance like this:

   clsRS232 test = new clsRS232;
   test.openport();
   test.sendfunc("test");
   test.Dispose();

But that doesn't change anything - I still get lots of UnauthorizedAccessExceptions - and if the other guy was right (that Dispose() of SerialPort-class only contains SerialPort.Close() ) - well then I guess I haven't really changed anything from my earlier approach, where I had a function call close();

thanks for your answers - still hoping to find the solution :)

like image 879
Lorenz Avatar asked Dec 06 '10 09:12

Lorenz


3 Answers

Since SerialPort implements IDisposable, you should write your code like this:

using( SerialPort port = new SerialPort( ... ) ){
  //do what you need with the serial port here
}

This will make sure that at the end of the using block, the serial port is freed, and if an exception occurs inside the using block it's freed to, since the using block is the exact same as a try/finally block, that closes/disposes the SerialPort inside the finally block.

EDIT

According to OP's needs the SerialPort should remain open longer than the timeframe of a method.

In that case I would wrap the entire logic that has to do with the serial port inside it's own class. In the constructor of the class, open the serial port, and write methods to do the operations you need. Then have this class implement IDisposable itself, and dispose the SerialPort inside your own Dispose method.

This will give you much better control on where you have to open and close/dispose the serial port and wrap the serial port logic into a proper class.

If you want to keep the port open for a period of time not easily contained by a block of code, then you will have to manually dispose it when you are finished with it, for example when the function that uses it gets closed or whatever triggers the release of the com port in your program.

EDIT 2

Your current implementation is like this:

clsRS232 test = new clsRS232;
test.openport();
test.sendfunc("test");
test.Dispose();

The problem here is that if sendfunc causes an exception in some way, it will never be disposed. What you gain from implementing IDisposable in the first place, is that you can change your code to look like this:

using( clsRS232 test = new clsRS232 ){
 test.openport();
 test.sendfunc("test");
}

Now you are guaranteed that Dispose will be called for your com port regardless of any exceptions inside the using block.

like image 195
Øyvind Bråthen Avatar answered Oct 06 '22 20:10

Øyvind Bråthen


I know this is very old, but I just came across the same issue and this solution worked for me, although it is a bit hacky. According to this thread why is access to com port denied? the issue is with a bug in the SerialPortClass. I created a wrapper class that only opens the port once, and create the class for the lifetime of the application. The SerialPort is then disposed in the Dispose of the class but is opened using the following :

  private SerialPort KickerPort { get; set; }
    .
    .
    .
private bool OpenPort()
        {
            //https://stackoverflow.com/questions/7219653/why-is-access-to-com-port-denied
            //due to a bug in the SerialPort code, the serial port needs time to dispose if we used this recently and then closed
            //therefore the "open" could fail, so put in a loop trying for a few times
            int sleepCount = 0;
            while (!TryOpenPort())
            {
                System.Threading.Thread.Sleep(100);
                sleepCount += 1;
                System.Diagnostics.Debug.Print(sleepCount.ToString());
                if (sleepCount > 50) //5 seconds should be heaps !!!
                {
                    throw new Exception(String.Format("Failed to open kicker USB com port {0}", KickerPort.PortName));
                }
            }
            return true;
        }
     private bool TryOpenPort()
                {
                    if (!KickerPort.IsOpen)
                    {
                        try
                        {
                            KickerPort.Open();
                            return true;
                        }
                        catch (UnauthorizedAccessException)
                        {
                            return false;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                    }
                    return true;
                }

this is called by :

 try
            {
                if (OpenPort())
                {
                    //do your thing here !
                }
                return false;
            }
            catch (Exception ex)
            {  
                throw ex;
            }

In my Tests ( I used it to open a cash drawer on a USB kicker) I found that sometimes it opened first time and other times it went through the sleep loop about 20 times depending on how recently the

like image 21
PabloInNZ Avatar answered Oct 06 '22 19:10

PabloInNZ


The implementation proposed by Øyvind Bråthen uses the IDisposable pattern in .NET. At the end of the using block, the Dispose function of the SerialPort instance is called, which will free the associated unmanaged resources (ie the serial port)

Call port.Dispose() yourself when you want to release it.

like image 41
Eilistraee Avatar answered Oct 06 '22 20:10

Eilistraee