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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|