# Serial.pm # John Simpson 2005-08-01 # # Serial port control functions specific to dealing with Alinco DJ-296 # ############################################################################### # # Copyright (C) 2005 John Simpson. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, version 2, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or visit http://www.gnu.org/licenses/gpl.txt # ############################################################################### package Serial ; require 5.003 ; use strict ; use Carp ; ############################################################################### # # configuration and globals use vars qw ( $port ) ; BEGIN { use Exporter ; use vars qw ( $VERSION @ISA @EXPORT @EXPORT_OK ) ; $VERSION = 1.0 ; @ISA = qw ( Exporter ) ; @EXPORT = qw ( openport closeport sendline recvline ) ; @EXPORT_OK = qw ( ) ; $port = undef ; } ############################################################################### # # open and close the port, using a given filename sub openport($) { open ( $port , "+<" , $_[0] ) or confess "Can\'t open $_[0] for input/output: $!\n" ; } sub closeport() { if ( $port ) { close ( $port ) ; $port = undef ; } } ############################################################################### # # send a line of text to the port. line ends with CR sub sendline($) { confess "Called sendline() without openport()\n" unless ( $port ) ; my $text = shift ; chomp $text ; syswrite ( $port , "$text\r" ) ; } ############################################################################### # # receive a line of text from the port. ends with LF. strip CR and NUL. sub recvline() { confess "Called recvline() without openport()\n" unless ( $port ) ; my $line = "" ; my $c = "" ; while ( my $z = sysread ( $port , $c , 1 ) ) { next if ( $c eq "\0" ) ; next if ( $c eq "\r" ) ; $line .= $c ; last if ( "\n" eq $c ) ; } chomp $line ; return $line ; } ############################################################################### # # make sure we can run 1 ;