USB 设备通过硬件端点将数据发送到 HID 设备、Xcode Swift、MacOS

发布于 2025-01-11 21:38:52 字数 1434 浏览 0 评论 0原文

func upload(value:UInt16, length:UInt16) throws -> [UInt8] {
        guard let deviceInterface = DFUDevice.sharedInstance.deviceInterfacePtrPtr?.pointee?.pointee else {
            throw DFUDeviceError.DeviceInterfaceNotFound
        }
        var kr:Int32 = 0
        var requestPtr:[UInt8] = [UInt8](repeating: 0, count: Int(length))

        var request = IOUSBDevRequest(bmRequestType: 161,
                                      bRequest: DFUREQUEST.UPLOAD.rawValue,
                                      wValue: value,
                                      wIndex: 0,
                                      wLength: length,
                                      pData: &requestPtr,
                                      wLenDone: 255)

        kr = deviceInterface.DeviceRequest(DFUDevice.sharedInstance.deviceInterfacePtrPtr, &request)

        if (kr != kIOReturnSuccess) {
            throw DFUDeviceError.RequestError(desc: "Upload request error: \(kr), request data: \(request)")
        }

        return requestPtr
    }

在硬件模式下向设备发送数据时出错:

Upload request error: -536854449, request data: IOUSBDevRequest(bmRequestType: 161, bRequest: 2, wValue: 184, wIndex: 0, wLength: 9, pData: Optional(0x000060000228a690), wLenDone: 0)

从此链接找到上述代码:USB 设备发送/接收数据正在检测设备,但无法发送数据

func upload(value:UInt16, length:UInt16) throws -> [UInt8] {
        guard let deviceInterface = DFUDevice.sharedInstance.deviceInterfacePtrPtr?.pointee?.pointee else {
            throw DFUDeviceError.DeviceInterfaceNotFound
        }
        var kr:Int32 = 0
        var requestPtr:[UInt8] = [UInt8](repeating: 0, count: Int(length))

        var request = IOUSBDevRequest(bmRequestType: 161,
                                      bRequest: DFUREQUEST.UPLOAD.rawValue,
                                      wValue: value,
                                      wIndex: 0,
                                      wLength: length,
                                      pData: &requestPtr,
                                      wLenDone: 255)

        kr = deviceInterface.DeviceRequest(DFUDevice.sharedInstance.deviceInterfacePtrPtr, &request)

        if (kr != kIOReturnSuccess) {
            throw DFUDeviceError.RequestError(desc: "Upload request error: \(kr), request data: \(request)")
        }

        return requestPtr
    }

Getting error in sending data to device in hardware mode:

Upload request error: -536854449, request data: IOUSBDevRequest(bmRequestType: 161, bRequest: 2, wValue: 184, wIndex: 0, wLength: 9, pData: Optional(0x000060000228a690), wLenDone: 0)

found above code from this link: USB device send/receive data it is detecting device, but not able to send data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文