You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STM32设备DFU模式下调用get_status方法获取状态时报错的问题求助

STM32设备DFU模式下调用get_status方法获取状态时报错的问题求助

我写了一段简单的代码,用来获取处于DFU模式的连接STM32设备的状态。我能正常获取到设备的基础信息,但调用get_status方法检查DFU状态时总是报错。

以下是我的代码:

import usb
import time
import usb.backend.libusb1


# DFU Commands
DFU_DETACH = 0x00
DFU_DNLOAD = 0x01
DFU_UPLOAD = 0x02
DFU_GETSTATUS = 0x03
DFU_CLRSTATUS = 0x04
DFU_GETSTATE = 0x05
DFU_ABORT = 0x06

class STDFU:
    def __init__(self):
        # Initialize the device (replace with your device's VendorID and ProductID)
        self.device = usb.core.find(idVendor=0x0483, idProduct=0xDF11, backend=usb.backend.libusb1.get_backend(find_library=lambda x: "libusb-1.0.dll"))

    if self.device is None:
        raise ValueError("Device not found.")

    # Set up the device (claim interface, reset device, etc.)
    self.device.set_configuration()
    self.device.reset()
    time.sleep(2)  # Ensure the device has time to reset

def get_device_info(self):
    
    if self.device:
        print(f"Device: {self.device}")
        print(f"Device Configuration: {self.device.get_active_configuration()}")
        # Removed the line that tried to access the 'state' attribute
        print("Device state info is not available via pyusb.")
    else:
        print("No device found.")

def get_status(self):
    
    try:
        if self.device is None:
            raise ValueError("Device not initialized.")

        print("Sending DFU_GETSTATUS request to device...")
        response = self.device.ctrl_transfer(0xA1, DFU_GETSTATUS, 0, 0, 6, timeout=20000)
        print(f"Received response: {response}")

        # Assuming response follows this format:
        dfu_status = DFUStatus()
        dfu_status.status = response[0]
        dfu_status.poll_timeout = response[1] | (response[2] << 8) | (response[3] << 16)
        dfu_status.state = response[4]
        dfu_status.iString = response[5]

        print(f"DFU Status: {dfu_status}")
        return dfu_status

    except usb.core.USBError as e:
        print(f"Failed to get status: {e}")
        raise



stdfu = STDFU()
stdfu.get_device_info()  
dfu_status = stdfu.get_status()

我每次运行都会触发这个错误:

usb.core.USBError: [Errno None] b'libusb0-dll:err [control_msg] sending control message failed, win error: A device attached to the system is not functioning.\r\n\n'

不过设备的基础信息是可以正常获取的,以下是设备信息片段:

Device: DEVICE ID 0483:df11 on Bus 000 Address 255 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x100 USB 1.0
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x0483
 idProduct              : 0xdf11
 bcdDevice              : 0x2200 Device 34.0
 iManufacturer          :    0x1 STMicroelectronics
 iProduct               :    0x2 STM32  BOOTLOADER
 iSerialNumber          :    0x3 366337783238
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x36 (54 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xc0 Self Powered
   bMaxPower            :   0x32 (100 mA)

真心希望能得到大家的帮助,提前谢谢各位了!

备注:内容来源于stack exchange,提问作者Nene Tetteh Wayo Akwetteh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:54:29