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 reads the # reply coming from the serial port and prints it on the standard output 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 = ; print "Received: $answer\n"; print FOUT "Received: $answer\n"; ascspell($answer); } # ascspell: displays the ASCII codes for each character in a given string 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 = ; close(FIN); open(FOUT, "+>serial_old.log"); open(PORTA, "+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 $ConfigurationFileName: $!\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 reads the # reply coming from the serial port and prints it on the standard output 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 = ; print "Received: $answer\n"; print FOUT "Received: $answer\n"; ascspell($answer); } # ascspell: displays the ASCII codes for each character in a given string sub ascspell{ @array = unpack("C*", $_[0]); print "["; foreach $ch (@array) { print "$ch "; print FOUT "$ch "; } print "]\n"; } # Initialisation $cfgfile="./ttyS0.conf"; # created by serconf.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 = ; 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;