HY, 是的,我正在写一个python3程序,它应该将文件的内容发送到LPC1758。 为了测试,我将在USB上面写一个简单的字符串。我使用libusb1.0 ....
但每次我都成了同样的错误,即使我重命名" 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)
这就是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'
答案 0 :(得分:0)
dev
是类DeviceHandle
的成员,它是setConfiguration
函数所期望的第一个参数的类型。
并且setConfiguration
需要2个参数而不是1,因为您在类usb.DeviceHandle
上调用方法而不是在usb.DeviceHandle
类型的对象上调用它,您应该使用:< / p>
dev.setConfiguration(1)
(bulkWrite
和bulkRead
同样如此)。
同样,get_configuration
方法会返回Configuration
个对象而不是数字,因此cfg != 1
将始终为真(这可能与cfg.index != 1
相反,但我可以使用{{1}}。我不确定。)