0

嗨,是的,我正在编写一个 python3 程序,它应该将文件的内容发送到 LPC1758。为了测试,我将在 USB 上方写一个简单的字符串。我使用 libusb1.0 ....

但是每次我成为同样的错误时,即使我将“dev”重命名为“device”,错误也是一样的。--> AttributeError: 'MainWindow' 对象没有属性 'dev'<--

class MainWindow(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
    QtGui.QMainWindow.__init__(self, parent)
    self.setupUi(self)
    self.pb_send.clicked.connect(self.pb_send_clicked)
    self.pb_open.clicked.connect(self.pb_open_clicked)
    self.pb_exit.clicked.connect(self.pb_exit_clicked)


  # SEND button event handler
def pb_send_clicked(self):

    send = "Yo Man"

    bulk_out_ep = 0x05
    bulk_in_ep = 0x82
    interface = 0
    length = 30

    dev = usb.core.find(idVendor= 0xfefe, idProduct= 0x0001)
    cfg = usb.control.get_configuration(dev)            # get current configuration

    print(dev)


    if dev is None:
        self.USB_label.setText("Device not found !!!")
        raise ValueError('Device not found')
    else:
        self.USB_label.setText("Device found")
        found = 1

    if(cfg != 1):                                       # check if device is configured
        usb.DeviceHandle.setConfiguration(self, 1)

    usb.util.claim_interface(dev, interface)

    usb.DeviceHandle.bulkWrite(self,bulk_out_ep,send)
    print("wrote to estick")
    readData = usb.DeviceHandle.bulkRead(self, bulk_in_ep, length)
    print("read from estick: "+ readData)

    usb.util.release_interface(dev, interface)

这是 Stacktrace 显示的内容:

Traceback (most recent call last):
  File "/home/user/workspace_Qt/ESS_project/ess_project.py", line 93, in pb_send_clicked
    readData = usb.DeviceHandle.bulkRead(self,endpoint=bulk_in_ep,size=length)
  File "/usr/lib/python3.3/usb/legacy.py", line 159, in bulkRead
    return self.dev.read(endpoint, size, self.__claimed_interface, timeout)
AttributeError: 'MainWindow' object has no attribute 'dev'
4

1 回答 1

0

dev是类的成员,DeviceHandle它是函数所期望的第一个参数的类型setConfiguration

并且setConfiguration需要 2 个参数而不是 1 个参数,因为您是在类上调用方法usb.DeviceHandle而不是在类型的对象上调用它usb.DeviceHandle,所以您应该使用:

dev.setConfiguration(1)

bulkWrite和一样bulkRead)。

此外,该get_configuration方法返回一个Configuration对象而不是一个数字,因此cfg != 1将始终为真(这可能适用,cfg.index != 1但我不确定)。

于 2014-06-24T15:29:59.323 回答