Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: USB port disconnect test Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
robert.rozee
Guru
Guru


Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 1285
Posted: 09 August 2018 at 4:17pm | IP Logged Quote robert.rozee

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 on 09 August 2018 at 9:33pm



Back to Top View robert.rozee's Profile Search for other posts by robert.rozee
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5960
Posted: 10 August 2018 at 5:29pm | IP Logged Quote Grogster

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!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
robert.rozee
Guru
Guru


Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 1285
Posted: 10 August 2018 at 10:55pm | IP Logged Quote robert.rozee

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 :-)
Back to Top View robert.rozee's Profile Search for other posts by robert.rozee
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 5960
Posted: 11 August 2018 at 9:16am | IP Logged Quote Grogster

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 on 11 August 2018 at 9:16am


__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
led-bloon
Newbie
Newbie


Joined: 21 December 2014
Location: Australia
Online Status: Offline
Posts: 36
Posted: 11 August 2018 at 9:26am | IP Logged Quote led-bloon

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 result

Edited by led-bloon on 11 August 2018 at 9:43am
Back to Top View led-bloon's Profile Search for other posts by led-bloon
 


If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Search the forums using Google.
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1401 seconds.
Privacy Policy     Process times : 0.02, 0, 0, 0.12