pythonwindowsserial-portpyserialcom0com

com0com and pyserial virtual serial ports. Can this be used to simulate unplugging a serial usb device?


I am using com0com and pyserial. I open one port, write to it using pyserial and read from it in the YAT emulator. This works great. Can this setup be used to simulate unplugging of a usb device that is emulating a serial port? I want to recreate a UnauthorizedAccessException that is rarely thrown by real devices in our application software upon unplug. After writing to CNCA0 using pyserial and reading from CNCB0 using YAT successfuly, I tried to close CNCB0 from pyserial and of course it wouldnt let me because port is already acquired by YAT (Access is denied). Any ideas about how to simulate an unplug action of a real device?


Solution

  • Unauthorized access is simple to replicate. Open up the port with another application, perhaps in another YAT tab. When you try to connect with a different application, you should get an unauthorized access error. However, I'm not sure if that is really the question you are asking.

    If you really want to emulate a port disconnect you should realize that you are also at the mercy of the serial driver. Different drivers will behave differently on sudden device removal. What I'm saying is that you can trick yourself into thinking you have a bullet-proof exception handling process when in reality, all bets are off.

    I did some experimenting with some of the com0com change commands and nothing I did could cause the port to "virtually disconnect". See the setupc.exe command >help for more info on what is available.

    You might also be interested in playing with the emulated noise feature. Open the command shell for com0com and execute

    change <YOUR_PORT_CNC> EmuNoise=0.0001
    

    The value is a percentage chance of corruption of the data stream. Fun stuff.