我正在尝试连接到USB数字刻度,代码确实在scale.IsConnected
成功时连接到刻度,但挂起在scale.Read(250)
,其中250应该是超时(以毫秒为单位)但它永远不会返回来自阅读
我正在使用this帖子中的代码,但由于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;
}
我设法让规模发挥作用,看看Mike的回答here
答案 0 :(得分:4)
我设法让比例正常工作,在我的回调中,当比例返回数据时,我正在执行Read
这是一个阻塞调用。因此,创建了一个死锁,应该只使用ReadReport
或Read
来看看下面发布的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.");
}
}
}
答案 1 :(得分:2)
我无法帮助解决您的问题,但不久之前我必须将脚踏开关集成到应用程序中,我发现这个USB HID C#库完美运行:
http://www.codeproject.com/Articles/18099/A-USB-HID-Component-for-C
也许你应该尝试一下,因为它很容易整合。
大卫