2017-01-08 2 views
-1

Mein Ziel ist es, nach den Dateien auf USB-Stick zu suchen, aber zuerst muss ich herausfinden, wie USB-Stick zu erkennen, die mit dem Computer verbunden ist. Ich habe diesen Code:kann keine usb mit pyserial finden

main.py

class tst(QtGui.QWidget): 
    def __init__(self): 
     super(tst, self).__init__() 

     ports = scanSerial() 
     print ports 

port.py Datei

def scanSerial(): 
    available = [] 
    for i in range(256): 
     try: 
      s = serial.Serial("/dev/ttyUSB" + str(i)) 
      available.append(s.portstr) 
      s.close() 
     except serial.SerialException as e: 
      print e 

    return available 

Und der Ausgang ist:

[Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0' 
[Errno 2] could not open port /dev/ttyUSB1: [Errno 2] No such file or directory: '/dev/ttyUSB1' 
[Errno 2] could not open port /dev/ttyUSB2: [Errno 2] No such file or directory: '/dev/ttyUSB2' 
[Errno 2] could not open port /dev/ttyUSB3: [Errno 2] No such file or directory: '/dev/ttyUSB3' 
[Errno 2] could not open port /dev/ttyUSB4: [Errno 2] No such file or directory: '/dev/ttyUSB4' 
[Errno 2] could not open port /dev/ttyUSB5: [Errno 2] No such file or directory: '/dev/ttyUSB5' 
[Errno 2] could not open port /dev/ttyUSB6: [Errno 2] No such file or directory: '/dev/ttyUSB6' 
[Errno 2] could not open port /dev/ttyUSB7: [Errno 2] No such file or directory: '/dev/ttyUSB7' 
[Errno 2] could not open port /dev/ttyUSB8: [Errno 2] No such file or directory: '/dev/ttyUSB8' 
[Errno 2] could not open port /dev/ttyUSB9: [Errno 2] No such file or directory: '/dev/ttyUSB9' 

Wenn ich ttyS verwenden * statt ttyUSB Da ist etwas:

Could not configure port: (5, 'Input/output error') 
Could not configure port: (5, 'Input/output error') 
Could not configure port: (5, 'Input/output error') 
Could not configure port: (5, 'Input/output error') 

Also Frage ist, wie man zum angegriffenen USB gelangt?

Grüße, Marius

EDIT: wird pyUSB versuchen. Danke euch allen!

+2

* USB-Stick mit Dateien * (aka Speichergerät) hat nichts mit 'pyserial' oder' tty' Geräten zu tun –

+1

Sie benötigen serielle USB-Kommunikation, wenn Sie USB-Serial-Gerät hatten. USB-Stick ist nur usb selbst, ohne irgendetwas "seriell". –

+0

@AlexP. Danke. gut zu wissen. Also ich denke, wird versuchen pyUSB dann :) danke –

Antwort

1

Durch die Re und subprocess Module:

import re 
import subprocess 
device_re = re.compile("Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I) 
df = subprocess.check_output("lsusb") 
devices = [] 
for i in df.split('\n'): 
    if i: 
     info = device_re.match(i) 
     if info: 
      dinfo = info.groupdict() 
      dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device')) 
      devices.append(dinfo) 
print devices 

Wenn devices gedruckt wird, dann sollte es die USB-Geräte derzeit vom Computer verwendet werden, zeigen.

+0

Ich weiß nicht, wie man pyserial verwenden das ist der einzige Weg, ich weiß, wie man den usb –

+0

Dank es zeigt dasselbe wie ich in terminal lsusb –

0

Sie pyUSB finden http://www.stackoverflow.com/questions/2487033/usb-device-identification für die Auflistung angeschlossenen Geräte und https://github.com/walac/pyusb/blob/master/docs/tutorial für pyUSB Tutorial

Code aufzuzählen Geräte mit pyUSB

verwenden können
import bus 
busses = usb.busses() 
for bus in busses: 
    devices = bus.devices 
    for dev in devices: 
    print "Device:", dev.filename 
    print "idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor) 
    print "idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct) 

(kopiert von http://www.stackoverflow.com/questions/2487033/usb-device-identification)

+0

ja ich denke ich werde pyUSB versuchen. Panzer –