{"id":866,"date":"2013-03-15T11:22:24","date_gmt":"2013-03-15T11:22:24","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=866"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"specific-usb-devices","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/serial-port\/specific-usb-devices","title":{"rendered":"Specific USB Devices"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nHow To Find Specific USB Devices That Are Connected<br \/>\n<\/h4>\n<p>\nThis example works by looking for a specific USB device VID and PID&nbsp;and was made to work with devices using Microchips USB CDC USB firmware stack. &nbsp;However it will work with all sorts of USB devices.\n<\/p>\n<pre>\r\n<code>\r\n\tusing namespace System::IO::Ports;\t\t\/\/&lt; Needed for USB serial port\r\n\tusing namespace 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 CharPosn;\r\n\tint CheckEachPortCount;\r\n\tString ^RegistryMainFolder;\r\n\tString ^Port;\r\n\tarray&lt;String^&gt; ^Names;\r\n\tString ^Name;\r\n\tarray&lt;String^&gt; ^FoundCommPorts = gcnew array&lt;String^&gt;(0);\r\n\r\n\r\n\t\/\/----- FIND OUR USB SERIAL PORT DEVICE -----\r\n\ttry\r\n\t{\r\n\t\t\/\/----- DISCOVER ALL AVAILABLE SERIAL PORTS -----\r\n\t\tarray&lt;String^&gt; ^AvailableSerialPorts;\r\n\t\t AvailableSerialPorts = SerialPort::GetPortNames();\r\n\r\n\t\t\/\/&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; DEBUG SIMULATE COM PORT CONNECTED\r\n\t\t\/\/Array::Resize(AvailableSerialPorts, AvailableSerialPorts-&gt;Length + 1);\r\n\t\t\/\/AvailableSerialPorts[AvailableSerialPorts->Length - 1] = &quot;COM3&quot;;\r\n\t\t\/\/&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\r\n\r\n\t\t\/\/----- CHECK REGISTRY TO FIND MATCHING USB 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\r\n\t\t\/\/HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USB\\[VID_VID+PID_PID]\\[RandomName]\\DeviceParameters\\PortName\r\n\r\n\t\t\/\/Retrieve all the subkeys for the specified key.\r\n\t\tRegistryMainFolder= L&quot;SYSTEM\\\\CurrentControlSet\\\\Enum\\\\USB&quot;;\r\n\t\tRegistryKey ^rkey = Registry::LocalMachine;\r\n\t\trkey = rkey-&gt;OpenSubKey(RegistryMainFolder);\r\n\t\tNames = rkey-&gt;GetSubKeyNames();\r\n\r\n\t\tfor each (Name in Names)\r\n\t\t{\r\n\t\t\t\/\/----- CHECK NEXT USB DEVICE IN THE REGISTRY -----\r\n\t\t\t\/\/This example is based on finding only devices where the USB name includes a specific VID and PID value like this:\r\n\t\t\t\/\/name = VID_04D8&amp;PID_F677\r\n\r\n\t\t\tif (\r\n\t\t\t\t(Name-&gt;Contains(&quot;VID_04D8&quot;)) &amp;&amp;\t\t\/\/&lt;&lt;&lt;&lt;&lt;&lt;&lt; OUR VID VALUE OUR USB DEVICE NAME WILL CONTAIN\r\n\t\t\t\t(Name-&gt;Contains(&quot;PID_F677&quot;))\t\t\/\/&lt;&lt;&lt;&lt;&lt;&lt;&lt; OUR PID VALUE OUR USB DEVICE NAME WILL CONTAIN\r\n\t\t\t)\r\n\t\t\t{\r\n\t\t\t\t\/\/----- THIS REGISTRY ENTRY LISTS ALL USB DEVICES WITH OUR VID AND PID -----\r\n\t\t\t\tRegistryKey ^rkey2 = Registry::LocalMachine;\r\n\t\t\t\trkey2 = rkey2-&gt;OpenSubKey(RegistryMainFolder);\r\n\t\t\t\trkey2 = rkey2-&gt;OpenSubKey(Name);\r\n\t\t\t\tNames = rkey2-&gt;GetSubKeyNames();\r\n\t\t\t\tfor each (Name in Names)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/----- GET NEXT USB DEVICE ENTRY -----\r\n\t\t\t\t\ttry\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tRegistryKey ^rkey3 = Registry::LocalMachine;\r\n\t\t\t\t\t\trkey3 = rkey2-&gt;OpenSubKey(Name + L&quot;\\\\Device Parameters&quot;);\r\n\t\t\t\t\t\tPort = Convert::ToString(rkey3-&gt;GetValue(&quot;PortName&quot;));\t\t\t\/\/Will be &quot;COM1&quot; etc\r\n\r\n\t\t\t\t\t\t\/\/IS THIS USB DEVICE CURRENTLY CONNECTED?\r\n\t\t\t\t\t\tfor (CheckEachPortCount = 0; CheckEachPortCount &lt; AvailableSerialPorts-&gt;Length; CheckEachPortCount++)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tif (AvailableSerialPorts[CheckEachPortCount]-&gt;ToString() == Port-&gt;ToString())\r\n\t\t\t\t\t\t\t{\r\n\t\t\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\t\t\tArray::Resize(FoundCommPorts, FoundCommPorts-&gt;Length + 1);\r\n\t\t\t\t\t\t\t\tFoundCommPorts[FoundCommPorts->Length - 1] = Port;\r\n\t\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\trkey3-&gt;Close();\r\n\t\t\t\t\t}\r\n\t\t\t\t\tcatch(System::Exception ^)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t}\t\t\t\t\t\t\r\n\t\t\t\t}\r\n\t\t\t\trkey2-&gt;Close();\r\n\t\t\t}\r\n\t\t}\r\n\t\trkey-&gt;Close();\r\n\r\n\t\t\/\/ADD EACH OF THE FOUND DEVICES TO THE COMM PORT COMBO BOX\r\n\t\tif (FoundCommPorts-&gt;Length)\r\n\t\t{\r\n\t\t\tArray::Sort(FoundCommPorts);\t\t\/\/Sort by port number\r\n\r\n\t\t\tfor (Count = 0; Count &lt; FoundCommPorts-&gt;Length; Count++)\r\n\t\t\t{\r\n\t\t\t\tcmbCommPort-&gt;Items-&gt;Add(FoundCommPorts[Count]);\r\n\t\t\t}\r\n\r\n\t\t}\r\n\t}\r\n\tcatch(System::Exception^ e)\r\n\t{\r\n\t\t\/\/MessageBox::Show(L&quot;Error looking for USB device:\\n&quot; + e, L&quot;Error&quot;, MessageBoxButtons::OK, MessageBoxIcon::Error);\r\n\t}\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; How To Find Specific USB Devices That Are Connected This example works by looking for a specific USB device VID and PID&nbsp;and was made to work with devices using Microchips USB CDC USB firmware stack. &nbsp;However it will work with all sorts of USB devices. using namespace System::IO::Ports; \/\/&lt; Needed for USB serial port [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-serial-port"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=866"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/866\/revisions"}],"predecessor-version":[{"id":1017,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/866\/revisions\/1017"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}