1

I am working on a Windows Forms Application that should get data from a USB Scale. The USB scale is handled like a Keyboard. If someone puts something on the scale, the scale starts to type the weight String like a USB Keyboard. Before, I'd let the scale type the Weight String into a Textbox by clicking into textBox in the Forms App. But now I need to obtain the weight string intern without letting the Scale write directly into a textBox. So that the Program can process the data from the scale while it is in Background.

So at first I think I have to choose a Device for the Input. (something like Keyborad on Com Port XY) So I need to create a List including all Input Devices. How do I do this in C# .Net?

I already tried:

string[] devices = GetRawInputDeviceList;
textBox1.Text = devices[0];
textBox2.Text = devices[1];

But this is not working. Could maybe someone tell me how to do that? Or what do you guys think is the best way to solve my Problem? Please Help!

5
  • "But this is not working" -- care to elaborate on that? Commented Mar 10, 2017 at 18:23
  • This post has interesting links on this subject: codeproject.com/Questions/242099/… Commented Mar 10, 2017 at 18:32
  • When trying to write in to the Textboxes it says: "An unhandled exception of type 'System.NullReferenceException' occurred in ScaleCalc75.exe". Commented Mar 10, 2017 at 18:36
  • Thank you for the links! Commented Mar 10, 2017 at 18:50
  • In debug mode, Visual Studio tells you the file name, the line number and the column number of where the exception occurs. Set a break point there and debug! Commented Mar 11, 2017 at 21:50

1 Answer 1

2

I want to inform you that the following code helped me solving my Problem. You will need Mike O’Brien’s USB HID library. You can download it in VisualStudio (NuGet Packages) or here: https://github.com/mikeobrien/HidLibrary

using System;
using System.Linq;
using System.Text;
using HidLibrary;

namespace HIDProject
{
    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.");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.