2013-05-18 224 views
2

无论何时插入闪存驱动器,我都能够检测到。但是,(听起来很傻)无法检测是否插入了多个USB。 另外,我很难编写代码从它读取数据(他们)。 有人可以帮忙吗? 谢谢,提前 :) P.S.我的系统是Windows 8 64位 整个事情是关于检测我将从传感器中选择的信号。如何检测多个USB端口并使用python从USB读取数据

这里是代码检测USB

import string 
from ctypes import windll 
import time 
import os 
def get_drives(): 
    drives = [] 
    bitmask = windll.kernel32.GetLogicalDrives() 
    for letter in string.uppercase: 
     if bitmask & 1: 
      drives.append(letter) 
     bitmask >>= 1 
    return drives 
if __name__ == '__main__': 
    before = set(get_drives()) 
    pause = raw_input("Please insert the USB device, then press ENTER") 
    print ('Please wait...') 
    time.sleep(5) 
    after = set(get_drives()) 
    drives = after - before 
    delta = len(drives) 
    if (delta): 
     for drive in drives: 
      if os.system("cd " + drive + ":") == 0: 
       newly_mounted = drive 
       print "There were %d drives added: %s. Newly mounted drive letter is %s" % (delta, drives, newly_mounted) 
    else: 
     print "Sorry, I couldn't find any newly mounted drives." 
+0

显示您到目前为止的内容,以便人们可以了解您需要 – jamylak

回答

0

我想对我的computer.it你的代码工作perfectly.My系统是WIN7 64位的职业选手。

+0

的帮助,它可以很好地工作......适用于一台设备。 – Nix

+0

它不适用于多个USB – Nix

+0

其实,我尝试了两个设备。它确实工作完美 – ray