Yes, using the code from AnonMonk...
You have to ask Win32 what the names of the COM ports are or probe up to a certain COMxx number.
There are USB ports that can show up as COM ports and I don't know how to create a complete list. I think its hard to do because a new COM port can just "show up" dynamically (i.e. when you plug in a new USB device).
This may not be right, but this is what it does on my machine.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Win32::SerialPort;
use Win32::OLE('in');
use constant wbemFlagReturnImmediately => 0x10;
use constant wbemFlagForwardOnly => 0x20;
my $computer = ".";
my $objWMIService = Win32::OLE->GetObject
("winmgmts:\\\\$computer\\root\\CIMV2")
or die "WMI connection failed.\n";
my $colItems = $objWMIService->ExecQuery
("SELECT * FROM Win32_SerialPortConfiguration",
"WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly);
foreach my $objItem (in $colItems)
{
print "Caption: $objItem->{Caption}\n";
print "Name: $objItem->{Name}\n";
}
__END__
prints:
Caption: COM1
Name: COM1
I think perhaps another possibility is to "probe COM ports".
Use a block "eval" on the "open_uart($_);" statement. >br>
Run through the first 32 ports. If a port works
works then $_ is a valid COM port and push it onto some stack.
my @valid_com_ports;
foreach my $com ( map{"COM$_"}1..32)
{
# decide if this $com is ok or not..
# perhaps use the open_uart function()
# and then push @valid_com_ports, $com;
}
|