Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: finding a serial port with Win32::SerialPort

by Marshall (Prior)
on Aug 22, 2011 at 10:20 UTC ( #921614=note: print w/ replies, xml ) Need Help??


in reply to finding a serial port with Win32::SerialPort

Well this dog just doesn't hunt.
I don't see any code that searches for a COM port.

sub connect { if ($os =~ /Win/i) { #this only works with COM1 ? Huh? # my @ports_win = ('COM1'); foreach ('COM1') #same as foreach (@ports_win){} { open_uart($_); } } }


Comment on Re: finding a serial port with Win32::SerialPort
Download Code
Re^2: finding a serial port with Win32::SerialPort
by pashanoid (Scribe) on Aug 22, 2011 at 10:24 UTC
    Yes, because my @ports_win = ('COM5', 'COM4','COM1', 'COM2',); crashes a win32 system
        Any chance of changing this http://eli.thegreenplace.net/2009/07/31/listing-all-serial-ports-on-windows-with-python/ to perl? Will pay $100 via my visa card. Please use either Win32::TieRegistry or Win32::Registry or if you wish I will make a donation in your name to PerlMonks!
      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; }

        This produces 3 com ports out of 1 existing... any chance of modifying it, so that it would show the propper one working COM2 or COM1? Here is my output:

        [pasha@pelikan file]$ wine reg.exe trying COM1 trying COM2 trying COM3 can't read Device Control Block for \\.\COM3 at registry.pl line 11 unable to open COM3 trying COM4 can't read Device Control Block for \\.\COM4 at registry.pl line 11 unable to open COM4 trying COM5 File not found can't open device: \\.\COM5 at registry.pl line 11 unable to open COM5 trying COM6 File not found can't open device: \\.\COM6 at registry.pl line 11 unable to open COM6 trying COM7 File not found can't open device: \\.\COM7 at registry.pl line 11 unable to open COM7 trying COM8 File not found can't open device: \\.\COM8 at registry.pl line 11 unable to open COM8 trying COM9 File not found can't open device: \\.\COM9 at registry.pl line 11 unable to open COM9 trying COM10 File not found can't open device: \\.\COM10 at registry.pl line 11 unable to open COM10 trying COM11 File not found can't open device: \\.\COM11 at registry.pl line 11 unable to open COM11 trying COM12 File not found can't open device: \\.\COM12 at registry.pl line 11 unable to open COM12 trying COM13 File not found can't open device: \\.\COM13 at registry.pl line 11 unable to open COM13 trying COM14 File not found can't open device: \\.\COM14 at registry.pl line 11 unable to open COM14 trying COM15 File not found can't open device: \\.\COM15 at registry.pl line 11 unable to open COM15 trying COM16 File not found can't open device: \\.\COM16 at registry.pl line 11 unable to open COM16 trying COM17 File not found can't open device: \\.\COM17 at registry.pl line 11 unable to open COM17 trying COM18 File not found can't open device: \\.\COM18 at registry.pl line 11 unable to open COM18 trying COM19 File not found can't open device: \\.\COM19 at registry.pl line 11 unable to open COM19 trying COM20 File not found can't open device: \\.\COM20 at registry.pl line 11 unable to open COM20 trying COM21 File not found can't open device: \\.\COM21 at registry.pl line 11 unable to open COM21 trying COM22 File not found can't open device: \\.\COM22 at registry.pl line 11 unable to open COM22 trying COM23 File not found can't open device: \\.\COM23 at registry.pl line 11 unable to open COM23 trying COM24 File not found can't open device: \\.\COM24 at registry.pl line 11 unable to open COM24 trying COM25 File not found can't open device: \\.\COM25 at registry.pl line 11 unable to open COM25 trying COM26 File not found can't open device: \\.\COM26 at registry.pl line 11 unable to open COM26 trying COM27 File not found can't open device: \\.\COM27 at registry.pl line 11 unable to open COM27 trying COM28 File not found can't open device: \\.\COM28 at registry.pl line 11 unable to open COM28 trying COM29 File not found can't open device: \\.\COM29 at registry.pl line 11 unable to open COM29 trying COM30 File not found can't open device: \\.\COM30 at registry.pl line 11 unable to open COM30 trying COM31 File not found can't open device: \\.\COM31 at registry.pl line 11 unable to open COM31 trying COM32 File not found can't open device: \\.\COM32 at registry.pl line 11 unable to open COM32

        My code:

        #!/usr/bin/perl use strict; use Win32::SerialPort; #my @valid_com_ports; foreach my $port_to_open ( map{"COM$_"}1..32) { print "trying $port_to_open\n"; eval { my $PortObj = new Win32::SerialPort ($port_to_ +open) or print "unable to open $port_to_open\n"; }; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://921614]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-07-31 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls