FtdiSharp 0.1.0
See the version list below for details.
dotnet add package FtdiSharp --version 0.1.0
NuGet\Install-Package FtdiSharp -Version 0.1.0
<PackageReference Include="FtdiSharp" Version="0.1.0" />
paket add FtdiSharp --version 0.1.0
#r "nuget: FtdiSharp, 0.1.0"
// Install FtdiSharp as a Cake Addin #addin nuget:?package=FtdiSharp&version=0.1.0 // Install FtdiSharp as a Cake Tool #tool nuget:?package=FtdiSharp&version=0.1.0
FtdiSharp
FtdiSharp is a .NET interface for FTDI USB controllers that provides high-level tools for advanced communication protocols (I²C, SPI, and GPIO) and also supplies a copy of FTDI's official DLL wrapper to provide low-level control for advanced users.
Communicate Directly With Sensors
FtdiSharp aims to simplify the process of interfacing directly with sensors which communicate using I2C or SPI for FTDI chips which support these advanced protocols. No microcontroller is required! Connect your sensor directly to a compatible FTDI device and you can use FtdiSharp to easily control it and make readings. Pair FtdiSharp with with ScottPlot to create real-time data visualization applications with only a few lines of code.
Intended Audience
Do not use FtdiSharp if your project only requires a USB serial port. FtdiSharp is intended for applications which seek to use advanced communication protocols beyond serial UART and provide FTDI-specific actions like reading/writing chip serial numbers. If your project only requires a USB serial port, use System.IO.Ports
which is simpler and officially supported on all operating systems.
Low-Level Driver Access
FtdiSharp provides access FTDI's FTD2XX_NET official DLL wrapper for advanced users. FTDI's official source code has been refactored to break it into smaller files, improve XML documentation, and utilize modern language features. FtdiSharp targets .NET Framework 4.6.2 and .NET 6 so it can be used in .NET Framework and .NET Core environments.
Demo Application
This project comes with a demo app that shows how to directly interface several common sensors. The FT232H can communicate directly with sensors using SPI and I²C, so no microcontroller is required. Unlike FTDI's official code samples (thousands of lines of spaghetti code, often in Visual Basic) the demo application in this project aims to demonstrate common I²C, SPI, and GPIO functionality with minimal complexity.
The demo shows how to use FtdiSharp to interact with common sensors:
- LM75A I²C temperature sensor and thermal watchdog
- BMP280 I²C pressure sensor (0.02 PSI sensitivity)
- LIS3DH I²C 3-axis accelerometer
- ADS1115 I²C 4-channel 16-bit ADC (860 samples per second)
- BH1750 I²C 16-bit ambient light sensor
- MCP3201 SPI 12-bit ADC (100k samples per second)
- HX710 SPI 21-bit ADC (40 samples per second)
- MCP3008 SPI 4-channel 10-bit ADC (200k samples per second)
- ADS1220 SPI 4-channel 24-bit ADC (2k samples per second)
USB Interactions Limit Update Rate
While sensors may support thousands of reads per second, limitations of the USB protocol restrict how frequently FtdiSharp can request data updates from connected devices. The strategy demonstrated on this page is best for applications which only require updates a few times per second. Applications which require high-speed repeated measurements or precise timing should not interface a FTDI controller directly to their sensor, but instead use a high-speed microcontroller to manage the interaction.
Quickstart
// Show all USB FTDI devices attached to the system
foreach (FtdiDevice device in FtdiDevices.Scan())
Console.WriteLine(device);
SPI Protocol
High level functions simplify communication with SPI devices for FTDI chips which support it (e.g., FT232H).
FT232H SPI Pinout
- SCK: D0
- MOSI: D1
- MISO: D2
- CS: D3
Interact with SPI Devices
This code reads voltage from a MCP3201 ADC
// Use the first USB FTDI device found
FtdiDevice device = FtdiDevices.Scan().First();
FtdiSharp.Protocols.SPI spi = new(device);
// Pull the cable select line low and read two bytes
spi.CsLow();
byte[] bytes = spi.ReadBytes(2);
spi.CsHigh();
// Calculate the 14-bit reading according to the datasheet
byte b1 = (byte)(bytes[0] & 0b00011111);
byte b2 = (byte)(bytes[1] & 0b11111110);
int value = ((b1 << 8) + b2) >> 1;
I²C Protocol
High level functions simplify communication with I2C devices for FTDI chips which support it (e.g., FT232H).
FT232H I²C Pinout
- SCL: D0
- SDA: D1 and D2 tied together
Scan for I²C Devices
// Use the first USB FTDI device found
FtdiDevice device = FtdiDevices.Scan().First();
// Create an I2C protocol communicator
FtdiSharp.Protocols.I2C i2c = new(device);
// Show all the I2C addresses in use
foreach (byte address in i2c.Scan())
Console.WriteLine(address);
Interact with an I²C Device
This code reads luminosity from a BH1750 light sensor
// Use the first USB FTDI device found
FtdiDevice device = FtdiDevices.Scan().First();
FtdiSharp.Protocols.I2C i2c = new(device);
// Enter continuous sensor mode
byte address = 0x23;
byte config = 0b00010000;
i2c.Write(address, config);
// Read light intensity as two bytes
byte[] bytes = i2c.Read(address, 2);
// Convert the two bytes to lumens according to the datasheet
double value = (bytes[0] * 256 + bytes[1]) / 1.2;
GPIO Protocol
Pins of a FT232H may be used as general purpose I/O (GPIO) controlled by software.
// Use the first USB FTDI device found
FtdiDevice device = FtdiDevices.Scan().First();
FtdiSharp.Protocols.GPIO gpio = new(device);
// Set pin states
byte direction = 0b11110000; // make D4-D7 outputs
byte value = 0b10100000; // make D7 and D5 high
gpio.Set(direction, value);
// Read pin states
byte reading = gpio.Read();
Additional Resources
Microsoft's System.IO.Ports should be used for applications which only seek to interface with a USB serial adapter.
Microsoft has a dotnet/iot repository with lots of useful code and information about protocols and common devices, including FT232H. I considered using this project but I found support for FT232H to be incomplete, incorrectly documented, and buggy. They have an impressive number of devices though, and it seems this library is being actively developed so I hope it will be more useful in the future.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0-windows7.0 is compatible. net7.0-windows was computed. net8.0-windows was computed. |
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- No dependencies.
-
net6.0-windows7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.2 | 145 | 11/15/2024 |
0.1.1 | 100 | 11/12/2024 |
0.1.0 | 107 | 11/11/2024 |
0.0.3-alpha | 509 | 2/7/2023 |
0.0.2-alpha | 157 | 1/30/2023 |
0.0.1-alpha | 146 | 1/30/2023 |