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 511873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:15:42+00:00 2026-05-13T07:15:42+00:00

I am receiving data from a device that’s sending information over the serial port

  • 0

I am receiving data from a device that’s sending information over the serial port and I get this exception: “ObjectDisposedException Safe Handle has been closed”. It may happen within seconds to several minutes and there doesn’t seem to be a pattern. Another thing that happens is that it will stop receiving data with no exception and I can see in the VS IDE output window tab that a thread has exited. I have no idea why it’s doing that also.

Here is how I create the serial port:

DeviceSerialPort serialport = new DeviceSerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
serialport.RHDataReceived += new EventHandler<DeviceDataEventArgs>(SerialPort_RHDataReceived);
serialport.Open();

And here is the serial port code:

namespace Instrument
{
    public class DeviceSerialPort
    {
        public event EventHandler<DeviceDataEventArgs> RHDataReceived;

        private SerialPort _serialPort;
        private byte[] _readBuffer = new byte[1024];

        public DeviceSerialPort(string portName, int baudRate, Parity parity, 
            int dataBits, StopBits stopBits)
        {
            _serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
            _serialPort.DataReceived +=new SerialDataReceivedEventHandler(SerialPort_DataReceived);

        }

        public void Open()
        {
            _serialPort.Open();
        }

        private void SerialPort_DataReceived(object sender, EventArgs e)
        {
            //SerialPort sp = (SerialPort)sender;

            int bytesRead = _serialPort.Read(_readBuffer, 0, _readBuffer.Length);


            if (_readBuffer[0] == 5)
            {
                onRHDataReceivedEvent(new DeviceDataEventArgs(_readBuffer[0], _readBuffer[1], _readBuffer[2], _readBuffer[3]));
            }
        }


        protected void onRHDataReceivedEvent(DeviceDataEventArgs e)
        {
            if (RHDataReceived != null)
            {
                RHDataReceived(this, e);
            }
        }

    }

    public class DeviceDataEventArgs : EventArgs
    {
        public byte Command { get; set; }
        public byte Data0 { get; set; }
        public byte Data1 { get; set; }
        public byte Crc { get; set; }

        public DeviceDataEventArgs(byte cmd, byte data0, byte data1, byte crc)
        {
            Command = cmd;
            Data0 = data0;
            Data1 = data1;
            Crc = crc;
        }
    }
}
  • 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-13T07:15:43+00:00Added an answer on May 13, 2026 at 7:15 am

    Your thread is very short lived, it lives for exactly the length of time it takes to open the serial port, and then it finishes. And when it does, your serial port gets disposed.

    This is how I see it. You’re using an event-based approach (handling the DataReceived event), in which case, you don’t need another thread, which is expensive in terms of framework and OS resources. In your case, simply get rid of the Thread object, and call serialPort.Open() directly. You will receive the DataReceived events in your existing thread.

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

Sidebar

Related Questions

No related questions found

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.