我正在尝试连接到USB电子秤。当scale.IsConnected实现时,代码确实会连接到scale,但它在scale.Read(250)上挂起,其中250应该是以毫秒为单位的超时,但它从未从读取中返回。
我使用的是this thread中的代码,但有一处更改是由于新版本的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;
}我设法让标尺正常工作,看看迈克的答案here
发布于 2012-10-18 23:22:30
我设法让天平工作起来了。在我的回调中,当scale返回数据时运行,我正在做Read,这是一个阻塞调用。
所以产生了一个死锁,我应该只使用ReadReport或Read。看看Mike的例子,他在下面发布了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.");
}
}
}发布于 2012-04-27 21:13:20
我不能帮助你解决你的问题,但是不久前我不得不将脚踏开关集成到一个应用程序中,我发现这个USB C#库工作得很好:
也许你应该试一试,因为它集成起来非常简单。
https://stackoverflow.com/questions/10178375
复制相似问题