develooper Front page | perl.beginners | Postings from April 2010

rs232 in perl

From:
axr0284
Date:
April 20, 2010 06:33
Subject:
rs232 in perl
Message ID:
9326e235-61d0-460e-8e7d-3d312b2d105e@y3g2000vbg.googlegroups.com
Hi,
 I am trying to learn how to access an RS232 port through perl. Here
is what I have so far.
I downloaded the win32:serial port and win32API:comm Port from here
http://search.cpan.org/dist/Win32-SerialPort/
I am using windows with ActivePerl.

My code is as follows:
[CODE]
use strict;
use Win32::SerialPort;
$|++;                       # force auto flush of output buffer

# RS232 port object
my $o_rs232Port;

# Number of bytes read
my $i_countIn = 0;

# Data read
my $s_stringIn = "";

# Number of bytes written
my $i_countOut = 0;

# RS232 status
my $BlockingFlags;
my $InBytes = 0;
my $OutBytes;
my $LatchErrorFlags;
######################################## GET DATA FROM GENERATOR
#######################################
$o_rs232Port = new Win32::SerialPort($ARGV[1]) || die "Can't open
$ARGV[1]: $!";
$o_rs232Port->user_msg("ON");
$o_rs232Port->baudrate(115200) || die "Can't set 115200";
$o_rs232Port->parity("none") || die "Can't set parity none";
$o_rs232Port->databits(8)  || die "Can't set 8 data bits";
$o_rs232Port->stopbits(1)  || die "Can't set one stopbit";
$o_rs232Port->handshake("xoff")  || die "Can't set flow control to xon/
xoff";
$o_rs232Port->write_settings || undef $o_rs232Port;
print "Can't change Device_Control_Block: $^E\n" unless
($o_rs232Port);

($BlockingFlags, $InBytes, $OutBytes, $LatchErrorFlags) = $o_rs232Port-
>status || die "could not get port status\n";
print "Status before write:$BlockingFlags, $InBytes, $OutBytes,
$LatchErrorFlags\n";

$i_countOut = $o_rs232Port->write('N');
warn "write failed\n" unless ($i_countOut == 1);

($BlockingFlags, $InBytes, $OutBytes, $LatchErrorFlags) = $o_rs232Port-
>status || die "could not get port status\n";
print "Status after write:$BlockingFlags, $InBytes, $OutBytes,
$LatchErrorFlags\n";

($i_countIn, $s_stringIn) = $o_rs232Port->read(1);
warn "read unsuccessful\n" unless ($i_countIn == 1);

# Close port
$o_rs232Port->close || die "failed to close COM1";

# frees memory back to perl
undef $o_rs232Port;
[CODE]
$ARGV[1] is COM1 in this case

This is the result on the command prompt:
[RESULT]
Status before write:4, , ,
Status after write:4, , ,
read unsuccessful
[RESULT]

I used a serial sniffer which showed that the perl script sent the
data correctly but could not receive any data. I am at a loss as to
why.

I hope somebody can help me out.

Thanks a lot,
Amish




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About