[python-win32] USB CDC-class support with win32file, exception processing issues

Ron Jackson Ron at FascinatingElectronics.com
Tue Jun 3 00:30:10 CEST 2008


When USB communication device class devices are installed in Windows
they appear as COMxxx ports, just like legacy serial ports. They are
different in that many RS-232 device parameters don't apply (such as
baud rate, handshaking, etc). Also, since they are USB devices they
may be suspended while remaining connected (as when the computer goes
into sleep mode), or may be unplugged at any time.

I am creating a simple Python communications library to support
USB-CDC devices, ignoring the unused legacy features but adding
suspend and disconnect support. I have found some problems with
exception processing that is making this problematic.

win32file.WriteFile works well. If the device is suspended I get
pywintypes.error (31, 'WriteFile', 'A device attached to the system is
not functioning.'). If the device is unplugged I get pywintypes.error
(1167, 'WriteFile', 'The device is not connected.').

I can find out if there are any characters in the input buffer using
win32file.ClearCommError. Its exception reporting half-way works. If
the device is suspended I get pywintypes.error (31, 'ClearCommError',
'A device attached to the system is not functioning.'). But if the
device is unplugged I get no error reported at all.

win32file.ReadFile provides neither of these errors. Either suspended
or unplugged just causes ReadFile to hang. I can get ReadFile to
return if I set a timeout, but no error is reported so I don't know if
it returned because the device hasn't sent the data yet, or because
the device is suspended or disconnected.

Is there some way to get ReadFile to report errors 31 and 1167 like
WriteFile does? Or is there some other way to determine port status as
"attached but not functioning" or "not connected"?

Thanks for the help!

  -- Ron


More information about the python-win32 mailing list