Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, October 14, 2005 12:34 AM
Are there any libraries or classes that allow C# applications to talk to devices on a the USB (Universal Serial Bus)?
All replies (14)
Friday, October 14, 2005 2:02 AM ✅Answered
I don't think theres any specific namespaces in the framework. You might want to take a look at this article which deals with working with USB devices and has some good pointers.
Cathal
Monday, October 17, 2005 3:28 AM ✅Answered
Read this article and follow some links from it http://msdn.microsoft.com/coding4fun/someassemblyrequired/isthatyou/default.aspx
As well you can look at http://www.componentsnotebook.com/notebooks/csharp/deviceio.aspx
or
http://www.icsharpcode.net/opensource/sharpusblib/
Friday, December 18, 2009 8:22 AM | 2 votes
this code defintly help u...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UsbLibrary;
namespace UsbApp
{
public partial class Sniffer : Form
{
public Sniffer()
{
InitializeComponent();
}
private void usb_OnDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("Found a Device");
}
private void usb_OnDeviceRemoved(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(usb_OnDeviceRemoved), new object[] { sender, e });
}
else
{
this.lb_message.Items.Add("Device was removed");
}
}
private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("My device was found");
//setting string form for sending data
string text = "";
for (int i = 0; i < this.usb.SpecifiedDevice.OutputReportLength - 1; i++)
{
text += "000 ";
}
this.tb_send.Text = text;
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
usb.RegisterHandle(Handle);
}
protected override void WndProc(ref Message m)
{
usb.ParseMessages(ref m);
base.WndProc(ref m); // pass message on to base form
}
private void btn_ok_Click(object sender, EventArgs e)
{
try
{
this.usb.ProductId = Int32.Parse(this.tb_product.Text, System.Globalization.NumberStyles.HexNumber);
this.usb.VendorId = Int32.Parse(this.tb_vendor.Text, System.Globalization.NumberStyles.HexNumber);
this.usb.CheckDevicePresent();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[arrText.Length];
for (int i = 0; i < arrText.Length; i++)
{
if (arrText[i] != "")
{
int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.Number);
data[i] = (byte)Convert.ToByte(value);
}
}
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Sorry but your device is not present. Plug it in!! ");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void usb_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(usb_OnSpecifiedDeviceRemoved), new object[] { sender, e });
}
else
{
this.lb_message.Items.Add("My device was removed");
}
}
private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{
string rec_data = "Data: ";
foreach (byte myData in args.data)
{
if (myData.ToString().Length == 1)
{
rec_data += "00";
}
if (myData.ToString().Length == 2)
{
rec_data += "0";
}
rec_data += myData.ToString() + " ";
}
this.lb_read.Items.Insert(0, rec_data);
}
}
private void usb_OnDataSend(object sender, EventArgs e)
{
this.lb_message.Items.Add("Some data was send");
}
}
}
Tuesday, February 16, 2010 4:40 PM
Hi Sahil,
Thankyou for posting the code. How about the usbLibrary that was used on the code? Is that handling the USB connectivity - how was implemented? I appricate if you could discuss little more about that. Thank you.ZZZ
Friday, March 26, 2010 6:44 AM | 1 vote
any library for usb in .net ????? which library have been used by sahil???? i feel lack of discussion....i want to communicate with usb devices.....Thanksss in advance with ur help that u u would put forward now....
Wednesday, April 28, 2010 10:47 AM
Hi !
I do not know about the program here above, but you could be interested in this library:
http://www.icsharpcode.net/opensource/sharpusblib/
Regards,
scamb
Tuesday, May 4, 2010 1:50 PM
Hi there,
The code in that Sniffer app was a big help to me but I've found a limitation in it and the usblibrary that it's using - it is not capable of picking up a usb device that does not have suitable drivers on the PC.
I'm looking to make an application that will work as a kind of auditor/monitor of anything that is plugged into a PC (for security) but I can't find anything that will let me identify a USB device with no drivers. I believe that this is because such a device does not get associated with any Device class. Does anyone know of a way to handle such cases? It would be massively appreciated!
Wednesday, May 5, 2010 4:31 AM
I'll be coming with a great usb help(c#) soon as i am into it deeply now...Guys have to wait till I succeeed....Thankss
Sunday, April 3, 2011 6:19 PM
All usb devices must have a driver associated with it. This is because you are connecting to microprocessor, and you need 'someone' (or in this case something) that 'talks' the same language of the microprocessor. That's what a driver does, it translates the signals send by the microprocessor.
And more important than that, you need to know when a specific microprocessor was connected.
The problem you reported was to do with the way the operation system handles usb devices.
Tuesday, May 3, 2011 9:41 PM
Please, visit this website:
http://www.USBHidNetClass.org
Interface WindForms USB Control for Visual c# and Visual BASIC and Microcontroller
Best Regards
simplicio
Thursday, May 19, 2011 4:18 PM
now, USB was used very popular, why do Microsoft not yet support some class to working with USB in net framework???
Wednesday, December 19, 2012 3:45 PM
Hello.
Do you could make it work on windows 64 bits?
Thank you very much,
Marcos
Thursday, July 11, 2013 2:56 PM
Yeah I know... its pretty dumb and how sad that they don't make framework for USB... <sigh>
Ron Boucher
Sunday, December 30, 2018 8:35 AM
Yes. You should have a look Usb.Net. It's cross platform (Android, Windows, UWP - Linux/MacOS recently added), and it also supports Hid communication.
Here is some sample code that sits across all three platforms and also works with Hid:
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
}