目前需求想通过 usb hid 传输hex文件,第一步先转化成bin文件,根据hid协议通过64 组数据一位进行传输,以下是源码
import usb.core
import usb.util
from intelhex import IntelHex
def hex_to_bin(hex_path, bin_path):
# load hex file
ih = IntelHex(hex_path)
# hex to bin file
ih.tobinfile(bin_path)
if __name__ == '__main__':
# usb vid and pid
vid = 0xC251
pid = 0x1A01
# file name
hex_name = 'HID_Demo_20211227.hex'
bin_name = 'HID_Demo_20211227.bin'
# find our device
dev = usb.core.find(idVendor = vid, idProduct = pid)
# was it found?
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_in = usb.util.find_descriptor(
intf,
# match the first IN endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
assert ep_in is not None
ep_out = 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_out is not None
# hex to bin file
hex_to_bin(hex_name, bin_name)
# open bin file
file = open(bin_name, "rb")
#demo = open('demo.bin', 'ab+')
while True:
data = file.read(64)
if data == b"":
break
#demo.write(data)
ep_out.write(data)
#print(data)
file.close()
#demo.close()
但是通过bus hound 抓取输出发现
多发送了一条前面+了一条加了00的 65 组数据,请问是什么原因,求解答,谢谢。