mikeobrien/HidLibrary

Best way to loops this.

jeremyoha450 opened this issue · 1 comments

Hi there

how can i gt this code to loop? i mean send command. read full response, send command again so on and so on?
Regards
Jeremy

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;

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

        //Console.WriteLine(Encoding.ASCII.GetString(cardData));
        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.");
    }

I've been able to loop this as so:

// Global HID device information
        private static int deviceCount = 3; 
        private static HidDevice[] _device = new HidDevice[deviceCount];


//in your Main() method
            int count = 0;
            foreach (var device in HidDevices.Enumerate(0x0ACD, 0x0500)) // 
            {
                      _device[count] = device;
                      if (_device[count != null)
                      {
                                 _device[count].OpenDevice();
                                 _device[count].MonitorDeviceEvents = true;
                                 
                                 if (count == 0)
                                 {
                                           _device[count].Inserted += DeviceAttachedHandler0;
                                           _device[count].Removed += DeviceRemovedHandler0;
                                           _device[count].ReadReport(OnReport0);
                                 }
                                 else if (count == 1)
                                 {
                                           _device[count].Inserted += DeviceAttachedHandler1;
                                           _device[count].Removed += DeviceRemovedHandler1;
                                           _device[count].ReadReport(OnReport1);
                                 }
                                 // and so on for as many devices as you have

                                 _device[count].CloseDevice();
                      }
                      count++;

            }

You will also need the corresponding methods for each device you create:

//attached handlers
        private void DeviceAttachedHandler0()
        {
            _device[0].ReadReport(OnReport0);
        }
        private void DeviceAttachedHandler1()
        {
            _device[1].ReadReport(OnReport1);
        }

//Removed Handlers
        private static void DeviceRemovedHandler0()
        {
        }
        private static void DeviceRemovedHandler1()
        { 
        }

//OnReports

        private void OnReport0(HidReport report) 
        {
            if (!_device[0].IsConnected) { return; } 
            // other card data things
            _device[0].ReadReport(OnReport0); 
        }
        private void OnReport1(HidReport report)
        {
            if (!_device[1].IsConnected) { return; }
           // other card data things
            _device[1].ReadReport(OnReport1);
        }

i would LOVE a better way to do this.

preferably dynamic so that it can create my reports and handlers for me.

that way i don't need to tell it how many devices, it could just create a new event handler and report for each device it finds.

Let me know if this helps or you find a better solution.

Good luck!