Share via


usb communication using c#

Question

Monday, November 25, 2013 5:34 AM

hello, 

good morning

in my application i need to communicate with usb using c# in windows forms applications. how to send data to usb and how to receive data from usb

please help me

Thanks and Regards

Rajani B

All replies (4)

Monday, November 25, 2013 9:38 AM âś…Answered

Hi Rajani,

As far as I know, if you want to transfer data througn USB,  you can use the SerialPort Class to connect a serial port, send and receive data.

Here is a link about SerialPort Class you can refer to: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

In the link, there is a sample describes how to use the SerialPort to transfer data.

Also, you can some references and similar cases that may be helpful to you in the following link:
http://social.msdn.microsoft.com/Search/en-US?query=send%20data%20to%20USB%20device&beta=0&ac=8

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Monday, November 25, 2013 5:51 AM

Hi,

You can use the Drive letter of the USB.

For example if the USB Drive letter is F, then your location to the USB will be 

string location = @"F:\"

Unless you want to know how to write data and get data from it. If you want to write data to the USB you must be specific, do you want to copy  a file to the USB or create text files, ect


Monday, November 25, 2013 7:25 AM

See article below

http://www.developerfusion.com/article/84338/making-usb-c-friendly/

There are lots of example as codeproject if you do a Google search for the following

codeproject usb communication c# 

jdweng


Tuesday, February 19, 2019 6:00 AM

Have a look at USB.Net, and Hid.Net. They work on Android, UWP, Windows, MacOS, and Linux.

Here is some sample code:

    private static async Task InitializeTrezor()
    {
        //Register the factory for creating Usb devices. This only needs to be done once.
        UWPUsbDeviceFactory.Register();

        //Register the factory for creating Usb devices. This only needs to be done once.
        UWPHidDeviceFactory.Register();

        //Define the types of devices to search for. This particular device can be connected to via USB, or Hid
        var deviceDefinitions = new List<FilterDeviceDefinition>
        {
            new FilterDeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x" },
            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" }
        };

        //Get the first available device and connect to it
        var devices = await DeviceManager.Current.GetDevices(deviceDefinitions);
        var trezorDevice = devices.FirstOrDefault();
        await trezorDevice.InitializeAsync();

        //Create a buffer with 3 bytes (initialize)
        var buffer = new byte[64];
        buffer[0] = 0x3f;
        buffer[1] = 0x23;
        buffer[2] = 0x23;

        //Write the data to the device
        await trezorDevice.WriteAsync(buffer);

        //Read the response
        var readBuffer = await trezorDevice.ReadAsync();
    }