views:

340

answers:

1

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:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Text;

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  A: 

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.

Aviad P.
Yes, your right! That seemed to have solved the premature thread exit problem. It has been running continuously without stopping now.
John Sheares
But, the "ObjectDisposedException Safe Handle has been closed" still occurs.
John Sheares
Zoom out a little bit from the code that creates the DeviceSerialPort object. Is it kept alive for the duration of the application?
Aviad P.
Ok, I think the problem is solved now. I was creating the DeviceSerialPort object as a local variable in the initialization function! To keep it alive, I turned the SerialPort variable into a private field that's in my form. Thanks for your help!
John Sheares