不幸的是,我在Windows下通过USB与PyUSB通信时再次遇到一些问题。在我大约2-3年前的问题中,问题是供应商提供了错误的文档和错误的SCPI命令。
现在,我想与Trinamic的步进电机控制器(TMCM-6110(进行通信。供应商提供的IDE一切都很好,但现在我想通过pyusb解决设备问题,从这个pyusb教程开始。
设备找到了,所以我假设:
- libusb已正确安装并找到
- idVendor和idProduct正确
我必须发送的命令是固定的9字节消息。例如:";移动到位置绝对电机0 12800步";。字节序列是:b'x01x04x01x00x00x00x32x01x39'
,然而,Python将其打印为b'x01x04x01x00x00x002x019'
,这让我感到困惑,并花了一段时间才发现这是相同的。
根据print(dev)
的输出,我假设地址是3。然而,我收到错误消息:
脚本:
import usb.core
import usb.util
dev = usb.core.find(idVendor=0x2a3c, idProduct=0x0100)
if dev is None:
raise ValueError('Device is not found')
# device is found :-)
print(dev)
cmd = b'x01x04x01x00x00x002x019'
# cmd = b'x01x04x01x00x00x00x32x01x39'
dev.write(3,cmd)
输出:
DEVICE ID 2a3c:0100 on Bus 001 Address 006 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x2 Communications Device
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x2a3c
idProduct : 0x0100
bcdDevice : 0x2e01 Device 46.01
iManufacturer : 0x1 Error Accessing String
iProduct : 0x2 Error Accessing String
iSerialNumber : 0x3 Error Accessing String
bNumConfigurations : 0x1
CONFIGURATION 1: 100 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x43 (67 bytes)
bNumInterfaces : 0x2
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0xc0 Self Powered
bMaxPower : 0x32 (100 mA)
INTERFACE 0: CDC Communication =========================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x1
bInterfaceClass : 0x2 CDC Communication
bInterfaceSubClass : 0x2
bInterfaceProtocol : 0x1
iInterface : 0x0
ENDPOINT 0x82: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x82 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0xff
INTERFACE 1: CDC Data ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x1
bAlternateSetting : 0x0
bNumEndpoints : 0x2
bInterfaceClass : 0xa CDC Data
bInterfaceSubClass : 0x0
bInterfaceProtocol : 0x0
iInterface : 0x0
ENDPOINT 0x3: Bulk OUT ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x3 OUT
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0x0
ENDPOINT 0x81: Bulk IN ===============================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x2 Bulk
wMaxPacketSize : 0x40 (64 bytes)
bInterval : 0x0
错误1:
Traceback (most recent call last):
File "C:TilmanProgramsPython37libsite-packagesusbcore.py", line 223, in get_interface_and_endpoint
return self._ep_info[endpoint_address]
KeyError: 3
错误2:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Trinamic.py", line 13, in <module>
dev.write(3,cmd)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 940, in write
intf, ep = self._ctx.setup_request(self, endpoint)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 215, in setup_request
intf, ep = self.get_interface_and_endpoint(device, endpoint_address)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 225, in get_interface_and_endpoint
for intf in self.get_active_configuration(device):
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 236, in get_active_configuration
self.managed_open()
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 102, in wrapper
return f(self, *args, **kwargs)
File "C:TestProgramsPython37libsite-packagesusbcore.py", line 120, in managed_open
self.handle = self.backend.open_device(self.dev)
File "C:TestProgramsPython37libsite-packagesusbbackendlibusb1.py", line 786, in open_device
return _DeviceHandle(dev)
File "C:TestProgramsPython37libsite-packagesusbbackendlibusb1.py", line 643, in __init__
_check(_lib.libusb_open(self.devid, byref(self.handle)))
File "C:TestProgramsPython37libsite-packagesusbbackendlibusb1.py", line 593, in _check
raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform
问题:
错误1:
KeyError: 3
是什么意思?3是错误的地址吗?错误2:
"NotImplementedError: Operation not supported or unimplemented on this platform"
是什么意思?如何修复?为了找出这里出了什么问题,还需要检查和尝试什么?
为什么这些通信示例,无论哪个接口(RS232、RS458、USB、LAN…(都不能立即完成并工作?
这是一个USB CDC ACM设备,因此与它交谈的最佳方式是确保它在";端口(COM&LPT(";列表,然后使用pySerial打开该COM端口。
如果你编写并签署一个INF文件,告诉Windows为你的设备加载不同的驱动程序(即WinUSB(,你可以使用libusb与它对话,但这是一个很大的问题。像这样对端点编号进行硬编码意味着您的代码只适用于此特定设备。