gpt4 book ai didi

c# - 连接到数字秤(Mettler Toledo PS90)作为 HID 仅当电缆首次连接到计算机时运行 On Report

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:14 25 4
gpt4 key购买 nike

我正在使用 Mike O Brien's HID Library连接到数字秤,设备成功打开,然后完美显示设备附加和删除消息。但最初只运行 OnReport 大约 20 次
Inital 在 OnReport 上运行大约 20 次后,它再也不会运行,除非我拔下 USB 电缆并重新连接。

我的代码在下面

if (scale.IsConnected)
{
scale.Inserted += DeviceAttachedHandler;
scale.Removed += DeviceRemovedHandler;
scale.MonitorDeviceEvents = true;
scale.ReadReport(OnReport);
MessageBox.Show("Hold Application Here");

秤的事件处理程序

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);
}

最佳答案

我设法让秤工作,在我的回调中,当秤返回数据时运行,我正在执行 Read,这是一个阻塞调用。因此产生了死锁,应该只使用 ReadReportRead 看看 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.");
}
}
}

关于c# - 连接到数字秤(Mettler Toledo PS90)作为 HID 仅当电缆首次连接到计算机时运行 On Report,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com