Integrate an USB Scale with Dynamics AX

I was recently playing with an Dymo USB scale and how to connect it to a Dynamics AX 2012 instance on a virtualized HyperV installation. It turned out that connecting these two is not so hard at all. The Dymo scale was immediately recognized by my PC.

Dymo scale connected to a Windows 10 PC

To access the USB scale, you need to know the Vendor ID and Product ID. This can be found in the Windows device manager. In my case the Vendor ID is 0x0922 and the Product ID is 0x8003

USB Vendor ID and Product ID in device manager

Access the USB Scale via C#

You need the Human Interface Device library, which is also available as source code. Download the Visual Studio project, open the .sln Solution from the SRC folder and build the library: https://github.com/mikeobrien/HidLibrary

Some code is needed to access the scale. Fortunately, there is a code snipped available here: http://r.lagserv.net/scalereader.htm .

  • Create a Visual Studio C# DLL Project.
  • Copy the code for the USBScale class from the website.
  • Add the previous built HIDLibrary to your projects references.
  • Add two int properties to the class, for Vendor ID and Product ID.

    public class USBScale
    {
        public int VendorId { get; set; }
        public int ProductId { get; set; }

  • Change the following line in the GetDevices() method at the USBScale class

public HidDevice[] GetDevices()
{
   //return HidDevices.Enumerate(0x0922, 0x8004).Cast().ToArray();
   return HidDevices.Enumerate(VendorId, ProductId).Cast<HidDevice>().ToArray();

}

  • For convenience, add a new method GetGramm()

    public decimal GetGramm()
    {
        decimal? lb = -1;
        decimal? gr = -1;
        decimal? oz = -1;
        bool? stable = false;

        GetWeight(out lb, out gr, out oz, out stable);

        if (gr.HasValue)
            return gr.Value;
        else
        return -1;
    }

 

  • Build the DLL library

Forward the USB device  to HyperV

There are different ways to pass an USB device to a virtual machine. However, I was using a tool called USB Redirect which is available as trial version for testing. It has two components. The server which manages and shares the USB devices is installed on the physical machine.

USB Redirect

The client is installed on the VM and can access the USB device at the physical machine.

USB Redirect Client

 

Integrate with Dynamics AX

Finally, the last step to integrate with Dynamics AX is easy. Copy the HID Library DLL and the USBScale DLL to the client bin folder. Create a form with a Scale button. At the clicked() method create a new instance of the USBScale class, provide your product and vendor ID and call GetGramm().

<YOUR_NS>.UsbScale scale = new <YOUR_NS>.UsbScale();
scale.set_VendorId(<YOUR_VENDOR_ID);
scale.set_ProductId(<YOUR_PRODUCT_ID);
real value;

value = scale.GetGramm();

info(strfmt(“%1 g”,value));

Dynamics AX with USB Scale