SerialPort.Close () Problem - Cannot Disable Application Using Task Manager!

This is a serious problem. Here I use the serial port in the application, and I look at the status of the connected device, opening and closing the port again.

If the device fails, DOES NOT have the SerialPort.Close () NEVER method, and this is really a show stop.

The worst part is that even closing the application using the task manager fails, it does not work (or it is forbidden, by accident).

The included device is a POS printer (USB) that mimics COM3, this is the Epson TM-T88IV (a very good model, by the way).

Do any of you have any experience?

+6
c # timeout crash serial-port
source share
6 answers

Reopening and closing the port is not recommended. See the "Notes" section in the "MSDN Library" article for SerialPort.Close (). There the background thread, which needs to be turned off before the port opens again, takes time. The amount of time is not predictable.

The Close () method can easily slow down if the DataReceived event handler is currently running. The most common way to get a deadlock is to call Control.Invoke () in the event handler. Make sure that you are not using any code in the event handler that blocks or requires switching the context of the stream. Using BeginInvoke () is fine.

The inability to kill the program is caused by a problem in the serial port device driver. Launch Taskmgr.exe, the Process tab, View + Select Columns and check "Handles". If, after killing the program, you see the β€œHandles” column showing 1, then the serial port driver hangs on an I / O request that it does not execute. A process cannot end until all of its kernel-mode threads have completed.

There is little that can be done on this particular problem, except to hope for a driver update or switching to another provider. Especially USB serial port emulators are known for having lousy device drivers. You get rid of the troublemaker, for example, taking it out in the parking lot and launching it several times using your car.

Another typical problem with USB emulators is that they are so easy to disconnect while they are in use. This works the same way as popping a flash drive from a socket while Windows is running. It would also be a good way to make a device driver damage .NET versions up to version 4.0, suffer a heart attack in the background thread when the device suddenly disappears. Briefly from the update, a small sign next to the connector that says: "Do not disconnect during use!" is a practical solution. They will still miss him a couple of times.

Fwiw, otherwise it means that the "Safely Remove Hardware" icon exists. You will get a solid "Don't do this!" if your program uses a port. But, of course, the operating system is powerless to force users to actually use it. Apple patented a method to make it fault tolerant by detecting the user's fingers on the device :)

+11
source share

Prior to .Net 4.0, a lot could go wrong with USB on SerialPort devices (program / crash system, etc.). It was easy to check for errors by pulling a USB adapter with an active port.

I recently conducted several tests using .Net 4.0 and fixed (of course, improved) ??? The test was a simple RX / TX via a USB SerialPort with a loop and pulled out a USB adapter during use. My program did not crash, and I was able to open the port again! This is a significant improvement.

+2
source share

Although sometimes the problem is that the SerialPort.Close method is blocking, this should not prevent the task manager from killing your process. If you cannot kill your process, it is almost certainly due to a bug in the USB serial driver. Unfortunately, your options are most likely the following:

  • Do not request device status.
  • ask the supplier to correct the error
  • solve the problem by finding out a way to poll the status of the device without access to the serial port
  • work around the problem by accessing the device using some other method (possibly using direct USB control).
+2
source share

Impossible?

I have an idea: put a flag inside the data receiving event that closes serialcom from the inside! Plain!

trying to do it myself

+2
source share

How about using a background stream to poll a device so that it doesn't stop your application from exiting?

+1
source share

Here is a link with many other links to this problem and (potential) solutions:

http://www.vbforums.com/showthread.php?t=558000

+1
source share

All Articles