none
Communicating with a USB device in C#/WPF RRS feed

  • Question

  • I have seen this question asked repeatedly. I need to talk to a USB device in C#. I would like to send some control transfer request to the device and get the output etc. Is there any C# libraries that can do this? I heard that we can do this using the SerialPort class, but the samples using SerialPort seem mostly in opening and reading/writing to normal ports and not USB. Can someone please point to some links/samples on talking to a USB device and sensing requests ot it?
    • Moved by Sabah ShariqMVP Monday, May 22, 2017 1:51 PM Moved From Visual C#
    • Moved by Bob Ding Thursday, June 1, 2017 9:00 AM
    Monday, May 22, 2017 12:07 PM

All replies

  • There are many types of USB devices, as shown in USB device class drivers included in Windows (Windows Drivers). It is also possible for a device to use the old-style serial interface. You need to specify more of what you need to do. You need to first determine the type of device and specify that.

    The following might help.



    Sam Hobbs
    SimpleSamples.Info

    Monday, May 22, 2017 7:19 PM
  • Hi,

    Try Cypress Suite USB

    http://www.cypress.com/documentation/software-and-drivers/ez-usb-fx3-software-development-kit

    samples:

    http://www.cypress.com/documentation/code-examples/usb-superspeed-code-examples

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 23, 2017 7:56 AM
  • Try Usb.Net.

    Some sample code:

    internal class TrezorExample : IDisposable
        {
            #region Fields
            //Define the types of devices to search for. This particular device can be connected to via USB, or Hid
            private readonly List<FilterDeviceDefinition> _DeviceDefinitions = new List<FilterDeviceDefinition>
            {
                new FilterDeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x", UsagePage=65280 },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x (Android Only)" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, Label="Trezor One Firmware 1.7.x" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, Label="Model T" }
            };
            #endregion
    
            #region Events
            public event EventHandler TrezorInitialized;
            public event EventHandler TrezorDisconnected;
            #endregion
    
            #region Public Properties
            public IDevice TrezorDevice { get; private set; }
            public DeviceListener DeviceListener { get; private set; }
            #endregion
    
            #region Event Handlers
            private void DevicePoller_DeviceInitialized(object sender, DeviceEventArgs e)
            {
                TrezorDevice = e.Device;
                TrezorInitialized?.Invoke(this, new EventArgs());
            }
    
            private void DevicePoller_DeviceDisconnected(object sender, DeviceEventArgs e)
            {
                TrezorDevice = null;
                TrezorDisconnected?.Invoke(this, new EventArgs());
            }
            #endregion
    
            #region Public Methods
            public void StartListening()
            {
                TrezorDevice?.Dispose();
                DeviceListener = new DeviceListener(_DeviceDefinitions, 3000);
                DeviceListener.DeviceDisconnected += DevicePoller_DeviceDisconnected;
                DeviceListener.DeviceInitialized += DevicePoller_DeviceInitialized;
            }
    
            public async Task InitializeTrezorAsync()
            {
                //Get the first available device and connect to it
                var devices = await DeviceManager.Current.GetDevices(_DeviceDefinitions);
                TrezorDevice = devices.FirstOrDefault();
                await TrezorDevice.InitializeAsync();
            }
    
            public async Task<byte[]> WriteAndReadFromDeviceAsync()
            {
                //Create a buffer with 3 bytes (initialize)
                var writeBuffer = new byte[64];
                writeBuffer[0] = 0x3f;
                writeBuffer[1] = 0x23;
                writeBuffer[2] = 0x23;
    
                //Write the data to the device
                return await TrezorDevice.WriteAndReadAsync(writeBuffer);
            }
    
            public void Dispose()
            {
                TrezorDevice?.Dispose();
            }
            #endregion
        }



    Sunday, December 30, 2018 8:42 AM