Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Is device Connected Windows?

Reply
Thread Tools

RE: Is device Connected Windows?

 
 
Tim Golden
Guest
Posts: n/a
 
      06-01-2006
[placid]

| Just wondering is there a way (not brute force) to check if a usb
| storage device is connected?

Hmmm. How do you identify "a usb storage device" to know that
it is or isn't connected?

You can certainly do something useful with wmi. eg,

<code>
import wmi

c = wmi.WMI ()
for usb_disk in c.Win32_DiskDrive (InterfaceType="USB"):
print usb_disk.Caption
print usb_disk.PNPDeviceID

</code>

Now, assuming that the PNPDeviceID is unique enough for
your purpose, you could probably do something with it
to keep hold of it and then check later whether the
same device is still inserted. One possibility is
to use a WMI watcher to spot when devices are removed:

<code>
import wmi

c = wmi.WMI ()
usb_watcher = c.watch_for (
notification_type="Deletion",
wmi_class="Win32_DiskDrive",
delay_secs=2,
InterfaceType="USB"
)

while True:
usb_removed = usb_watcher () # can optionally timeout
print usb_removed.PNPDeviceID

</code>

Lots of other possibilities, a polling loop, two
watchers, one for creation one for deletion etc.
Depends exactly what your requirements are.

TJG

__________________________________________________ ______________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
__________________________________________________ ______________________
 
Reply With Quote
 
 
 
 
placid
Guest
Posts: n/a
 
      06-05-2006

Tim Golden wrote:
> [placid]
>
> | Just wondering is there a way (not brute force) to check if a usb
> | storage device is connected?
>
> Hmmm. How do you identify "a usb storage device" to know that
> it is or isn't connected?
>
> You can certainly do something useful with wmi. eg,
>
> <code>
> import wmi
>
> c = wmi.WMI ()
> for usb_disk in c.Win32_DiskDrive (InterfaceType="USB"):
> print usb_disk.Caption
> print usb_disk.PNPDeviceID
>
> </code>
>
> Now, assuming that the PNPDeviceID is unique enough for
> your purpose, you could probably do something with it
> to keep hold of it and then check later whether the
> same device is still inserted. One possibility is
> to use a WMI watcher to spot when devices are removed:
>
> <code>
> import wmi
>
> c = wmi.WMI ()
> usb_watcher = c.watch_for (
> notification_type="Deletion",
> wmi_class="Win32_DiskDrive",
> delay_secs=2,
> InterfaceType="USB"
> )
>
> while True:
> usb_removed = usb_watcher () # can optionally timeout
> print usb_removed.PNPDeviceID
>
> </code>
>


Thanks mate, this is what i was after, i can work my way through here.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection between a lan-connected pc and a wireless connected =?Utf-8?B?Q2lhY2NpaG9tZQ==?= Wireless Networking 1 08-17-2006 11:00 AM
Wireless connected but don't have LAN connected Thaqalainnaqvi@gmail.com Computer Support 6 06-11-2006 09:06 PM
connected but not connected =?Utf-8?B?R2xlbm4=?= Wireless Networking 1 12-04-2005 12:35 AM
problem: connected, but not connected to a wireless network Andrew Smith Wireless Networking 0 08-20-2004 07:51 PM
Connected Icon Showing, But In Reality Not Connected. Thoughts On ? Robert11 Computer Support 2 03-02-2004 04:02 PM



Advertisments