{"id":219,"date":"2016-12-20T11:51:46","date_gmt":"2016-12-20T11:51:46","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=219"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"ftdi-usb-devices","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/serial-port\/ftdi-usb-devices","title":{"rendered":"FTDI USB Devices"},"content":{"rendered":"<h4>\n\tFinding Connected FTDI Devices<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nusing System.IO.Ports;\r\nusing Microsoft.Win32;\t\t\/\/&lt; Needed to access registry\r\n<\/code><\/pre>\n<pre>\r\n<code>\r\n\tint Count;\r\n\tint CheckEachPortCount;\r\n\tstring RegistryMainFolder;\r\n\tstring Port;\r\n\tstring[] FoundCommPorts = new string[0];\r\n\tstring[] FoundSerialNumbers = new string[0];\r\n\r\n\t\/\/----- DISPLAY AVAILABLE SERIAL PORTS -----\r\n\tcmbSerialPort.Items.Add(&quot;Not connected&quot;);\r\n\ttry\r\n\t{\r\n\t\t\/\/----- DISCOVER ALL AVAILABLE SERIAL PORTS -----\r\n\t\tstring[] AvailableSerialPorts;\r\n\t\tAvailableSerialPorts = SerialPort.GetPortNames();\r\n\t\t\t\t\r\n\t\t\/\/----- CHECK REGISTRY TO FIND MATCHING FDTI DEVICES AND ONLY INCLUDE THOSE COMM PORTS -----\r\n\t\t\/\/(We only show comm ports for our devices that are currently connected)\r\n\t\t\/\/This is the registry path we&#39;re interested in (taken from FTDI web site):-\r\n\t\t\/\/HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS\\VID_VID+PID_PID+Serial_Number\\0000\\DeviceParameters\\PortName\r\n\r\n\t\t\/\/Retrieve all the subkeys for the specified key.\r\n\t\tRegistryMainFolder= &quot;SYSTEM\\\\CurrentControlSet\\\\Enum\\\\FTDIBUS&quot;;\r\n\r\n\t\tRegistryKey rkey = Registry.LocalMachine;\r\n\t\trkey = rkey.OpenSubKey(RegistryMainFolder);\r\n\t\tstring[] names = rkey.GetSubKeyNames();\r\n\r\n\t\tforeach (string name in names)\r\n\t\t{\r\n\t\t\t\/\/----- CHECK NEXT FTDI DEVICE IN THE REGISTRY -----\r\n\t\t\t\/\/The name will be like this\r\n\t\t\t\/\/\tname = VID_VID+PID_PID+\r\n\t\t\t\/\/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\r\n\t\t\t\/\/if you wanted to.  This code however simply allows all FTDI devices.\r\n\t\t\t\t\t\r\n\t\t\tif ( (name.Contains(&quot;PID&quot;)) &amp;&amp; (name.Contains(&quot;VID&quot;)) )\r\n\t\t\t{\r\n\t\t\t\trkey = Registry.LocalMachine;\r\n\t\t\t\trkey = rkey.OpenSubKey(RegistryMainFolder);\r\n\t\t\t\trkey = rkey.OpenSubKey(name);\r\n\t\t\t\trkey = rkey.OpenSubKey(&quot;0000\\\\Device Parameters&quot;);\r\n\t\t\t\tPort = Convert.ToString(rkey.GetValue(&quot;PortName&quot;));\t\t\t\/\/Will be COM1 etc\r\n\r\n\t\t\t\tfor (CheckEachPortCount = 0; CheckEachPortCount &lt; AvailableSerialPorts.Length; CheckEachPortCount++)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (AvailableSerialPorts[CheckEachPortCount].ToString() == Port.ToString())\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/THIS PORT EXISTS SO DEVICE IS CONNECTED - ADD IT TO THE ARRAY OF FOUND DEVICES\r\n\t\t\t\t\t\tArray.Resize(ref FoundCommPorts, FoundCommPorts.Length + 1);\r\n\t\t\t\t\t\tArray.Resize(ref FoundSerialNumbers, FoundSerialNumbers.Length + 1);\r\n\t\t\t\t\t\tFoundCommPorts[FoundCommPorts.Length - 1] = Port;\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\trkey.Close();\r\n\r\n\t\t\/\/ADD EACH OF THE FOUND DEVICES TO THE COMM PORT COMBO BOX\r\n\t\tif (FoundCommPorts.Length &gt; 0)\r\n\t\t{\r\n\t\t\tArray.Sort(FoundCommPorts, FoundSerialNumbers);\t\t\/\/Sort by port number\r\n\r\n\t\t\tfor (Count = 0; Count &lt; FoundCommPorts.Length; Count++)\r\n\t\t\t\tcmbSerialPort.Items.Add(FoundCommPorts[Count]);\r\n\t\t}\r\n\t\tif (cmbSerialPort.Items.Count == 2)\r\n\t\t\tcmbSerialPort.SelectedIndex = 1;\r\n\t\telse\r\n\t\t\tcmbSerialPort.SelectedIndex = 0;\r\n\t}\r\n\tcatch(Exception )\r\n\t{\r\n\r\n\t}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Finding Connected FTDI Devices using System.IO.Ports; using Microsoft.Win32; \/\/&lt; Needed to access registry int Count; int CheckEachPortCount; string RegistryMainFolder; string Port; string[] FoundCommPorts = new string[0]; string[] FoundSerialNumbers = new string[0]; \/\/&#8212;&#8211; DISPLAY AVAILABLE SERIAL PORTS &#8212;&#8211; cmbSerialPort.Items.Add(&quot;Not connected&quot;); try { \/\/&#8212;&#8211; DISCOVER ALL AVAILABLE SERIAL PORTS &#8212;&#8211; string[] AvailableSerialPorts; AvailableSerialPorts = SerialPort.GetPortNames(); \/\/&#8212;&#8211; CHECK [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-serial-port"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/219\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}