我使用python脚本调用cyusb.dll,发送数据正常,但是接收数据全是0;用脚本发送数据,用软件接收可以收到;软件发送数据,脚本接收到的全是0,应该是接收到数据了,不知道是哪里出错了,哪位大佬能给个建议。
python脚本:
import clr
import os
import sys
import
time
import ctypes as c
import binascii
# 添加CyUSB.dll引用
dll_path = os.path.abspath("C:/Users/PDA/Desktop/cypress/CYUSB301/CyUSB.dll")
sys.path.append(dll_path)
clr.AddReference("C:/Users/PDA/Desktop/cypress/CYUSB301/CyUSB")
# 导入CyUSB命名空间
from CyUSB import USBDeviceList, CyConst, CyUSBDevice, CyBulkEndPoint, CyControlEndPoint, CyIsocEndPoint
def find_device(vid, pid
usb_devices = USBDeviceList(CyConst.DEVICES_CYUSB)
device = None
for dev in usb_devices:
if dev.VendorID == vid and dev.ProductID == pid:
device = dev
break
return device, usb_devices
def send_data(device, endpoint_addr, data
endpoint = device.EndPointOf(endpoint_addr)
if endpoint is None:
print(f"无法找到端点地址: {endpoint_addr}")
return
buffer = bytearray(data)
bytes_written = endpoint.XferData(buffer, len(buffer))
print(f"数据发送成功, 发送字节数: {bytes_written}")
return bytes_written
def receive_data(device, endpoint_addr, size
endpoint = device.EndPointOf(endpoint_addr)
if endpoint is None:
print(f"无法找到端点地址: {endpoint_addr}")
return
buffer = bytearray(size)
endpoint.TimeOut = 20
bytes_read = endpoint.XferData(buffer, len(buffer))
print(f"数据接收成功, 接收字节数: {bytes_read}")
print(buffer)
print(binascii.b2a_hex(buffer))
return None#buffer[:bytes_read]
def main():
# 替换为你的设备的VID和PID
VID = 0x04B4 # Vendor ID
PID = 0x00F1 # Product ID
# 查找并配置设备
usb_devices = USBDeviceList(CyConst.DEVICES_CYUSB)
device = usb_devices[VID, PID]
if device is None:
print("设备未找到")
return
print("设备已连接")
# 设置端点地址(替换为你的设备的端点地址)
IN_ENDPOINT = 0x81 # 输入端点地址
OUT_ENDPOINT = 0x01 # 输出端点地址
# 发送数据到设备
out_data = [0xaa, 0xbb, 0xcc, 0xdd, 0x0e, 0x06, 0x07, 0x08, 0x09,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x01, 0xa2, 0x03, 0x04, 0x05, 0xf6, 0xf7, 0x08, 0x09, 0x01, 0x02, 0x03]
send_data(device, OUT_ENDPOINT, out_data)
time.sleep(1)
receive_data(device, IN_ENDPOINT, 1024) # 假设接收64字节数据
print(device.bHighSpeed)
# 释放资源
usb_devices.Dispose()
if __name__ == "__main__":
main()