简体   繁体   中英

cant find any usb with pyserial

My goal is to look for the files on usb stick, but first I need to find out how to detect usb stick which is connected to computer. I have this code:

main.py

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

        ports = scanSerial()
        print ports

port.py file

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

And the output is:

[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'

If I use ttyS* instead of ttyUSB there is something:

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')

So question is how to get to the attacked USB?

Regards, Marius

EDIT: will try pyUSB. Thank to you all guys!

Using re and subprocess modules:

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

When devices is printed then it should show the usb devices currently used by the computer.

Try using pyserial list_ports tool http://pyserial.readthedocs.io/en/latest/tools.html#module-serial.tools.list_ports example:

import serial.tools.list_ports
for i in serial.tools.list_ports.comports():
    d = serial.Serial(i[0])
    print '%s - ' % i[0] , d.isOpen()

You can use pyUSB , see http://www.stackoverflow.com/questions/2487033/usb-device-identification for listing attached devices and https://github.com/walac/pyusb/blob/master/docs/tutorial for pyUSB tutorial

code to enumerate devices using pyUSB

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)

(copied from http://www.stackoverflow.com/questions/2487033/usb-device-identification )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM