Please help on win32::serialport

Please help on win32::serialport

am 13.07.2007 18:41:05 von jis

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

#!/usr/bin/perl

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

while(1)
{
$data="";
$labeldata="";
while(!($data=~/\r/))
{
$data=$serial->input();
$labeldata=$labeldata.$data ;

}
}

closePort($DEVICE);

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

$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) = @_;
$serial->close();
}


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?

regards,
jis

Re: Please help on win32::serialport

am 13.07.2007 19:21:03 von df4or

jis wrote:

> 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

[...code...]

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

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 option';
$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
cpu
$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.

Cheers,
Ekki

Re: Please help on win32::serialport

am 13.07.2007 19:48:57 von jis

On Jul 13, 12:21 pm, "Ekki Plicht (DF4OR)" wrote:
> jis wrote:
> > 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
>
> [...code...]
>
> > 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!!!!
>
> 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 option';
> $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
> cpu
> $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.
>
> Cheers,
> Ekki

Ekki,

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