[Israel.pm] Searching for COM ports in Windows

Dov Levenglick dov.levenglick at gmail.com
Thu Jan 10 00:10:01 PST 2013

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.

use strict;

use warnings;

use Win32::TieRegistry;

my $RegistryAccess = {Access=>"KEY_READ", Delimiter=>"/"};

my $Enum = Win32::TieRegistry->new(
"HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Enum", $RegistryAccess);


sub search_ports_recursive


    my $registry = shift;

    my $friendly_name;

    my @subkey_names = grep !/^Properties$/, $registry->SubKeyNames();

    foreach my $name (@subkey_names)


        my $subkey = $registry->Open($name, $RegistryAccess);

        my $class = $subkey->GetValue("Class");

        if ($class and "Ports" eq $class)


            $friendly_name = $subkey->GetValue("FriendlyName");

            my $control = $subkey->Open("Device Parameters", $RegistryAccess

            my $port = $control->GetValue("PortName");

            print "$port: $friendly_name\n";








Output (on my machine) in the format of <port number>: <device friendly
name>. :

LPT1: ECP Printer Port (LPT1)

COM1: Communications Port (COM1)

COM3: Intel(R) Active Management Technology - SOL (COM3)

COM5: HP un2430 Mobile Broadband Module Diagnostics (COM5)

COM7: HP un2430 Mobile Broadband Module NMEA (COM7)

       בברכה  |  Best Regards,
דוב לוונגליק  |  Dov Levenglick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20130110/f3550820/attachment.htm 

More information about the Perl mailing list