python中的USB模块出错

时间:2012-01-24 05:22:31

标签: python

这是我的代码。 该代码应该用于监控打印机状态

#!/usr/bin/python
import usb

class HardwareMonitor_printer(object):
    def __init__(self, vendor_id, product_id):
        self.device_found = False # True if device found
        busses = usb.busses() # enumerate busses    
        self.handle = None
        print 'busses: ',busses
        for bus in busses:
            print 'bus: ',bus
            devices = bus.devices
            print 'devices: ',devices
            for dev in devices:
                if dev.idVendor == vendor_id and dev.idProduct == product_id: # device matches    
                    self.dev = dev
                    self.conf = self.dev.configurations[0]
                    self.intf = self.conf.interfaces[0][0]
                    self.endpoints = []
                    for endpoint in self.intf.endpoints:
                        self.endpoints.append(endpoint)
                    self.device_found = True
                    return

    def open(self):
        if self.handle:
            self.handle = None
        try:
            self.handle = self.dev.open()
            self.handle.detachKernelDriver(0)
            self.handle.detachKernelDriver(1)
            self.handle.setConfiguration(self.conf)
            self.handle.claimInterface(self.intf)
            self.handle.setConfiguration(1)
            self.handle.claimInterface(1)
            self.handle.setAltInterface(self.intf)
            return True
        except:
            return False

    def write(self, ep , buff, timeout=100):
        try:
            return self.handle.interruptWrite(ep, buff, timeout) #return bytes written    
        except Exception as e:
            print e
            return 0

    def read(self, ep, size, timeout=100):
        try:
            return self.handle.interruptRead(ep, size, timeout) # return data read    
        except:
            return []

    def getDeviceName(self):
        try:
            for i in range (1, 4):
                print self.handle.getString(i,25)
        except IOError as e:
            print e

    def endpoint(self):
        return self.endpoints

if __name__ == '__main__':    
    printer= HardwareMonitor_printer(0x0dd4,0x01a8)
    if printer.device_found:
        printer.open()
        printer.getDeviceName()
        #I somehow need to write into usb device
        print printer.write(1 , [0x10], 1000)
        time.sleep(1)
        byteread = printer.read(0x10, 64)
        print 'rx data: ',byteread

这是我的输出。

$ sudo python hardware_monitor.py
CUSTOM Engineering S.p.A.
TG2480-H
Self Power Mode
error submitting URB: No such file or directory
0
rx data:  []

非常感谢你guyz回复... :) 该程序应该监视打印机的状态。 如您所见,我可以追踪设备 - > TG2480-H 我现在需要的是将一些十六进制值写入设备 这样我就可以解析回复...... 但它显示出错误 “错误提交URB:没有这样的文件或目录”

任何想法..... !!! 我是usb模块的新手

0 个答案:

没有答案