.NET Data Link
The data link component includes support for communicating with microcontroller devices from a .NET application. The
data link supports communications over native USB, FTDI USB-to-serial, and standard serial port interfaces.
The data link component is a separate component that works atop of the class that provides basic communications with the device. This allows the same
messaging protocol to function regardless of the transport interface.
Support for data link communications is provided by the DZX.Devices.dll library. The library is provided with source code within the .NET Software
download.
USB Data Link
The data link component provides the ability to transfer messages with a native USB using the USBDevice class. The code snippet below shows how to connect
to a USB device and create a the data link to transfer the messages over USB.
using DZX.Devices.DataLinks;
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]);
// Create the application specific data link (generated from Data Link Editor)
DataLinkApp link = new DataLinkApp();
// Assign the device to the link
link.USBDevice = device;
}
By default, the data link will use the default pipes to transmit and receive messages. If the device provides multiple pipes, the link can be assigned
specific pipes for message transfers. The code snippet below shows how to assign specific pipes for transmitting and receiving messages.
// Create the application specific data link
DataLinkApp link = new DataLinkApp();
// Assign the transmit pipe address
link.TxPipe = 0x01;
// Assign the receive pipe address
link.RxPipe = 0x81;
FTDI Data Link
The data link component provides the ability to transfer messages over the link using a connection to an FTDI D2XX device. The
FTDI D2XX drivers must be installed upon the PC in order to use
the FTDI device class. The code snippet below shows how to connect to an FTDI device and create a data link to transfer messages with
the device.
using DZX.Devices.DataLinks;
using DZX.Devices.FTDI;
// Probe for available FTDI devices
FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[] nodes = FTDIDevice.GetAttached();
if (nodes.Length > 0)
{
FTDIDevice device = new FTDIDevice();
// Establish a connection with the first found device
device.Connect(nodes[0]);
// Create the application specific data link (generated from Data Link Editor)
DataLinkApp link = new DataLinkApp();
// Assign the device to the link
link.FTDIDevice = device;
}
Serial Data Link
The data link component provides the ability to transfer messages over the link using a connection to a standard serial port. The code snippet
below shows how to open a serial port and create a data link to transfer messages over the port.
using DZX.Devices.DataLinks;
using System.IO.Ports;
// Probe for available serial ports
string[] ports = SerialPort.GetPortNames();
if (ports.Length > 0)
{
// Establish a connection with the first found port
SerialPort port = new SerialPort(ports[0]);
port.Open();
// Create the application specific data link (generated from Data Link Editor)
DataLinkApp link = new DataLinkApp();
// Assign the port to the link
link.SerialPort = port;
}
Receive Events
The data link class provides events for each of the messages that can be received from the target device. If the event handlers are within a form
or other UI element, it may be necessary to synchronize the events with the thread that created the UI element. The data link class
automatically synchronizes the events to the control that is assigned to the 'ContainerControl' property. The code snippet below shows an example
of assigning a form to the 'ContainerControl' property of the link.
private void Form1_Load(object sender, EventArgs e)
{
// Provide the form to synchronize events with the UI thread
link.ContainerControl = this;
}
Demo Application
The .NET Software Download includes an application, with source code, that demonstrates the usage of a data link. The
application supports communicating with devices over native USB, an FTDI USB-to-serial device, and a standard serial port.
See the Demo Data Links for more information about using the demonstration application.