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

  • SEARCH
  • Home
  • 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 8445725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:43:54+00:00 2026-06-10T09:43:54+00:00

Here is the code which reads data from a serial port. To keep the

  • 0

Here is the code which reads data from a serial port. To keep the things simple, let’s do it on a button click;

    private System.IO.Ports.SerialPort serialPort;

    private void button1_Click(object sender, EventArgs e)
    {
        if (serialPort == null)
            serialPort = new SerialPort("COM7", 4800, Parity.None, 8, StopBits.One);
            //COM7 is hard coded just for the sake of example

        if (!serialPort.IsOpen)
            serialPort.Open();

        textBox1.AppendText(serialPort.ReadExisting());
    }

The code runs perfectly fine unless my laptop goes to sleep. When the system wakes-up from sleep, the serialPort object is not null but serialPort.IsOpen returns false and I get the error, “The requested resource is in use.” while calling serialPort.Open(). There is nothing in the inner exception.enter image description here

I have tried many things with serialPort object like closing, disposing or explicitly assigning it to null and reinitialization but it’s of no use, same error on same line every time.

        if (!serialPort.IsOpen)
        {
            try
            {
                serialPort.Open();
            }
            catch
            {
                serialPort.Close();
                serialPort.Dispose();
                serialPort = null;

                serialPort = new SerialPort("COM7", 4800, Parity.None, 8, StopBits.One);
                serialPort.Open();
            }
        }

I also have tried serialPort.DataReceived event but the event is not fired once the system woke-up from sleep.

The only workaround is to stop the application and run it again. I will really appreciate if anyone give me a clue to sort it out in a decent manner.

  • 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-06-10T09:43:56+00:00Added an answer on June 10, 2026 at 9:43 am

    As I suggested in comment the workaround could be made by using SystemEvents.PowerModeChanged event to detect when system is about to suspend. When that state is detected you need to close serial port and reopen it when operating system is about to resume from a suspended state.

    hamad confirmed that this workaround works.

    Here is the code.

    SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
    
    void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        if (e.Mode != PowerModes.Resume)
            serialPort.Close();
    }
    

    Also note from documentation of event:

    Caution

    Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result.

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

Sidebar

Related Questions

Let's say I want to have a function which reads data from the SerialPort
I have a class which reads data from port through TcpClient. I've created 2
Here is my code which is doing the conversion from hex to decimal. The
In a function that reads data (data meaning exclusively strings) from disk, which should
I'm downloaded the code from John Papa's book here: http://silverlight-data.com/ and am sucessfully running
I have an app which reads some data from JSON, and I have 2
I have some socket connection code that makes use of boost::asio which reads from
I am required to create a program which reads in data from a .cvs
I have a SL application that reads some data from the database, which I
I have C# WPF application which reads data from database then does some work.

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.