コード例 #1
0
ファイル: Program.cs プロジェクト: njmube/hidsharp
        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...");

#if SAMPLE_OPEN_AND_READ
			var device = loader.GetDevices(0x1b1c, 0x0c04).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(24726, 344);
            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();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: stephenwhittle/hidsharp
        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...");

#if SAMPLE_OPEN_AND_READ
            var device = loader.GetDevices(0x1b1c, 0x0c04).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(24726, 344);
            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();
        }