Objectdisposedexception Safe Handle Has Been Closed Serial Port Average ratng: 3,8/5 3929reviews

SerialPort ObjectDisposedException ‹ ‹ Author Message Toomas Posted:.NET Base Class Library, SerialPort ObjectDisposedException I use serialport on a form to communicate with serial port scanner.

So this is a rather small question with a big explanation. As is noted by the title I am getting an unhandled exception telling me my Safe handle has been closed. What I'll probably have to do is edit this post a few times with more and more code to help me diagnose what the problem is. Warhammer 40k 5th Edition Rulebook Torrent.

I'm using POS for.NET to make a Service Object for my RFID and MSR device. Although my devices are the same, I have 2 different Virtual COM Port chips that communicate to those devices. One by Silicon labs, the other by FTDI. I wanted to use the plug and play features with POS for.NET so I gave it both my Hardware ID's. Because it is plug and play I have the full hardware path available to me which I can then create a SafeFileHandle using a call to PInvoke and using that SafeFileHandle I create a FileStream.

The FTDI chip doesn't let me talk to the devices directly like that so I have to get the friendly name of the device then use mutex to pull out the COM port then create a SerialPort instance. That step works fine and great. As a FYI I have tried to use the Friendly name of both chips to get the COM port and the Silicon Labs one (for some strange reason) doesn't get listed using SetupAPI.GetDeviceDetails using the Ports GUID. I'm not sure on that one since in Device Manager the Silicon labs Device Class Guid is the Ports GUID. Well since both the SerialPort and the FileStream have a Stream object I decided to use that to read and write to that port. The problem with that is if I send a RFID command to the MSR device the MSR device doesn't respond back with anything. So if I use this code.

This problem (and in particular the reference to a SerialPort instance) sounds suspiciously like the problem documented. As I understand it, in the case of a non-permanent SerialPort (like one associated with a USB device, for example) when the port 'goes away' unexpectedly the underlying Stream associated with it gets disposed. If there is an active read or write operation on the port at the time a subsequent call to SerialPort.Close can lead to the exception you mention, however the exception is occurring in Microsoft's code running on a different thread and cannot be caught from within your code. (It will still be seen by any 'last chance' exception handler you have bound to the UnhandledException event on the AppDomain.) There seem to be two basic workaround styles in the linked document.

Objectdisposedexception Safe Handle Has Been Closed Serial PortObjectdisposedexception Safe Handle Has Been Closed Serial Port

I am getting and error that the 'safe handle has been closed' when my code is running. Close Serial COM Port safely in C#. System.ObjectDisposedException: Safe handle. C# SerialPort System.ObjectDisposedException, safe handle has been closed in System.DLL. When the USB cable is pulled out (before the serial port is closed).

In both instances, after opening the port you store a reference to the BaseStream instance for the open port. One workaround then suppresses garbage collection on that base stream.

The other explicitly calls Close on the base stream, capturing any exceptions thrown during that operation, before calling Close on the SerialPort. EDIT: For what it's worth, under the.NET framework V4.5, it appears that none of the documented workarounds on the Microsoft Connect site fully resolve the problem although they may be reducing the frequency with which it occurs.:-(.