• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 
April 24, 2024, 04:13:41 pm

News:

SMF - Just Installed!


How to get started talking to a USB device

Started by Colin, July 31, 2011, 01:21:39 pm

Previous topic - Next topic

Colin

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

July 31, 2011, 02:31:06 pm #1 Last Edit: July 31, 2011, 02:34:05 pm by Roman Novgorodov
Hello

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

USB device enumeration (without NrDeviceBox control)

           
    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.

   
   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:

        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.

            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
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Colin

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

    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

    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)

    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)

    // 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

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

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.



    nrHid1.WriteReport(0, cmd1);