我正在使用Mike O Brien's HID Library连接到电子尺,设备成功打开,然后完美地显示设备附加和删除的消息。,但是最初只运行OnReport大约20次。
在OnReport上运行了大约20次之后,它再也不会运行了,除非我拔掉USB线并重新连接。
我的代码如下
if (scale.IsConnected)
{
scale.Inserted += DeviceAttachedHandler;
scale.Removed += DeviceRemovedHandler;
scale.MonitorDeviceEvents = true;
scale.ReadReport(OnReport);
MessageBox.Show("Hold Application Here");scale的事件处理程序
private void DeviceAttachedHandler()
{
MessageBox.Show("Device attached.");
scale.ReadReport(OnReport);
}
private static void DeviceRemovedHandler()
{
MessageBox.Show("Device removed.");
}
private void OnReport(HidReport report)
{
if (!scale.IsConnected) { return; }
//var cardData = new Data(report.Data);
decimal weight = Convert.ToDecimal(report.Data[4]);// (Convert.ToDecimal(report.Data[4]) +
MessageBox.Show(weight.ToString());
//Convert.ToDecimal(report.Data[5]) * 256) / 100;
//Console.WriteLine(!cardData.Error ? Encoding.ASCII.GetString(cardData.CardData) : cardData.ErrorMessage);
//Console.WriteLine(report.Data);
scale.ReadReport(OnReport);
}发布于 2012-10-18 23:21:46
我设法让scale工作,在我的回调中,当scale返回数据时,我正在做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.");
}
}
}https://stackoverflow.com/questions/10130837
复制相似问题