Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: finding a serial port with Win32::SerialPort

by Marshall (Canon)
on Aug 22, 2011 at 11:58 UTC ( [id://921632]=note: print w/replies, xml ) Need Help??


in reply to Re^2: finding a serial port with Win32::SerialPort
in thread finding a serial port with Win32::SerialPort

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; }

Replies are listed 'Best First'.
Re^4: finding a serial port with Win32::SerialPort
by pashanoid (Scribe) on Aug 22, 2011 at 15:02 UTC

    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"; }; }
      This sounds good in that the field has been narrowed to either COM1 or COM2!
      You are getting some sort of response from both "virtual" serial ports - that is different than "no device connected" - you have "wires to something" on those two ports and the OS is saying that it can talk to what it thinks are 2 devices. This good!

      So how does your app work when you talk to COM1?

      I do not know (if either) of these COM devices is the serial device that you want to talk to. But is likely that software COM1 and COM2 are aliases to the same physical serial port. You have to experiment with this!

      If you have only one physical DB-9 connector on the motherboard, that is very likely to be true.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found