连接到数字秤(Metler托莱多PS90)作为HID运行OnReport只有当电缆连接到计算机的第一次连接到、电缆、数字、计算机

2023-09-03 22:11:09 作者:堇墨浮华

我使用迈克ØBrien的HID库连接到数字刻度, 设备成功打开则显示设备附加和放大器;删除邮件完美。但仅运行OnReport约20次初步 经过Inital约20运行在OnReport它永远不会再次运行,除非我断开USB连接线并重新连接。

我的code低于

 如果(scale.IsConnected)
      {
          scale.Inserted + = DeviceAttachedHandler;
          scale.Removed + = DeviceRemovedHandler;
          scale.MonitorDeviceEvents = TRUE;
          scale.ReadReport(OnReport);
的MessageBox.show(保持应用程序在这里);
 

事件处理程序的比例

 私人无效DeviceAttachedHandler()
    {
        的MessageBox.show(设备连接。);
        scale.ReadReport(OnReport);
    }

    私有静态无效DeviceRemovedHandler()
    {
        的MessageBox.show(设备除去。);
    }
    私人无效OnReport(HidReport报告)
    {
        (!scale.IsConnected)如果{返回; }


        // VAR cardData =新数据(report.Data);
        小数重量= 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(car​​dData.CardData):cardData.ErrorMessage);
            //Console.WriteLine(report.Data);
        scale.ReadReport(OnReport);
    }
 

解决方案

我设法规模的工作,在我的回调其运行时的规模返回的数据我在做这是一个阻塞调用。因此,一个僵局被创造,应该只使用了 ReadReport 看看小李的例子低于他贴< A HREF =htt​​ps://github.com/mikeobrien/HidLibrary/issues/17相对=nofollow>此处。

 使用系统;
使用System.Linq的;
使用System.Text;
使用HidLibrary;

命名空间MagtekCardReader
{
    类节目
    {
        私人const int的厂商ID = 0x0801;
        私人const int的的ProductId = 0×0002;

        私有静态HidDevice _device;

        静态无效的主要()
        {
            _device = HidDevices.Enumerate(厂商ID,产品).FirstOrDefault();

            如果(_device!= NULL)
            {
                _device.OpenDevice();

                _device.Inserted + = DeviceAttachedHandler;
                _device.Removed + = DeviceRemovedHandler;

                _device.MonitorDeviceEvents = TRUE;

                _device.ReadReport(OnReport);

                Console.WriteLine(读者发现,preSS任意键退出。);
                Console.ReadKey();

                _device.CloseDevice();
            }
            其他
            {
                Console.WriteLine(无法找到读者。);
                Console.ReadKey();
            }
        }

        私有静态无效OnReport(HidReport报告)
        {
            (!_device.IsConnected)如果{返回; }

            VAR cardData =新数据(report.Data);

            Console.WriteLine(cardData.Error Encoding.ASCII.GetString(car​​dData.CardData):!?cardData.ErrorMessage);

            _device.ReadReport(OnReport);
        }

        私有静态无效DeviceAttachedHandler()
        {
            Console.WriteLine(设备连接。);
            _device.ReadReport(OnReport);
        }

        私有静态无效DeviceRemovedHandler()
        {
            Console.WriteLine(设备除去。);
        }
    }
}
 
智 能 一 卡 通 监狱 管 理 系 统

I am using Mike O Brien's HID Library to connect to digital scale, Device opens successfully then displays device attached & removed messages perfectly. But only runs OnReport for about 20 times initially After Inital approx 20 runs on OnReport it never runs again, unless I remove usb cable and re-connect.

My code is below

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

Event Handlers for the 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);
    }

解决方案

I managed to get the scale working, In my callback which runs when scale returns data I was doing Read which is a blocking call. So a deadlock was created, should have only used ReadReport or Read take a look at Mike's example below which he posted 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.");
        }
    }
}

 
精彩推荐
图片推荐