Please help on win32::serialport

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have got a very strange looking issue. I have a perl code which
reads the serial port.
I have the following  configuration for the port


use strict;
use warnings;
use Win32::SerialPort;

sub openPort($);
sub closePort($);

my $DEVICE = "COM1";
my $data;
my $labeldata;
my $myfile="C:\perl123\scan.txt";

my $serial =openPort($DEVICE);

        $labeldata=$labeldata.$data  ;



sub openPort($)
    my ($device) = @_;
    my $serial = Win32::SerialPort->new ($device, 1);
                 die "Can't open serial port $serial: $^E\n" unless

    $serial->databits(7) || die" could not set databits";
    $serial->baudrate(9600) || die" could not set baud rate";
    $serial->parity("even")|| die" even parity";
    $serial->stopbits(1) || die" stop bits";

    $serial->handshake("none")|| die" could nt set handshake";

    return $serial;

    sub closePort($)
      my ($serial) = @_;

Now the code was not reading serial port . So I open a VB window and
wrote a code  using commport and my serial port works fine.

Now  I tried my perl code again. The perl code could read the serial
port now and it works fine after that!!!!

I compared the difference. I couldnt find a significant difference.

Anybody has any clue why this is so?


Re: Please help on win32::serialport

jis wrote:

Quoted text here. Click to load it


Quoted text here. Click to load it

Sounds to me like some initialization issue.

When I use Device::SerialPort (same as Win32:SerialPort, but for Linux) I
use the following statements for complete init of the serial device:

$ser->baudrate($baud)                   || die 'fail setting baudrate, try -b

$ser->parity("none")                    || die 'fail setting parity to none';
$ser->databits(8)                       || die 'fail setting databits to 8';
$ser->stopbits(1)                       || die 'fail setting stopbits to 1';
$ser->handshake("none")                 || die 'fail setting handshake to none';
$ser->datatype('raw')                   || die 'fail setting datatype raw';
$ser->write_settings                    || die 'could not write settings';
$ser->error_msg(1);                     # use built-in error messages
$ser->user_msg(1);                      #
$ser->read_const_time(100);             # important for nice behaviour,
otherwise hogs

$ser->read_char_time(100);              # dto.

Especially the write_settings seems to be missign in your code. AFAIUI this
routine actually sets the previously selected values.


Re: Please help on win32::serialport

Quoted text here. Click to load it


You are absaolutely correct. write_settings solved my problem. Thanks
a lot. It did save a lot of time.

Site Timeline