Win32: How to find out the number of serial ports?

J

Josef Moellers

Hi,

I'm helping a friend revive some old hardware (a timekeeping device
using smart cards). I've sorted out all the communications details (ISO
7816) and most of it is working fine. The program is written in (you
guessed it) Perl using Win32::API and Serial-Port.

Now I'd like to add a feature to scan the serial ports for the device,
but rather than assume that any modern PC has 2 serial ports, I'd like
to determine exactly how many serial ports the current system supports.

I've looked at the Registry, but that only carried some information
about the serial ports (if you know which ports you have, you can find
out more). A colleague suggested something like cfgmgr, but there
doesn't seem to be a module for this.

Under a more familiar OS, I'd look at /proc/tty/driver/serial, but on XP?

Josef
 
T

Thomas Kratz

Josef said:
Hi,

I'm helping a friend revive some old hardware (a timekeeping device
using smart cards). I've sorted out all the communications details (ISO
7816) and most of it is working fine. The program is written in (you
guessed it) Perl using Win32::API and Serial-Port.

Now I'd like to add a feature to scan the serial ports for the device,
but rather than assume that any modern PC has 2 serial ports, I'd like
to determine exactly how many serial ports the current system supports.

I've looked at the Registry, but that only carried some information
about the serial ports (if you know which ports you have, you can find
out more). A colleague suggested something like cfgmgr, but there
doesn't seem to be a module for this.

Under a more familiar OS, I'd look at /proc/tty/driver/serial, but on XP?

Here's a way using the WMI interface. I guess you could do the same by
going through the registry after resolving a bunch of cryptic device_ids,
but this is a case where WMI actually saves time and nerves.

use strict;
use warnings;

use Win32::OLE qw/in/;
Win32::OLE->Option(Warn => 1);

my $wmi = Win32::OLE->GetObject(
"winmgmts:{impersonationLevel=impersonate,(security)}"
) or die
"error initializing WMI interface, ",
Win32::OLE->LastError;

print $_->{DeviceID}, "\n"
for in($wmi->InstancesOf('Win32_SerialPort'));

If you need more information about the properties of the serial port, get
the "WMI Object Browser" with the free "WMI Tools" from M$.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
J

Josef Moellers

Thomas said:
Josef Moellers wrote:
Here's a way using the WMI interface. I guess you could do the same by
going through the registry after resolving a bunch of cryptic
device_ids, but this is a case where WMI actually saves time and nerves.

use strict;
use warnings;

use Win32::OLE qw/in/;
Win32::OLE->Option(Warn => 1);

my $wmi = Win32::OLE->GetObject(
"winmgmts:{impersonationLevel=impersonate,(security)}"
) or die
"error initializing WMI interface, ",
Win32::OLE->LastError;

print $_->{DeviceID}, "\n"
for in($wmi->InstancesOf('Win32_SerialPort'));

Thanks, "Tausend dank".

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top