USB HID在C#中的read()挂起 [英] USB HID hangs on Read() in c#

查看:746
本文介绍了USB HID在C#中的read()挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到一个USB数字刻度,代码没有连接到规模为 scale.IsConnected 成真,但挂在 scale.Read(250),其中250应以毫秒为单位的超时时间,但它从来没有从读返回结果。



我使用的代码从除了这是由于的线程= https://github.com/mikeobrien/HidLibrary相对=nofollow>迈克ØBrien的HID图书馆

 公共HidDevice [] GetDevices()
{
HidDevice [] hidDeviceList;


// Metler托莱多
hidDeviceList = HidDevices.Enumerate(0x0eb8).ToArray();
如果(hidDeviceList.Length大于0)
返回hidDeviceList;

返回hidDeviceList;
}






我设法得到大规模的工作,看看小李的回答这里


< DIV CLASS =h2_lin>解决方案

我设法规模的工作,在我的回调它运行时的规模将数据返回我是做这是一个阻塞调用。因此,一个僵局被创造,应该只使用 ReadReport 看看小李的下面的例子,他发布< A HREF =https://github.com/mikeobrien/HidLibrary/issues/17相对=nofollow>此处。

 使用系统; 
使用System.Linq的;
使用System.Text;
使用HidLibrary;

命名空间MagtekCardReader
{
类节目
{
私人const int的厂商ID = 0x0801;
私人const int的产品编号= 0×0002;

私有静态HidDevice _device;

静态无效的主要()
{
_device = HidDevices.Enumerate(厂商ID,产品).FirstOrDefault();

如果(_device!= NULL)
{
_device.OpenDevice();

_device.Inserted + = DeviceAttachedHandler;
_device.Removed + = DeviceRemovedHandler;

_device.MonitorDeviceEvents = TRUE;

_device.ReadReport(OnReport);

Console.WriteLine(读者发现,按任意键退出。);
Console.ReadKey();

_device.CloseDevice();
}
,否则
{
Console.WriteLine(无法找到读者。);
Console.ReadKey();
}
}

私有静态无效OnReport(HidReport报告)
{
如果(_device.IsConnected!){返回; }

变种cardData =新的Data(report.Data);

Console.WriteLine(cardData.Error Encoding.ASCII.GetString(cardData.CardData):!?cardData.ErrorMessage);

_device.ReadReport(OnReport);
}

私有静态无效DeviceAttachedHandler()
{
Console.WriteLine(设备连接);
_device.ReadReport(OnReport);
}

私有静态无效DeviceRemovedHandler()
{
Console.WriteLine(设备中删除。);
}
}
}


I am trying to connect to a USB digital scale, The code does connect to the scale as scale.IsConnected comes true, but hangs on scale.Read(250) where 250 should be the timeout in milliseconds but it never return from Read

I am using the code from this thread except one change which was due to the new version of Mike O Brien's HID Library

public HidDevice[] GetDevices ()
    {
      HidDevice[] hidDeviceList;


      // Metler Toledo
      hidDeviceList = HidDevices.Enumerate(0x0eb8).ToArray();
      if (hidDeviceList.Length > 0)
    return hidDeviceList;

      return hidDeviceList;
    }


I managed to get the scale working, take a look at Mike's answer here

解决方案

I managed to get the scale working, In my callback which runs when scale returns data I was doing Read which is a blocking call. So a deadlock was created, should have only used ReadReport or Read take a look at Mike's example below which he posted here .

using System;
using System.Linq;
using System.Text;
using HidLibrary;

namespace MagtekCardReader
{
    class Program
    {
        private const int VendorId = 0x0801;
        private const int ProductId = 0x0002;

        private static HidDevice _device;

        static void Main()
        {
            _device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();

            if (_device != null)
            {
                _device.OpenDevice();

                _device.Inserted += DeviceAttachedHandler;
                _device.Removed += DeviceRemovedHandler;

                _device.MonitorDeviceEvents = true;

                _device.ReadReport(OnReport);

                Console.WriteLine("Reader found, press any key to exit.");
                Console.ReadKey();

                _device.CloseDevice();
            }
            else
            {
                Console.WriteLine("Could not find reader.");
                Console.ReadKey();
            }
        }

        private static void OnReport(HidReport report)
        {
            if (!_device.IsConnected) { return; }

            var cardData = new Data(report.Data);

            Console.WriteLine(!cardData.Error ? Encoding.ASCII.GetString(cardData.CardData) : cardData.ErrorMessage);

            _device.ReadReport(OnReport);
        }

        private static void DeviceAttachedHandler()
        {
            Console.WriteLine("Device attached.");
            _device.ReadReport(OnReport);
        }

        private static void DeviceRemovedHandler()
        {
            Console.WriteLine("Device removed.");
        }
    }
}

这篇关于USB HID在C#中的read()挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