Project 1: Opening FTDI Comm Port in C# - Embedded Micro Software

Embedded Micro Software
www.emicros.com
Visual Studio C# 2015
Go to content

Main menu:

PROJECTS
Project #1: Opening FTDI Comm Port in C#

OVERVIEW:

In this project we'll use C# to find out which comm port is assigned to the FTDI UMFT234XF USB to UART module. The example code can be used with most FTDI devices and shows how to use the FTDI DLL to make the appropriate calls for information and control. The end result is a very low cost and high speed serial port that we can build upon for a variety of MicroChip PIC projects.

REQUIRED:

  1. FTDI Driver: Be sure to install the driver for the FTDI module at http://www.ftdichip.com/Drivers/VCP.htm. The current version that I'm using as of 11/11/2016 is http://www.ftdichip.com/Drivers/CDM/CDM21224_Setup.zip.
  2. FDTI DLL: Go to the FTDI C# examples page at http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/CSharp.htm and download the FTD2XX_NET.DLL zipped folder.
  3. C#: Download and install the free Microsoft Visual Studio 2015.
  4. UMFT234XF: Digikey has them for just over $5.95 at http://www.digikey.com/product-search/en?keywords=UMFT234XF

THE DETAILS:

Launch Visual Studio and create a new Windows Forms Application. I named it "P1 FTDI Open Port" and placed it on the C drive at C:\Emicros\Projects\.



Unzip the FTD2XX_NET folder. The current version that I'm using is FTD2XX_NET_v1.0.14 but versions after that should be ok. Copy the 3 files to the new C# project you created.



Under the Project tab click on Add Reference and then browse to your project folder and Add FTD2X_NET.dll. Click OK after adding it.



Double click on Form1 window to open Form1.cs. Near the top of the file add the statement using FTD2XX_NET;



On Form1 add a listbox control and name it listBox3 and also add a button and name it alt_open_btn.



Add the following code shown in green.

     public partial class Form1 : Form
    {
       public Form1()
      {
                InitializeComponent();
      }

      FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
       FTDI Ftdi1 = new FTDI();
       string comstr;
       UInt32 ftdiDeviceCount;

       private void GetVirtuCommPort()
       {
           ftdiDeviceCount = 0;
           
           Ftdi1.ResetPort();

           // Determine the number of FTDI devices connected to the machine
           ftStatus = Ftdi1.GetNumberOfDevices(ref ftdiDeviceCount);

           // Check status
           if (ftStatus == FTDI.FT_STATUS.FT_OK)
           {
               listBox3.Items.Add("Number of FTDI devices: " + ftdiDeviceCount.ToString());
           }
           else
           {
               return;
           }

           if (ftdiDeviceCount == 0)
           {
               alt_open_btn.Text = "Search for FTDI";
               listBox3.Items.Add("No FTDI device found!");
               return;
           }

           // Allocate storage for device info list
           FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new                    
                                                        FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

           // Populate our device list
           ftStatus = Ftdi1.GetDeviceList(ftdiDeviceList);

           if (ftStatus == FTDI.FT_STATUS.FT_OK)
           {
               for (UInt32 i = 0; i < ftdiDeviceCount; i++)
               {
                   listBox3.Items.Add("Device Index: " + i.ToString());
                   listBox3.Items.Add("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                   listBox3.Items.Add("Type: " + ftdiDeviceList[i].Type.ToString());
                   listBox3.Items.Add("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                   listBox3.Items.Add("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                   listBox3.Items.Add("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                   listBox3.Items.Add("Description: " + ftdiDeviceList[i].Description.ToString());
                   listBox3.Items.Add("");
               }
           }
           // Open first device in our list by serial number
           ftStatus = Ftdi1.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
           if (ftStatus != FTDI.FT_STATUS.FT_OK)
           {
               listBox3.Items.Add("Failed to open device (error " + ftStatus.ToString() + ")");
               return;
           }
           Ftdi1.GetCOMPort(out comstr);
           listBox3.Items.Add("Com Port: " + comstr);
           ftStatus = Ftdi1.Close();
           alt_open_btn.Text = "Open VIRTU Found On " + comstr;
           if (ftStatus != FTDI.FT_STATUS.FT_OK)
           {
               listBox3.Items.Add("Failed to open device (error " + ftStatus.ToString() + ")");
               return;
           }

       }

The last statement to add is the GetVirtuCommPort(); to Form1_Load() as shown below.

       private void Form1_Load(object sender, EventArgs e)
       {
           GetVirtuCommPort();
       }

Now when you execute the program with an FTDI device connected you should see something similar to the following. In this case I had a BARECAN connected and it was found on Comm Port #4.


Back to content | Back to main menu