Finding Connected FTDI Devices


using System.IO.Ports;
using Microsoft.Win32;		//< Needed to access registry

	int Count;
	int CheckEachPortCount;
	string RegistryMainFolder;
	string Port;
	string[] FoundCommPorts = new string[0];
	string[] FoundSerialNumbers = new string[0];

	//----- DISPLAY AVAILABLE SERIAL PORTS -----
	cmbSerialPort.Items.Add("Not connected");
	try
	{
		//----- DISCOVER ALL AVAILABLE SERIAL PORTS -----
		string[] AvailableSerialPorts;
		AvailableSerialPorts = SerialPort.GetPortNames();
				
		//----- CHECK REGISTRY TO FIND MATCHING FDTI DEVICES AND ONLY INCLUDE THOSE COMM PORTS -----
		//(We only show comm ports for our devices that are currently connected)
		//This is the registry path we're interested in (taken from FTDI web site):-
		//HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS\VID_VID+PID_PID+Serial_Number\0000\DeviceParameters\PortName

		//Retrieve all the subkeys for the specified key.
		RegistryMainFolder= "SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS";

		RegistryKey rkey = Registry.LocalMachine;
		rkey = rkey.OpenSubKey(RegistryMainFolder);
		string[] names = rkey.GetSubKeyNames();

		foreach (string name in names)
		{
			//----- CHECK NEXT FTDI DEVICE IN THE REGISTRY -----
			//The name will be like this
			//	name = VID_VID+PID_PID+
			//It is possible to custom program a serial number into FTDO devices which will appear after the final + character and you could look for this
			//if you wanted to.  This code however simply allows all FTDI devices.
					
			if ( (name.Contains("PID")) && (name.Contains("VID")) )
			{
				rkey = Registry.LocalMachine;
				rkey = rkey.OpenSubKey(RegistryMainFolder);
				rkey = rkey.OpenSubKey(name);
				rkey = rkey.OpenSubKey("0000\\Device Parameters");
				Port = Convert.ToString(rkey.GetValue("PortName"));			//Will be COM1 etc

				for (CheckEachPortCount = 0; CheckEachPortCount < AvailableSerialPorts.Length; CheckEachPortCount++)
				{
					if (AvailableSerialPorts[CheckEachPortCount].ToString() == Port.ToString())
					{
						//THIS PORT EXISTS SO DEVICE IS CONNECTED - ADD IT TO THE ARRAY OF FOUND DEVICES
						Array.Resize(ref FoundCommPorts, FoundCommPorts.Length + 1);
						Array.Resize(ref FoundSerialNumbers, FoundSerialNumbers.Length + 1);
						FoundCommPorts[FoundCommPorts.Length - 1] = Port;
						break;
					}
				}
			}
		}
		rkey.Close();

		//ADD EACH OF THE FOUND DEVICES TO THE COMM PORT COMBO BOX
		if (FoundCommPorts.Length > 0)
		{
			Array.Sort(FoundCommPorts, FoundSerialNumbers);		//Sort by port number

			for (Count = 0; Count < FoundCommPorts.Length; Count++)
				cmbSerialPort.Items.Add(FoundCommPorts[Count]);
		}
		if (cmbSerialPort.Items.Count == 2)
			cmbSerialPort.SelectedIndex = 1;
		else
			cmbSerialPort.SelectedIndex = 0;
	}
	catch(Exception )
	{

	}

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.