Hi Monks, I am trying to use the Device::SerialPort() module from the CPAN perl library & I seem to be having some issue reading the values back for the command I send to serial port. I send some command to the Serial Port & when trying to read the value back, I see some garbage being readout. How can i really tell whether my command did go through OK. As a matter of fact, I know that the Serial Port is being opened by the perl scripts because at the same time when I tried opening up the same port using other application I cannot. Below is the piece of code that I have.

my $PortName = '/dev/ttyS16' ; my $PortObj = new Device::SerialPort($PortName) || die "Can't open $Po +rtName: $!\n"; $PortObj->databits(8); $PortObj->baudrate(19200); $PortObj->parity("none"); $PortObj->stopbits(1); #$PortObj->flowcontrol("none"); $PortObj->write('asic_rst 0'); my ($r1, $saw1) = $PortObj->read(255) ; $PortObj->write("asic_rst 1"); my ($r2, $saw2) = $PortObj->read(255) ; $PortObj->close || die "failed to close"; undef $PortObj;

The values of the return variables is as follows.

r1 = 8 $saw1 = a®k}r.\x17‚ r2 = 8 $saw1 = a®k}r.\x17Š

How do i know what is being read in $saw1/$saw2 is correct ? I know my response to the serial port is not what I am seeing here.Also note that instead of read i tried input() & lookfor() commands as well. Any input is appreciated.

Ok, so now I based on the recommendations from lot of monks, I have switched from Device::SerialPort (cygwin) to Win32::SerialPort. Below is the code. And also the output response. Basically I am still seeing the same scribbled data when trying to read. I am at all lost here.

#!/usr/bin/perl -w use strict; use warnings ; use Win32::SerialPort qw( :STAT ) ; # } my $PortName = 'COM17' ; my $serial_port = new Win32::SerialPort($PortName) || die "Can't open + $PortName: $!\n"; $serial_port->databits( 8 ); $serial_port->baudrate(19200); $serial_port->parity("none"); $serial_port->stopbits( 1 ); $serial_port->handshake("rts") ; my $response = undef ; my $complete = 0 ; my $command = "12345" ; $serial_port->write("$command\r") ; sleep 1 ; my ( $blocking_flags, $in_bytes, $out_bytes, $latch_error_flags ) = $s +erial_port->status() ; print("1: $blocking_flags\n") ; print("2: $in_bytes\n") ; print("3: $out_bytes\n") ; print("4: $latch_error_flags\n") ; $serial_port->lookclear; #clear the buffer sleep (2) ; if( $in_bytes > 0 ) { for( my $i = 0 ; $i < $in_bytes ; $i++ ) { # Get a byte at a time to process. $response .= $serial_port->read( 1 ) ; if( $response =~ /\r/ ) { $complete = 1 ; } } } print ("Response is: $response\n") ; $serial_port->close || die "failed to close"; undef $serial_port; # frees memory back
1: 0 2: 5 3: 0 4: 0 Response is: 1ªó5&#9500;

In reply to Using Device::SerialPort by hardy004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.