2

I am writing an application that uses virtual (shared) serial ports via a USB to RS232 adapter. If the user accidentally disconnects the USB adapter, my data stream stops (occasionally throwing an IO exception but not always). Normally, the user could just reconnect the port and the listener would continue. But all of the virtual port applications I have found need to be reset, then then my application must be re-initialized. I could put a read timeout in my listener but I'm curious if there isn't a clean way to detect the ports use the property changed event handler. Since it's a virtual port, I don't believe using hardware level commands will work. I have achieved the detection in a console app just using a while loop where I continually poll the ports using GetPortNames. But that just isn't practical in a forms app using a DataReceived Event Handler, especially considering all the parsing I do on the data stream. I have searched multiple sites (including this one) and no one has adequately addressed the issue. Please, any help appreciated.

VillageTech
  • 1,968
  • 8
  • 18
  • This depends on OS-level driver used for your adapter. It seems there is no universal, generic way to detect it. – VillageTech Dec 08 '19 at 19:31
  • 1
    Yeah... not a hardware level issue. I will figure it out. But thanks anyway. – Mountain Drew Dec 09 '19 at 00:35
  • This article will be helpful. [A USB Library to Detect USB Devices](https://www.codeproject.com/Articles/60579/A-USB-Library-to-Detect-USB-Devices) – kunif Dec 09 '19 at 05:54
  • Thank you. Seems like a useful tool but I don't want the user running another third party app (mine and the port emulator). I have come to the conclusion that the problem is a little more complex than I originally thought. I implemented a PropertyChangedEventHandler from INotifyPropertyChanged and it works except the only time it fires is when the user changes the port. I could start another thread the periodically checks if the port is in the GetPortNames collection but I don't really want the additional overhead. I think catching a ReadTimeout exception is the best way to handle this. – Mountain Drew Dec 09 '19 at 22:15
  • The program is a sample for confirmation, and the essence is information on how to use it in API and C#. If this is incorporated into your program, it will be recognized by Plug and Play. – kunif Dec 10 '19 at 04:04
  • You might consider using WMI. See https://stackoverflow.com/questions/45165299/wmi-get-list-of-all-serial-com-ports-including-virtual-ports and https://stackoverflow.com/a/1082075/2009197 – Baddack Dec 11 '19 at 00:31
  • As a follow up. I was able to solve this issue by creating a BackgroundWorker thread, then using a lambda in a while loop to periodically check if the port was in the GetPortNames array (and setting a Thread.Sleep for a reasonable period). Since the thread runs asynchronously, it does not interfere with the serial port read and does not seem to add to my overhead. It worked perfectly. The only problem was that it did not work with the virtual port emulator I had chosen. I will not mention any names... – Mountain Drew Dec 18 '19 at 01:42

0 Answers0