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

serial port communication

April 21, 2010 03:32
serial port communication
Message ID:
 I am trying to figure out how to use the serial port in perl. I am
using windows with ActivePerl. This is what I have so far:
$o_rs232Port = new Win32::SerialPort($ARGV[1]) || die "Can't open
$ARGV[1]: $!";
$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/
$o_rs232Port->buffers(4096, 4096);
$o_rs232Port->write_settings || undef $o_rs232Port;
print "Can't change Device_Control_Block: $^E\n" unless

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

$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,

($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;

Status before write:4, , ,
Status after write:4, , ,
read unsuccessful

It seems it writes fine but it won't read and I am clueless why.
Thanks for any help,
Amish Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About