1 (edited by winters 2018-07-25 23:39:25)

Topic: HidSharp not able to read the device data

Hi, I have a 2D scanner. The device is can be identified correctly, but by some reasons not able to get barcode value from the scanner. I will appreciate it very much if you could give some clues ...
I use HIDSharp20.
In the system it is identified as "HID Keyboard Device".
I use usb scanner YHD-M200

It uses device: "Honeywell Imaging & Mobility     HID POS C0A7AE (VID 3118, PID 2441, version 0.0)".

Output:

   (VID 5102, PID 3, version 0.0)
Dell Dell USB Keyboard ? (VID 16700, PID 8195, version 3.1)
Honeywell Imaging & Mobility     HID POS C0A7AE (VID 3118, PID 2441, version 0.0)
Honeywell Imaging & Mobility     HID Keyboard Emulation C0A7AE (VID 3118, PID 2441, version 0.0)
Honeywell Imaging & Mobility     REM C0A7AE (VID 3118, PID 2441, version 0.0)

Opening HID class device...
HID POS
Max Lengths:
  Input:   64
  Output:  64
  Feature: 2

The operating system name for this device is:
  \\?\hid#vid_0c2e&pid_0989&mi_01#7&24fa9ba&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.

----------------------------------------------------------

using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace HidSharp.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            HidDeviceLoader loader = new HidDeviceLoader();
            Thread.Sleep(2000); // Give a bit of time so our timing below is more valid as a benchmark.

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var deviceList = loader.GetDevices().ToArray();
            stopwatch.Stop();
            long deviceListTotalTime = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Complete device list (took {0} ms to get {1} devices):",
                              deviceListTotalTime, deviceList.Length);
            foreach (HidDevice dev in deviceList)
            {
                Console.WriteLine(dev);
            }
            Console.WriteLine();

            Console.WriteLine("Opening HID class device...");


            var dtest_evice = loader.GetDevices(3118);
            var device = loader.GetDevices(3118).FirstOrDefault(d => d.ProductName == "HID POS");
            if (device == null) { Console.WriteLine("Failed to open device."); Environment.Exit(1); }
            Console.Write(device.ProductName);
            Console.Write(@"
Max Lengths:
  Input:   {0}
  Output:  {1}
  Feature: {2}

The operating system name for this device is:
  {3}

"
, device.MaxInputReportLength
, device.MaxOutputReportLength
, device.MaxFeatureReportLength
, device.DevicePath
);

            HidStream stream;
            if (!device.TryOpen(out stream)) { Console.WriteLine("Failed to open device."); Environment.Exit(2); }
            StringBuilder sb = new StringBuilder();
            using (stream)
            {
                int n = 0;
                while (true)
                {

                    var bytes = new byte[device.MaxInputReportLength];
                    int count = 0;
                   
                    try
                    {
                        count = stream.Read(bytes, 0, bytes.Length);
                                            }
                    catch (TimeoutException)
                    {
              //          Console.Write(count);
                        Console.WriteLine("Read timed out.");
                        continue;
                    }

                    if (count > 0)
                    {
                        var decoded = Encoding.UTF8.GetString(bytes);
                        if (decoded.IndexOf("\u0002\u0003]Q1\n\r\n\0") >= 0)
                        {
                            Console.WriteLine(sb.ToString());
                            sb.Length = 0;
                        }
                        else
                        {
                            sb.Append(decoded.Replace("\u00028]Q1", "").Replace("s\0\u0001", " "));
                        }
                        Console.WriteLine();
                        if (++n == 100)
                        {
                            break;
                        }
                    }
                }
            }

            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }
    }
}

2 (edited by khurram.hameed 2019-04-02 04:33:12)

Re: HidSharp not able to read the device data

Hi,

I am stuck in the same problem anyone please find direct.


I am trying to read data from HID based weight scale (AccuPost 70N) but I also stop at the same point console output is

"Read time out"
"Read time out"
"Read time out"

My code is as follows


-------------------------------------------------------------------------------------------------------------------------------

