10

This is a problem I used to have all the time with the serial port class in .NET 2.0. It was suggested that upgrading to .NET 4 would fix the problem... and it did in almost all cases.

If I am using the serial port class built-in to .NET to communicate with a USB-to-serial adapter, and the adapter is unexpectedly unplugged while the port is open, on occasion I get an unhandled exception:

Application: test.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean ByRef)
   at System.StubHelpers.StubHelpers.SafeHandleAddRef(System.Runtime.InteropServices.SafeHandle, Boolean ByRef)
   at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(Microsoft.Win32.SafeHandles.SafeFileHandle, System.Threading.NativeOverlapped*, Int32 ByRef, Boolean)
   at System.IO.Ports.SerialStream+EventLoopRunner.WaitForCommEvent()
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

Again, most of the time it works fine. In fact, I can't even reproduce the problem on my own computers. This happens to a friend of mine. Is there any way at all to trap this error?

Edit: I was able to reproduce the behavior myself. I am experimenting now, but still am stumped as to what causes this error in .NET 4.0.

Edit 2: This seems to only happen on Windows 7. Unplugging and plugging in the USB-to-serial adapter on XP works beautifully.

Brad
  • 159,648
  • 54
  • 349
  • 530

4 Answers4

6

It seems that this bug won't be fixed by Microsoft any time soon, and that there is no good workaround. I've spent over a year working on this problem off-and-on.

The solution for me was to use a 3rd party component. After testing 15+, I've found that the only one that really works is CommStudio.

The free express version is here: http://www.componentsource.com/products/commstudio/downloads.html?rv=42917

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Ridiculous answer. This software costs US $783. Just to solve one tiny problem. – Elmue Nov 29 '18 at 22:07
  • @Elmue Actually, it's free... or was 7 years ago when this answer was written. I bet if you spent 5 minutes Googling, you could find that Express version again. There's also absolutely nothing ridiculous about it. I tested a massive amount of these things with faulty hardware and this was the *only* component that was able to handle it reliably. The only thing ridiculous is your reaction to me trying to help you. I'm not selling this, it isn't my code, if you don't want to use it then don't! It's pretty simple. – Brad Nov 29 '18 at 22:10
4

Adding the following to app.config

<runtime>
 <legacyUnhandledExceptionPolicy enabled="1"/>
</runtime>

has worked for me. See Problem with SerialPort

Community
  • 1
  • 1
Justin Wignall
  • 3,490
  • 20
  • 23
2

(I know this is a 2-year old question, but it looks like the problem is still relevant, so I'm posting some code I found that I think could be useful.)

I had a similar problem in a .NET 2.0 VB project. Often when disconnecting an USB-to-serial adapter I would get an "UnauthorizedAccessException" that crashed the program.

I haven't had the chance to port that project to .NET 4.x yet. Also, I understand the exception I was seeing is not the same as that mentioned by the OP, but I think the underlying problem might be the same, so I'd like to share a solution I found.

http://go4answers.webhost4life.com/Example/unauthorizedaccessexception-6130.aspx

The code towards the bottom of that page solved the problem for me. Here's the actual VB.NET class I used:m

Option Explicit On
Option Strict Off           ' so we can use late binding (1)

Imports System.IO.Ports

''' <summary>
''' This is a drop-in replacement for .net built-in SerialPort class.
''' It avoids "UnauthorizedAccessException" errors that sometimes happen
''' when disconnecting and reconnecting USB-to-serial adapters.
''' </summary>
''' <remarks>http://go4answers.webhost4life.com/Example/unauthorizedaccessexception-6130.aspx</remarks>

Public Class ExSerialPort

    Inherits SerialPort

    Public Sub New()
        MyBase.New()
    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Dim mytype As Type = GetType(SerialPort)
        Dim field As Reflection.FieldInfo = mytype.GetField("internalSerialStream", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
        Dim stream As Object = field.GetValue(Me)

        If Not stream Is Nothing Then
            Try
                stream.Dispose()   ' (1)
            Catch ex As Exception
                ' do nothing
            End Try
        End If
        MyBase.Dispose(disposing)
    End Sub

End Class
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Marcello Romani
  • 2,967
  • 31
  • 40
1

I also had this problem, but I found a .NET library which fires a Faulted event, when the adapter is unplugged and after that it disconnects normally. You can reconnect any time, when the adapter is plugged back. The name of the library is ZylSerialPort.NET.

Zilord
  • 11
  • 1