Dear Monks,

I need to communicate with an appliance, the consolle port of which is linked to my RedHat 7.2 system's serial port.

I started writing a Perl programs that uses the basic filehandle syntax

open(PORTA, "+</dev/ttyS0"); ... print PORTA $stringout; ... $answer = <PORTA>;

I tested it connecting my serial port to a Windows workstation running Hyperterminal, and it worked out ok!

Sadly, when I tried it on the needed connection, I got absolutely no response from the appliance.

I blamed it on the different serial port configuration requested by the appliance (9600 bps, 8-bit, 1-stop, no parity, ?flow control? = none), and so I turned to the more sophisticated "Device::SerialPort" module, which should provide the right tools to set the port parameters.

I tested it again using Hyperterminal but this time, though I managed to get my strings sent, the replies I sent from the Windows hyperterminal never reached my Linux PC...naturally, it didn't work with the appliance either.

I must certainly be going wrong somewhere, but I have no clue at all.

Could you please help?

I attach hereby the two versions of the program (both of which read the strings that must be sent from an input text file) and the script I used to set the port parameters.

Thank you very much in advance.

Best regards
Bruno

serial_old.pl - The first version of the program ================================================ #!/usr/bin/perl -w # Routines # comunicate: adds a customary terminator to the string, which is then + sent to # the serial port, the standard output and the logfile; finally it rea +ds the # reply coming from the serial port and prints it on the standard outp +ut and # the logfile sub comunicate{ $terminator = "\n"; $stringout = $_[0].$terminator; print PORTA $stringout; print "Sent: $stringout\n"; print FOUT "Sent: $stringout\n"; ascspell($stringout); $answer = <PORTA>; print "Received: $answer\n"; print FOUT "Received: $answer\n"; ascspell($answer); } # ascspell: displays the ASCII codes for each character in a given str +ing sub ascspell{ @array = unpack("C*", $_[0]); print "["; foreach $ch (@array) { print "$ch "; print FOUT "$ch "; } print "]\n"; } # Main open(FIN,$ARGV[0]) or die ("error opening input file because: $!"); @inner = <FIN>; close(FIN); open(FOUT, "+>serial_old.log"); open(PORTA, "+</dev/ttyS0"); foreach $riga (@inner) { chomp($riga); comunicate($riga); } close(PORTA); close(FOUT); ============ serconf.pl - The script that sets the port parameters and writes them +to a config file ====================================================================== +================ #!/usr/bin/perl -w use Device::SerialPort; # Initialisation $PortName="/dev/ttyS0"; $ConfigurationFileName="./ttyS0.conf"; $quiet=1; $lockfile="./lock"; $PortObj = new Device::SerialPort ($PortName, $quiet, $lockfile) || die "Can't open $PortName: $!\n"; $PortObj->user_msg(ON); $PortObj->databits(8); $PortObj->baudrate(9600); $PortObj->parity("none"); $PortObj->stopbits(1); $PortObj->handshake("rts"); print "Given configuration\n\n"; print "Port Name: $PortName\n"; $baud = $PortObj->baudrate; $parity = $PortObj->parity; $data = $PortObj->databits; $stop = $PortObj->stopbits; $hshake = $PortObj->handshake; print "B = $baud, D = $data, S = $stop, P = $parity, H = $hshake\n"; $PortObj->write_settings; $PortObj->save($ConfigurationFileName) || warn "Can't save $Configurat +ionFileName: $!\n"; ====================================================================== +=================== serial.pl - The Device::SerialPort version ========================================== #!/usr/bin/perl -w use Device::SerialPort; # Routines # comunicate: adds a customary terminator to the string, which is then + sent to # the serial port, the standard output and the logfile; finally it rea +ds the # reply coming from the serial port and prints it on the standard outp +ut and # the logfile sub comunicate{ $terminator = "\n"; $stringout = $_[0].$terminator; print PORTA $stringout; print "Sent: $stringout\n"; print FOUT "Sent: $stringout\n"; ascspell($stringout); $answer = <PORTA>; print "Received: $answer\n"; print FOUT "Received: $answer\n"; ascspell($answer); } # ascspell: displays the ASCII codes for each character in a given str +ing sub ascspell{ @array = unpack("C*", $_[0]); print "["; foreach $ch (@array) { print "$ch "; print FOUT "$ch "; } print "]\n"; } # Initialisation $cfgfile="./ttyS0.conf"; # created by serco +nf.pl $PortObj= tie(*PORTA,'Device::SerialPort', $cfgfile) || die "Can't start $cfgfile\n"; my $name= $PortObj->alias; my $baud = $PortObj->baudrate; my $parity = $PortObj->parity; my $data = $PortObj->databits; my $stop = $PortObj->stopbits; my $hshake = $PortObj->handshake; print "Port: $name\nB = $baud, D = $data, S = $stop, P = $parity, H = +$hshake\n"; # Main open(FIN,$ARGV[0]) or die ("error opening input file because: $!"); @inner = <FIN>; close FIN; open(FOUT, "+>serial.log"); foreach $riga (@inner) { chomp($riga); comunicate($riga); } close PORTA || print "port close failed\n"; undef $PortObj; untie *PORTA; close FOUT;

Edited by Chady -- fixed formatting, added code tags.


In reply to Sending commands and viewing replies from a serial-attached appliance by bgianardo

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.