HIDlibrary循环写入和读取代码的最佳方式

时间:2018-02-10 11:43:17

标签: c# windows visual-studio-2015

我正在使用HIDLibrary并想知道如何将此代码转换为循环?我的意思是发送命令。阅读完整响应,再次发送命令等等?

代码工作没有问题我可以得到命令等,但想要尝试让它连续循环直到关闭等等。稍后我将添加更多命令但是想先让它循环然后接受它从那里。

试图解释更多,但这就是我所追求的,但网站一直在说我需要解释更多,但没有更多的解释。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HidLibrary;

namespace test
{
    class Program
    {
        private const int VendorId = 1637;
        private const int ProductId = 20833;

        private static HidDevice _device;

        public static string tString1 = null;

        static void Main()
        {
            _device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();

            if (_device != null)
            {
                _device.OpenDevice();

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

                _device.MonitorDeviceEvents = true;

                var message = new byte[] { 0, 81, 80, 73, 71, 83, 183, 169, 13 };
                _device.Write(message);

                _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 = report.Data;

            tString1 += Encoding.ASCII.GetString(cardData);

            if (tString1 != null)
            {
                if (tString1.IndexOf((char)13) > -1)
                {
                    tString1 = tString1.Replace(" ", ",");
                    Console.WriteLine(tString1);
                    Console.WriteLine("");
                    tString1 = null;
                }
                else
                {
                    _device.ReadReport(OnReport);
                }
            }
        }

        private static void DeviceAttachedHandler()
        {
            Console.WriteLine("Device attached.");
            _device.ReadReport(OnReport);
        }

        private static void DeviceRemovedHandler()
        {
            Console.WriteLine("Device removed.");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以添加一个while循环来检查Main()中的设备连接状态。

_device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
while (_device.IsConnected)
{
    if (_device == null)
    {
        Console.WriteLine("No device was found");
        return;
    }
    _device.OpenDevice();

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

    _device.MonitorDeviceEvents = true;

    var message = new byte[] { 0, 81, 80, 73, 71, 83, 183, 169, 13 };
    // Adding a timeout here would prevent hanging issue
    _device.Write(message, 500);

     _device.ReadReport(OnReport);

    //Console.WriteLine("Reader found, press any key to exit.");
    //Console.ReadKey();

    //_device.CloseDevice();
}