.NET USB Devices
The .NET software archive includes libraries and demo applications for creating custom .NET applications
that communicate with microcontroller devices using USB.
Example Boards
The .NET software is demonstrated on boards running the USB-Boot and USB-Runtime demo applications that are found
within the Embedded SDK kits. The following boards are supported with the required demo applications.
DZX.Devices.dll Library
The devices library contains the USBDevice class for communicating with generic interface USB devices. The class provides the ability to probe for
attached devices. The snippet below shows a variety of ways to probe for devices.
using DZX.Devices.USB;
// Get the paths of all attached USB devices
string[] paths = USBDevice.GetAttached();
// Get the paths of all devices with VID=0x0000 and PID=0x0010
paths = USBDevice.GetAttached(0x0000, 0x0010);
// Get the paths of all devices with VID=0x0000 and PID=0x0010 and Serial Number = 1234567890
paths = USBDevice.GetAttached(0x0000, 0x0010, "1234567890");
Once a device has been found using the static GetAttached method as show above, an instance of the device can be created and a connection can be
established. The snippet below shows how to establish a connection with the device.
using DZX.Devices.USB;
// Probe for any attached devices
string[] paths = USBDevice.GetAttached();
if (paths.Length > 0)
{
USBDevice device = new USBDevice();
// Establish a connection with the first found device
device.Connect(paths[0]);
}
Automatic Connect/Disconnect
The USBDevice class will automatically detect if the physical device is detached and the connection is lost. The class provides a 'Connected' property
that indicates the current connection status. If the device were to be removed and reconnected, the class will automatically re-establish a connection.
using DZX.Devices.USB;
private void Example()
{
device.ConnectedChanged += device_ConnectedChanged;
}
private void device_ConnectedChanged(object sender, EventArgs e)
{
USBDevice device = (sender as USBDevice);
if (device.Connected)
{
// Device just connected
}
else
{
// Device just disconnected
}
}
Sending Data
The USBDevice class provides simple methods for sending and receiving data with the physical device. Once connected, data can be sent to the device in the following manner.
using DZX.Devices.USB;
private void Example()
{
// Create a message, 8-bytes in length
byte[] msg = new byte[8];
// Assign message values (could be anything)
for (int k = 0; k < msg.Length; k++)
{
msg[k] = (byte)k;
}
try
{
// Send the message and only wait for up to 1000 msec for the device to accept it
device.Write(msg, 1000);
}
catch (USBException)
{
// Transfer failed
}
}
Receiving Data
The USBDevice class uses a background thread for receiving data from the device. The background thread is used so that the device is continually being read
so that the device can send data whenever needed without having to wait for the application to explicitly read the device.
Data can be received as a response to a request message in a synchronous fashion. The code snippet below illustrates sending data and waiting for a response
message to be received by the device.
using DZX.Devices.USB;
private void Example()
{
// Create a request message
byte[] request = new byte[8];
try
{
// Send the request message and only wait for up to 1000 msec for the device to accept it
device.Write(request, 1000);
// Wait for a message to be received, waiting for up to 1000 msec
byte[] data = device.Read(1000);
if (data != null)
{
// A response message has been received
}
}
catch (USBException)
{
// Transfer failed
}
}
Data can also be received by periodically polling and accepting all of the received data. The data will be provided in the form of messages that contain timestamps
of when the data has been received by the background reading thread.
using DZX.Devices.USB;
// A periodically called function
private void OnTimer()
{
// Get all of the received messages
USBMessage[] msgs = device.ReadAvailable();
foreach (USBMessage msg in msgs)
{
// Any data with the message?
if (msg.Size > 0)
{
// Print the contents for example
Console.WriteLine(string.Format("{0} - {1}", msg.Timestamp, BitConverter.ToString(msg.Data)));
}
}
}
Demo Application
The USB demo application provides a demonstration of using the DZX.Devices.dll library
to communicate with devices. The application also provides the ability to update the firmware/software within
the device if it is running the DZX USB DFU Bootloader.
Test Connection
The test connection button will send a message with random data to the selected device. The application will then wait to receive a response. The application
is expecting the device to echo the message. The device should be running the USB-Runtime demo application which will echo all received messages. The results are
displayed within the lower portion of the dialog.
File Download
The application demonstrates how to download new firmware/software to a device using the DZX.Devices.dll library. Begin by
selecting the data file (.hex, .srec, or .bin) to be downloaded to the device. By default, the USB-Runtime application within the Embedded SDK kits will already have a
data file that can be used. Use the 'Browse' button to navigate to the location of the data file. Once the file is selected, click 'Download' to have the file contents
loaded into the device. The results of the operation will
be displayed within the lower portion of the dialog.