Author Topic: How to get started talking to a USB device  (Read 2222 times)

Colin

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to get started talking to a USB device
« on: July 31, 2011, 04:21:39 pm »
I'd like to find out how to get started using nrUsb to talk to a particular USB device.

I have looked at DemoUSBOsrKit and DemoUSB which both see my device in the nrDeviceBox . The details of how the library is used gets hidden by the internals of nrDeviceBox so I don't know how to drive nrUsb directly.

Using C# in Visual Studio 2008, I only want to talk to a particular vendor ID and product ID so I have set those properties in to an instance of nrUsb. I can see methods like Open and Enable but I haven't had any success using those or accessing a pipe.

Could I get some help in the form of a few simple C# statements to open a pipe through to my USB device?



Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: How to get started talking to a USB device
« Reply #1 on: July 31, 2011, 05:31:06 pm »
Hello

Possible I don't understand you correctly, but let me describe common tasks:

USB device enumeration (without NrDeviceBox control)

Code: [Select]
           
    nrUsb1.Update(); // update device list
    for (int i = 0; i < nrUsb1.DeviceCount; i++)
   {
                var dev = (NrUsbDevice)nrUsb1[i]; // get device details
                if(dev.VendorID == 1130 && dev.ProductID == 1001) // check device IDs
                {
                    MessageBox.Show("Device " + dev.NameFriendly + " " + dev.VendorID + " " + dev.ProductID);
                    nrUsb1.DeviceIndex = i; // select needed active device
                    break;
                }
    }

USB driver installation

Before you will try to make I/O operations over USB pipes, you need install compatible driver.

Code: [Select]
   
   nrUsbDriver1.Usb = nrUsb1;  // assign nrUsb1 object to driver component
   nrUsbDriver1.Install();          // start driver installation

USB device I/O operations

Before try make I/O operations you need know pipe ID for out direction  and pipe ID for input direction. See specification of your device or try find these values experimentally.
You should use NrUsbPipePair class for make I/O. You can create this component at run time or at design time: click nrUsb1.PipePairs[] property. After that you set correct values for nrUsbPipePair1.PipeInput and nrUsbPipePair1.PipeOutput properties.

Now data sending looks like here:

Code: [Select]
        private void bSend_Click(object sender, EventArgs e)
        {
            try
            {
                nrUsb1.Active = True;
                nrUsb1.PipePairs[0].Write("Test");
                nrUsb1.Active = False;
            }
            catch (Exception err)
            {
                eInputData.AppendText("\r\nError: " + err.Message);
            }
        }

Incoming data can be available in nrPipePair1.OnDataReceived handler.


Send USB control request

Also you can send special control request to device.

Code: [Select]
            var request = new NrUsbRequest()
            {
                ReqType = NrUsbRequestType.Vendor,
                ReqDirection = NrUsbRequestDirection.HostToDevice,
                ReqRecipient = NrUsbRequestRecipient.Device,
                Index = 0,
                Value = 0,
                Request = func,
                Length = 1,
                Data = new byte[1] { value }
            };
            try
            {
                // send request to device
                nrUsb1.ControlRequest(request);
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error.Message);
            }

Roman Novgorodov
DeepSoftware LLC
« Last Edit: July 31, 2011, 05:34:05 pm by Roman Novgorodov »
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Colin

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to get started talking to a USB device
« Reply #2 on: July 31, 2011, 10:53:30 pm »
Thank you for the detailed reply. I haven't managed to talk to the device yet, it seems that nrDeviceBox handles more than I imagined. I tried entering your example code but it was failing as there were no devices. So I simplified it to

Code: [Select]
    nrUsb1.Update(); // update device list
    Console.WriteLine("Number of devices " + nrUsb1.DeviceCount);

This always showed 0 devices though one of the sample projects was definitely accessing a NrUsbDevice to show product and vendor IDs before.

Instead I tried using nrHid

Code: [Select]
    nrHid1.Update();
    Console.WriteLine("Number of HID devices " + nrHid1.DeviceCount);

This shows the 1 device I expected to be present. So I showed the vendor and product IDs (curiously the vendor and product ID properties have the same name but are not the same case as nrUsb !) which matched (once I remembered I was comparing decimal vs. hexadecimal)

Code: [Select]
    for (int i = 0; i < nrHid1.DeviceCount; i++)
    {
        var dev = (NrHidDevice)nrHid1[i]; // get device details
        Console.WriteLine("HID " + dev.NameFriendly + " " + dev.VendorId + " " + dev.ProductId);
    }

Full of hope, I tried to write something to the device but failed (wrong parameters to Write)

Code: [Select]
    // cmd1 is an array of 8 Byte
    nrHid1.DeviceIndex = 0;
    nrHid1.Active = true;
    nrHid1.Write(cmd1, 0, 8 );   // This did not work
    nrHid1.Active = false;

Can I ask what code do I need to use a nrHid instead of nrUsb to write to the device?



Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: How to get started talking to a USB device
« Reply #3 on: August 01, 2011, 05:59:22 am »
Hello

Thank you for detailed information.

If your device is visible as HID, you need use NrHid component for access to device.
The good news that you do not need install any drivers if your device is detected as HID by Windows.
You should set correct Report ID for make I/O operations.
Please try to play with DemoHID\DemoHID.csproj  project.

Possible some details you will find here:
http://forums.nrcommlib.com/index.php?board=22.0
http://forums.nrcommlib.com/index.php?board=20.0

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Colin

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to get started talking to a USB device
« Reply #4 on: August 02, 2011, 12:18:29 am »
Thanks, I can now send to the HID and receive data back. The crucial changes were to use WriteReport instead of Write and to leave the nrHid1 Active so that the data would get picked up.


Code: [Select]
    nrHid1.WriteReport(0, cmd1);