#define SAMPLE_OPEN_AND_READ
//#define SAMPLE_DYMO_SCALE

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;

namespace HidSharp.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            HidDeviceLoader loader = new HidDeviceLoader();
            Thread.Sleep(2000); // Give a bit of time so our timing below is more valid as a benchmark.

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            var deviceList = loader.GetDevices().ToArray();
            stopwatch.Stop();
            long deviceListTotalTime = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Complete device list (took {0} ms to get {1} devices):",
                              deviceListTotalTime, deviceList.Length);
            foreach (HidDevice dev in deviceList)
            {
                Console.WriteLine(dev);
            }
            Console.WriteLine();

            Console.WriteLine("Opening HID class device...");

            int VID = 0x04d9;
            int PID = 0x8010;

#if SAMPLE_OPEN_AND_READ
            var device = loader.GetDevices(VID, PID).First();
            if (device == null) { Console.WriteLine("Failed to open device."); Environment.Exit(1); }

            Console.Write(@"
Max Lengths:
  Input:   {0}
  Output:  {1}
  Feature: {2}

The operating system name for this device is:
  {3}

"
, device.MaxInputReportLength
, device.MaxOutputReportLength
, device.MaxFeatureReportLength
, device.DevicePath
);

            HidStream stream;
            if (!device.TryOpen(out stream)) { Console.WriteLine("Failed to open device."); Environment.Exit(2); }

            using (stream)
            {
                int n = 0;
                while (true)
                {
                    var bytes = new byte[device.MaxInputReportLength];
                    int count;

                    try
                    {
                        count = stream.Read(bytes, 0, bytes.Length);

                    }
                    catch (TimeoutException)
                    {
                        Console.WriteLine("Read timed out.");
                        continue;
                    }
                   
                    if (count > 0)
                    {
                        Console.Write("* {0} : ", count);

                        for (int i = 0; i < count && i < 62; i++)
                        {
                            Console.Write("{0:X} ", bytes(i));
                        }

                        Console.WriteLine();
                        if (++n == 100) { break; }
                    }
                }
            }
#elif SAMPLE_DYMO_SCALE
            HidDevice scale = loader.GetDeviceOrDefault(VID, PID);
            if (scale == null) { Console.WriteLine("Failed to find scale device."); Environment.Exit(1); }

            HidStream stream;
            if (!scale.TryOpen(out stream)) { Console.WriteLine("Failed to open scale device."); Environment.Exit(2); }

            using (stream)
            {
                int n = 0; DymoScale scaleReader = new DeviceHelpers.DymoScale(stream);
                while (true)
                {
                    int value, exponent;
                    DymoScaleUnit unit; string unitName;
                    DymoScaleStatus status; string statusName;
                    bool buffered;

                    scaleReader.ReadSample(out value, out exponent, out unit, out status, out buffered);
                    unitName = DymoScale.GetNameFromUnit(unit);
                    statusName = DymoScale.GetNameFromStatus(status);

                    Console.WriteLine("{4}  {0}: {1}x10^{2} {3} ", statusName, value, exponent, unitName, buffered ? "b" : " ");
                    if (!buffered) { if (++n == 100) { break; } }
                }
            }
#else
#error "No sample selected."
#endif
           
            Console.WriteLine("Press a key to exit...");
            Console.ReadKey();
        }
    }
}



OUTPUT
-------------------------------------------------------------------------
Complete device list (took 117 ms to get 3 devices):
PixArt Lenovo USB Optical Mouse (VID 0x17ef, PID 0x6019, version 256)
LITE-ON Technology USB NetVista Full Width Keyboard. (VID 0x04b3, PID 0x3025, version 265)
Holtek USB-HID demo code (VID 0x04d9, PID 0x8010, version 256)

Opening HID class device...

Max Lengths:
  Input:   9
  Output:  129
  Feature: 9

The operating system name for this device is:
  \\?\hid#vid_04d9&pid_8010#7&149dc3b3&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.
Read timed out.