AttributeError: 'MainWindow'对象没有属性'dev'

时间:2022-02-27 10:46:27

Hy, Yeah so, i am writing an python3 programm, that should send the content of an file to an LPC1758. For testing i will write a simple string above USB. I use libusb1.0 ....

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

But every time i become the same error, even if i rename "dev" into "device" the error is the same. --> AttributeError: 'MainWindow' object has no attribute 'dev'<--

但是每次我变成相同的错误,即使我将“dev”重命名为“设备”,错误也是一样的。-> 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)

This is what Stacktrace shows:

这就是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'

1 个解决方案

#1


0  

dev is a member of the class DeviceHandle which is the type of the first parameter expected by the setConfiguration functions.

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

And setConfiguration expects 2 parameters instead of 1, because you are calling the method on the class usb.DeviceHandle instead of calling it on an object of type usb.DeviceHandle, you should use:

setConfiguration需要两个参数而不是1,因为您在类usb上调用这个方法。DeviceHandle,而不是在usb类型的对象上调用它。DeviceHandle,您应该使用:

dev.setConfiguration(1)

(same thing for bulkWrite and bulkRead).

(隔板和隔板也是一样)。

Also the get_configuration method returns a Configuration object instead of a number, so cfg != 1 will always be true (this may work with cfg.index != 1 instead but I'm not sure).

get_configuration方法还返回一个配置对象而不是一个数字,因此cfg != 1将始终为真(这可能适用于cfg。索引!= 1,但我不确定)。

#1


0  

dev is a member of the class DeviceHandle which is the type of the first parameter expected by the setConfiguration functions.

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

And setConfiguration expects 2 parameters instead of 1, because you are calling the method on the class usb.DeviceHandle instead of calling it on an object of type usb.DeviceHandle, you should use:

setConfiguration需要两个参数而不是1,因为您在类usb上调用这个方法。DeviceHandle,而不是在usb类型的对象上调用它。DeviceHandle,您应该使用:

dev.setConfiguration(1)

(same thing for bulkWrite and bulkRead).

(隔板和隔板也是一样)。

Also the get_configuration method returns a Configuration object instead of a number, so cfg != 1 will always be true (this may work with cfg.index != 1 instead but I'm not sure).

get_configuration方法还返回一个配置对象而不是一个数字,因此cfg != 1将始终为真(这可能适用于cfg。索引!= 1,但我不确定)。