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 🙂
Since
SerialPortimplementsIDisposable, you should write your code like this:This will make sure that at the end of the
usingblock, the serial port is freed, and if an exception occurs inside theusingblock it’s freed to, since theusingblock is the exact same as a try/finally block, that closes/disposes theSerialPortinside the finally block.EDIT
According to OP’s needs the
SerialPortshould 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
IDisposableitself, and dispose the SerialPort inside your ownDisposemethod.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:
The problem here is that if
sendfunccauses an exception in some way, it will never be disposed. What you gain from implementingIDisposablein the first place, is that you can change your code to look like this:Now you are guaranteed that
Disposewill be called for your com port regardless of any exceptions inside theusingblock.