<div dir="ltr">Hi,<br>Someone at work asked how they can find which device is connected to which COM port in windows. I came up with the following solution (based on his original attempt). I know that this can be optimized. It can also, possibly, be generalized and put on CPAN as a module. This is where you guys come in. Please let me know what you think.<br>

<br>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">use </span><span style="font-size:10.0pt;font-family:Consolas;color:black">strict</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">use </span><span style="font-size:10.0pt;font-family:Consolas;color:black">warnings</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">use </span><span style="font-size:10.0pt;font-family:Consolas;color:black">Win32::TieRegistry</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">RegistryAccess</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= {</span><span style="font-size:10.0pt;font-family:Consolas;color:black">Access</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">=&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;KEY_READ&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">, </span><span style="font-size:10.0pt;font-family:Consolas;color:black">Delimiter</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">=&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;/&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">};</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">Enum</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">Win32::TieRegistry</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">new</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Enum&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">, </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">RegistryAccess</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:black">search_ports_recursive</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">Enum</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> </span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">sub </span><span style="font-size:10.0pt;font-family:Consolas;color:black">search_ports_recursive</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">{</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">registry</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">shift</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">friendly_name</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    </span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">@</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey_names</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">grep </span><span style="font-size:10.0pt;font-family:Consolas;color:black">!</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">/</span><span style="font-size:10.0pt;font-family:Consolas;color:black">^Properties$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">/</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">, </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">registry</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">SubKeyNames</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">();</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">foreach my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">name</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:black">@</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey_names</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">)</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    {</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">        </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">registry</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">Open</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">name</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">, </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">RegistryAccess</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">        </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">class</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">GetValue</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;Class&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">        </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">if </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">class</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">and </span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;Ports&quot; </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">eq </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">class</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">)</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">       
{</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">           
</span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">friendly_name</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">GetValue</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;FriendlyName&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">           
</span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">control</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">Open</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;Device
Parameters&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">, </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">RegistryAccess</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">           
</span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">my </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">port</span><span style="font-size:10.0pt;font-family:Consolas;color:black"> </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">= </span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">control</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">-&gt;</span><span style="font-size:10.0pt;font-family:Consolas;color:black">GetValue</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;PortName&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">           
</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">print </span><span style="font-size:10.0pt;font-family:Consolas;color:blue">&quot;$port:
$friendly_name\n&quot;</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">;</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">       
}</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">        </span><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">else</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#a020f0">        </span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">{</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">           
</span><span style="font-size:10.0pt;font-family:Consolas;color:black">search_ports_recursive</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">(</span><span style="font-size:10.0pt;font-family:Consolas;color:black">$</span><span style="font-size:10.0pt;font-family:Consolas;color:#a000f0">subkey</span><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">);</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>



<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">       
}</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">    }</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#b22200">}</span><span style="font-size:10.0pt;font-family:Consolas"></span></p>

<br><br>Output (on my machine) in the format of <span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">&lt;port
number&gt;: &lt;device friendly name&gt;. </span>:<br>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;;color:black">LPT1:
ECP Printer Port (LPT1)</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;;color:black">COM1:
Communications Port (COM1)</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;;color:black">COM3:
Intel(R) Active Management Technology - SOL (COM3)</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;;color:black">COM5: HP
un2430 Mobile Broadband Module Diagnostics (COM5)</span></p>

<p class="MsoNormal"><span style="font-family:&quot;Courier New&quot;;color:black">COM7: HP
un2430 Mobile Broadband Module NMEA (COM7)</span></p>

<br clear="all"><div><div dir="ltr"><div><br><br></div><div><span style="font-family:&#39;courier new&#39;,monospace">       בברכה  |  Best Regards,  </span></div><div><font face="&#39;courier new&#39;, monospace">דוב לוונגליק  |  Dov Levenglick </font></div>

</div></div>
</div>