Here are two classes for dealing with USB/Hid at the raw Windows API level: Different platforms have different APIs for dealing with both USB and Hid. Hid is usually a little easier to work with than direct USB. A USB device may or may not be a Hid device and vice versa. USB devices usually fall in to two categories: Hid, and USB. Otherwise, you will receive a COM object error at runtime when closing. You have to make sure you call the Dispose() method when closing your application. Void watcher_EventRemoved(object sender, EventArrivedEventArgs e) Void watcher_EventArrived(object sender, EventArrivedEventArgs e) / Used to dispose of the USB device watchers when the USBControl class is disposed of. WatcherRemove.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3") WatcherRemove.EventArrived += new EventArrivedEventHandler(watcher_EventRemoved) var queryRemove = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3") WatcherRemove = new ManagementEventWatcher() WatcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2") WatcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived) var queryAttach = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2") WatcherAttach = new ManagementEventWatcher() Private ManagementEventWatcher watcherRemove Private ManagementEventWatcher watcherAttach used for monitoring plugging and unplugging of USB devices.
KickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)īuffer.BlockCopy(buffer, 0, dst, 0, count) Ĭonsole.I used the following code to detect when USB devices were plugged and unplugged from my computer: class USBControl : IDisposable Public ReliableSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) Public class ReliableSerialPort : SerialPort
Code is available for download here : Download code
At the openning of the serial port, a continous read function is started, raising an event (OnDataReceived) when some bytes arrive into the reception buffer.Ī full code example is shown below. An example of implementation of a SerialPort named OptimizedSerialPort is proposed here. The behaviour makes the System.IO.Ports.SerialPort impossible to use.Ī solution for getting a reliable solution is to use the WinAPI serial port accessed through BaseStream. Whatever the settings of the SerialPort are, data will be transmitted only upon the arrival of a special character. Also, if you try to read periodicaly the number of bytes available on the serial port, BytesToRead will always return 0. If you don’t send this « special » byte, the event will not trigger.
In WPF applications, SerialPort doesn’t work anymore, event if it has the same attributes and methods ! More precisely, DataReceived event will trigger only when a few characters are sent, including ‘0x0A’ (‘\n’).
In Winform applications, SerialPort available from the ToolBox is working in most tested situations, including using DataReceived event and BytesToRead function in order to know how many bytes are available in the reception buffer. Let’s focus on different tested situations in order to have an idea of what is working and not. NET system.IO.Ports.SerialPort », C# implementation of the serial port is not reliable at all. As explained by Ben Voigt in his excellent article « If you must use a.