【问题标题】:Windows application using libusb: runtime error due to mutex lock使用 libusb 的 Windows 应用程序:由于互斥锁导致的运行时错误
【发布时间】:2015-06-10 10:51:03
【问题描述】:

我正在尝试通过 USB 在笔记本电脑和嵌入式板之间建立通信。主板的 USB 驱动程序已安装,并且已在 Windows 设备管理器中检测到。现在我正在使用 libusb 在 Visual Studio 2013(Win32 控制台应用程序)中开发代码。

libusb_init() 函数不会返回错误,但是当我尝试使用libusb_open_device_with_vid_pid() 打开设备时,libusb 库的threads_windows.c 文件中的函数usbi_mutex_init() 会中断执行。此文件包含“Microsoft Windows 上的 libusb 同步”的源代码。

我也尝试调用函数libusb_get_device_list(),但得到同样的错误。你能建议一个解决方案吗?

我的应用程序源代码中的main.cpp -->

#include <iostream>
#include "libusb.h"

using namespace std;

int main()
{
 int init_status = 0;
 libusb_context *context = NULL;
 libusb_device_handle *device;

 init_status = libusb_init(&context);
 if (init_status<0)
 {
    cout << "could not initialize";
    return -1;
 }

 device = libusb_open_device_with_vid_pid(NULL, 0x0483, 0x5750); //execution breaks here
 if (device == NULL)
 {
    cout << "could not open device";
    return -1;
 }
 else
 {
    cout << "Device opened successfukky";
 }

 return 0;
}

来自 libusb 源代码的threads_windows.c -->

int usbi_mutex_lock(usbi_mutex_t *mutex) {
    DWORD result;
    if(!mutex) return ((errno=EINVAL));
    result = WaitForSingleObject(*mutex, INFINITE);  //execution breaks here
    if(result == WAIT_OBJECT_0 || result == WAIT_ABANDONED)
        return 0; // acquired (ToDo: check that abandoned is ok)
    return ((errno=EINVAL)); // don't know how this would happen
                         //   so don't know proper errno
}

【问题讨论】:

  • 在我看来,您使用 libusb_init() 获得了特定的非默认上下文,但是当您使用 libusb_open_device_with_vid_pid() 时,您通过了 NULL,这意味着使用默认上下文而不是您的上下文指针。
  • 如果它是单个设备并且您只需要一个会话,您可能只想使用默认上下文。该文档表明libusb_open_device_with_vid_pid() 存在“限制”,并且它不适用于测试应用程序以外的任何内容。我认为这是因为在实际应用中可能有多个设备,而这个便利函数只接受第一个匹配参数的设备。但是,您看到的问题可能与上下文有关。
  • 谢谢,这有助于解决问题。问题在于上下文。

标签: visual-c++ runtime-error libusb


【解决方案1】:

在我看来,您使用 libusb_init() 获得了特定的非默认上下文,但是当您使用 libusb_open_device_with_vid_pid() 时,您传递的是 NULL,这意味着使用默认上下文而不是 @987654324 创建的上下文@。

如果它是单个设备并且您只需要一个会话,您可能只想使用默认上下文。该文档表明libusb_open_device_with_vid_pid() 存在“限制”,它不适用于测试应用程序以外的任何内容。我认为这是因为在实际应用程序中可能有多个设备,而这个便利函数只接受第一个匹配参数的设备。但是,您看到的问题可能与上下文有关。

对于一次性类型的特定项目,您的代码将类似于:

#include <iostream>
#include "libusb.h"

using namespace std;

int main()
{
    int init_status = 0;
    libusb_device_handle *device;

    // specify NULL for the context address so that libusb_init will use default content.
    // this means that any libusb function with a context argument will be called with NULL.
    init_status = libusb_init (NULL);
    if (init_status < 0)
    {
        cout << "could not initialize";
        return -1;
    }
    // open the first device found in the device list with this vendor id and product id
    // for a real application in a multi-device environment we would need to
    // iterate through the various devices using libusb_get_device_list() to get
    // the list of devices then using libusb_get_device_descriptor() to iterate
    // through the list to find the device we want. also need libusb_free_device_list   ()
    // after finishing with the list.  lots of work for a simple one off project
    device = libusb_open_device_with_vid_pid (NULL, 0x0483, 0x5750);
    if (device == NULL)
    {
        cout << "could not open device";
        return -1;
    } else {
        cout << "Device opened successfully";
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2011-03-03
    • 2021-07-19
    相关资源
    最近更新 更多