Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:57 11 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : USB port disconnect test

Author Message
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2463
Posted: 06:17am 09 Aug 2018
Copy link to clipboard 
Print this post

hi,
i've been doing some experiments with detecting an unpligged USB cable under windows, for which i've never found a good solution online. when unplugged from the USB, many USB to serial bridges remain visible until you actually try writing something to them. the common solution is to try resending the serial port parameters, but this has issues with blocking your application for a second or two, as well as upsetting some devices even if they are plugged in and happy.

but a few days ago i hit upon an odd solution that seems to work: simply try opening the serial port (even though you already have it opened). if the hardware is missing a different error code is returned to the one returned if the port is present but in use. you can see this behaviour with the simple console command:
dir \\.\COM3
(where com3 is the serial port you are working with)

i've produced a test version of GFXterm that uses this approach, and would be interested in hearing how it works out for different folks:
2018-08-09_160552_GFXterm_test_version,_9-aug-2018.zip

an unplugged USB cable is detected almost instantly, and this method should also work with geoff's ASCII Video Terminal.

please do post back to this thread with your results.


cheers,
rob :-)
Edited by robert.rozee 2018-08-10
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9753
Posted: 07:29am 10 Aug 2018
Copy link to clipboard 
Print this post

Hey Rob.

Windows 10 blocked it. I told Bill everything was OK, and run it anyway. AVG then blocked it, saying it was a 'Very rare file' and has been submitted to AVG for evaluation. I have temporarily disabled AVG.

- Connect to COM4.
- Remove USB plug. Serial I/O error: ClearCommError failed. (22) The device does not recognize the command.
- Replace USB plug.
- Terminal echo is working, connection lost.
- Reconnect to COM4, everything is fine again.

Does that help at all?


Smoke makes things work. When the smoke gets out, it stops!
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2463
Posted: 12:55pm 10 Aug 2018
Copy link to clipboard 
Print this post

it does help, very much :-) i wonder how long it will be before we just see blanket responses of 'this is not a microsoft approved application', or '32-bit applications can not be run on this computer'?


the 'ClearCommError failed...' error message is coming from a check to see if serial data is waiting - called every 15ms or thereabouts. this seems to be how CP2102 USB to serial bridges respond to being unplugged (or more correctly, how the silicon labs driver responds). this response is OK, and if every USB to serial bridge driver responded this way we would be away laughing.

i've found that the CDC driver (built into windows, as used by 1455 solutions) does not produce a ClearCommError failure when the USB cable is unplugged, hence the need for intercepting USB device change events and using the 'reopen the port' or 'get/set parameters' trick.

i assume you used a CP2102, and not a 1455? it could also be that win10 has a different CDC driver than the one built into XP (i use XP). i'd also be interested in the results from testing with a CH340G (arduino clone), geoff's ascii terminal if possible, and anything else that happens to be handy.


the most important behaviours are that GFXterm:
1. responds almost immediately to an in-use port being unplugged, without the user needing to press any keys, by going offline (with some sort of popup message),
2. if something unrelated is unplugging (or plugging in) nothing should happen - no going offline!
3. in normal use (no plugging or unplugging going on), everything else continues to work as expected.


once every is working smoothly again, i may well combine all the error messages to a simple "a communications error has occurred, please check that the USB to serial bridge has not been unplugged". after all, there seems to be no good reason for real/fixed serial ports to produce any errors, it is purely a USB-related issue.

has anyone else been able to do any testing?


cheers,
rob :-)
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9753
Posted: 11:16pm 10 Aug 2018
Copy link to clipboard 
Print this post

NO, I WAS USING AN E28 MODULE WITH THE 1455 INTERFACE!

I will try a 2102 soon, and let you know.
As to your three points:

1) YES - within about half a second.
2) YES - nothing else seemed to be affected(my mouse and KB are also USB)
3) YES - Plugging in flash-drives etc did not upset GFXterm's connection.

On another subject, AVG labs have reported your file as clean.





We all knew it would have been, but it is always nice to get the confirmation. Edited by Grogster 2018-08-12
Smoke makes things work. When the smoke gets out, it stops!
 
led-bloon

Senior Member

Joined: 21/12/2014
Location: Australia
Posts: 208
Posted: 11:26pm 10 Aug 2018
Copy link to clipboard 
Print this post

This help?

Windows XP - FTDI chip (clone)
Test:
- Connect to COM6.
- Remove USB plug. Serial I/O error: ClearCommError failed. (5) "Access is denied".
- Replace USB plug.
- Terminal echo is working, connection lost.
- Reconnect to COM6, everything is fine again.

Plus
EXACTLY same result for Win10 (only different port)

led

Edit: Added win 10 resultEdited by led-bloon 2018-08-12
Miss you George
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025