连接到数字秤(梅特勒-托利多PS90),因为HID仅在电缆首次连接到计算机时运行OnReport

本文关键字:连接 HID 运行 OnReport 计算机 因为 数字 PS90 | 更新日期: 2023-09-27 18:30:50

我正在使用Mike O Brien的HID库连接到数字秤,设备成功打开,然后完美地显示设备附加和删除的消息。但最初
只运行 OnReport 大约 20 次在 OnReport 上运行大约 20 次 Inital 后,除非我拔下 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);
    }

连接到数字秤(梅特勒-托利多PS90),因为HID仅在电缆首次连接到计算机时运行OnReport

我设法让秤工作,在我的回调中,当 scale 返回我正在做的数据时运行Read这是一个阻塞调用。所以造成了一个死锁,应该只使用ReadReport或者Read看看他下面发布的迈克的例子 这里.

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