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

Discussion in 'Perl Misc' started by Josef Moellers, Mar 15, 2005.

  1. 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Mar 15, 2005
    #1
    1. Advertising

  2. Josef Moellers

    Thomas Kratz Guest

    Josef Moellers wrote:
    > 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^.-
     
    Thomas Kratz, Mar 15, 2005
    #2
    1. Advertising

  3. Thomas Kratz wrote:
    > Josef Moellers wrote:


    >> 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.


    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Mar 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Hoag

    Can't locate serial ports

    Daniel Hoag, Feb 11, 2004, in forum: Java
    Replies:
    3
    Views:
    535
    Daniel Hoag
    Feb 13, 2004
  2. Sven Goosen
    Replies:
    0
    Views:
    717
    Sven Goosen
    Apr 3, 2004
  3. r norman

    Controlling Serial Ports

    r norman, Jun 27, 2004, in forum: C++
    Replies:
    2
    Views:
    362
  4. Max Kotasek
    Replies:
    4
    Views:
    1,030
    Max Kotasek
    Apr 9, 2010
  5. mreister
    Replies:
    1
    Views:
    3,386
    mreister
    May 25, 2010
Loading...

Share This Page