【问题标题】:Visual Studio C# .exe runs, but USB HID device quits workingVisual Studio C# .exe 运行,但 USB HID 设备停止工作
【发布时间】:2015-02-23 00:34:30
【问题描述】:

当我在 Visual Studio 2013 中运行我的 C# 应用程序时,我可以可靠地与我的自定义 USB HID 设备通信。

当我直接运行 .exe(发布或调试)时,应用程序运行,但没有与 USB HID 设备通信。

我知道与 App Store 应用程序通信需要添加清单,但我正在构建一个 Windows 窗体应用程序。我找不到任何有关为 Windows 窗体应用启用 USB 权限的详细信息。

无法访问调试器,我不知道从哪里开始寻找。

【问题讨论】:

    标签: c# visual-studio-2013 usb exe


    【解决方案1】:

    是否有可能 VS 以管理员身份运行而您以 exe 身份运行 非管理员(意味着 exe 没有提升的权限?) 您可以发布代码或遇到的异常吗?

    【讨论】:

      【解决方案2】:

      我找到了解决方案,但我对此并不满意。它似乎与时间有关。这与权限无关。

      我将一个 USB HID 数据包写入我的设备,然后读取一个响应数据包。

      在 Visual Studio 之外运行 .exe 时,它​​以某种方式认为在发送写入数据包后立即有响应。

      如果我说:

      System.Threading.Thread.Sleep(1)
      

      在写入数据包命令和读取数据包命令之间,它的功能就像在 Visual Studio 中一样。

      这足以让任何人猜测根本原因吗?

      编写报告代码:

      var success = Kernel32.WriteFile(
                      deviceInformation.WriteHandle,
                      outputReportBuffer,
                      outputReportBuffer.Length,
                      ref numberOfBytesWritten,
                      IntPtr.Zero);
      
      Debug.WriteLine(success ?"usbGenericHidCommunication:writeReportToDevice(): -> Write report succeeded"
                                      : "usbGenericHidCommunication:writeReportToDevice(): -> Write report failed!");
      

      读取报告代码:

      nonManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
      nonManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));
      Marshal.StructureToPtr(hidOverlapped, nonManagedOverlapped, false);
      
      // Read the input report buffer
         Debug.WriteLine("usbGenericHidCommunication:readReportFromDevice(): -> Attempting to ReadFile");
      success = Kernel32.ReadFile(
                      deviceInformation.ReadHandle,
                      nonManagedBuffer,
                      inputReportBuffer.Length,
                      ref numberOfBytesRead,
                      nonManagedOverlapped);
      
                  if (!success)
                  {
                      // We are now waiting for the FileRead to complete
                      Debug.WriteLine(
                          "usbGenericHidCommunication:readReportFromDevice(): -> ReadFile started, waiting for completion...");
      
                      // Wait a maximum of 3 seconds for the FileRead to complete
                      var result = Kernel32.WaitForSingleObject(eventObject, 3000);
      
                      switch (result)
                      {
                              // Has the ReadFile completed successfully?
                          case Constants.WaitObject0:
      
                              // Get the number of bytes transferred
                              Kernel32.GetOverlappedResult(deviceInformation.ReadHandle, nonManagedOverlapped, ref numberOfBytesRead, false);
      
                              Debug.WriteLine("usbGenericHidCommunication:readReportFromDevice(): -> ReadFile successful (overlapped) {0} bytes read", numberOfBytesRead);
                              break;
      
                              // Did the FileRead operation timeout?
                          case Constants.WaitTimeout:
      
                              // Cancel the ReadFile operation
                              Kernel32.CancelIo(deviceInformation.ReadHandle);
      

      【讨论】:

      • Debug.WriteLine 会导致短暂的延迟,但仅限于调试模式。由于您的程序对延迟敏感,这可能是它在调试中工作的唯一原因。
      • 另外,不要假设失败意味着等待 I/O。您需要检查GetLastError 才能知道(pinvoke 已经获取了最后一个错误值,您只需查看它)
      猜你喜欢
      • 2011-10-18
      • 2011-11-02
      • 2016-03-08
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 2012-05-10
      • 1970-01-01
      相关资源
      最近更新 更多