python usb通讯问题

想通过python的pyusb 给USB设备发送数据
import usb.util

dev = usb.core.find(idVendor=0x2704, idProduct=0x2017)
print(dev)

看运行结果应该已经连接成功

img

我想发送一条16进制的数据,不知改如何写 例如发送:
01 5b 00 00 00 00 00 00
01 00 00 00 00 00 00 00

import usb.util
 
dev = usb.core.find(idVendor=0x2704, idProduct=0x2017)
print(dev)
if dev is None:
    raise ValueError('Device not found')

dev.set_configuration()
def tx_data():
        try:
            #文本转换Hex
            hex_str = bytes.fromhex('01 5b 00 00 00 00 00 00')
            #参数1 endpoint,usb设备连接成功打印信息截图中的endpoint值
            #参数2 待写入的数据
            #参数3 超时时间,默认为None
            data = dev.write(0x81,hex_str,1000)
            print(data)
        except Exception as e:
            print(e)

img

试试这样:

import usb.util

dev = usb.core.find(idVendor=0x2704, idProduct=0x2017)
print(dev)
for cfg in dev:
    for intf in cfg:
        sys.stdout.write('\t' + str(intf.bInterfaceNumber) + ',' + str(intf.bAlternateSetting) + '\n')
        for ep in intf:
            sys.stdout.write('\t\t' + str(ep.bEndpointAddress) + '\n')
            if ep.bEndpointAddress:
                try:
                    dev.write(ep.bEndpointAddress, 'test', intf.bInterfaceNumber)
                except Exception:
                    print "\t\terror : dev.write("+str(ep.bEndpointAddress)+", 'test', "+str(intf.bInterfaceNumber)+")"



if dev is None:
    raise ValueError('Device not found')

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]

ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e: \
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT)

assert ep is not None

# write the data比如发送这两个十六进制数据
ep.write('\x01\x5b')