Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Win32::SerialPort Query Current Values

by spickles (Scribe)
on Sep 21, 2009 at 20:33 UTC ( [id://796607]=perlquestion: print w/replies, xml ) Need Help??

spickles has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple program to query a serial port to list its current settings. When I run the program, it reports the following:
Current baud rate is 1200 Current data bits setting is 7 Current parity setting is none Current stop bits setting is 1 Current handshake setting is none
When I re-run the program again on a device with known different settings (such as 9600 baud) the variables are not changing - I get the same output. I figured out that this is due to the fact that the Win32::SerialPort module is querying my host machine for the serial port settings, not the device I'm connected to. Is there a way to query the connected device for its current settings using this module? Here is the code:
#!c:/perl/bin/perl use strict; use warnings; use Win32::SerialPort qw( :STAT 0.19 ); my $Configuration_File_Name = "serialport_settings.txt"; print "Enter a COM port number:\n"; chomp (my $PortNumber = <>); my $PortName = "COM" . $PortNumber; my $PortObj = new Win32::SerialPort ($PortName) || die "Can't open $Po +rtName: $^E\n"; # $quiet is optional #$PortObj->save($Configuration_File_Name) || warn "Can't save $Configu +ration_File_Name: $^E\n"; my $current_baud = $PortObj->baudrate; my $current_parity = $PortObj->parity; my $current_databits = $PortObj->databits; my $current_stopbits = $PortObj->stopbits; my $current_handshake = $PortObj->handshake; print "Current baud rate is $current_baud\n"; print "Current data bits setting is $current_databits\n"; print "Current parity setting is $current_parity\n"; print "Current stop bits setting is $current_stopbits\n"; print "Current handshake setting is $current_handshake\n"; undef $PortObj; __END__

Replies are listed 'Best First'.
Re: Win32::SerialPort Query Current Values
by GrandFather (Saint) on Sep 21, 2009 at 20:53 UTC

    The module can't do it because the machine can't do it. The host can not know the connection characteristics of the client device without 'sucking it to see'. If you know that you are getting a stream of characters from the client device (a serial connection to a GPS for example), then you can configure the host for different parameters until you get sensible data from the client. That is not a general solution to the problem however.

    Note that the number of stop bits can be set to 2 without affecting receiving characters from the client. Probably hand shake can be safely set to none, at least to get going. Parity can generally be set to none, at least for determining baud rate if data bits is set to 8 and the high bit is ignored. That really just leaves the baud rate to be determined.


    True laziness is hard work
      So if I wanted to determine the settings on a serial connection, I'd have to call the program several times with settings that I guess and save the output to a file. When I get readable output back, I know I have the right settings. Is that about right?

        Tell us more about what you are trying to achieve and we may be able to help more. Running a program several times doesn't sound like a sensible use of a computer to me. I'd be writing the code to make the guesses and test the outcomes - run one program once and let the computer do the hard work.


        True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-16 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found