Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3453718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:24:27+00:00 2026-05-18T09:24:27+00:00

I’m writing an app in c# that utilizes the SerialPort class to communicate with

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:24:28+00:00Added an answer on May 18, 2026 at 9:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.