2 # Module to do serial handling on perl FileHandles
7 use POSIX qw(:termios_h);
9 use Scalar::Util qw(weaken);
17 # Linux-specific Baud-Rates
18 use constant B57600 => 0010001;
19 use constant B115200 => 0010002;
20 use constant B230400 => 0010003;
21 use constant B460800 => 0010004;
22 use constant CRTSCTS => 020000000000;
27 my $class = ref $pkg || $pkg;
28 my $device = shift || "/dev/ttyS0";
30 my $self = $pkg->SUPER::new($device, O_RDWR|O_NOCTTY|O_EXCL|O_NDELAY) || return;
33 $$self->{ORIGTERM} = POSIX::Termios->new();
34 my $term = $$self->{TERM} = POSIX::Termios->new();
35 $$self->{ORIGTERM}->getattr(fileno($self));
36 $term->getattr(fileno($self));
37 my ($speed) = grep {/^\d+$/} @_;
41 $baud = &{'POSIX::B' . $speed};
43 $term->setispeed($baud);
44 $term->setospeed($baud);
46 my $cflag = $term->getcflag(); my $lflag = $term->getlflag();
47 my $oflag = $term->getoflag(); my $iflag = $term->getiflag();
50 ########################################################################
51 $iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
53 $lflag &= ~(ECHO|ECHONL|ICANON|ISIG);
54 $cflag &= ~(CSIZE|PARENB|HUPCL);
55 #########################################################################
58 $cflag |= CLOCAL|CREAD;
59 $cflag |= (grep {/^cs7$/i} @_) ? CS7 : CS8;
60 if (my ($parity) = grep {/^(odd|even)$/i} $@) {
62 $cflag |= PARODD if $parity =~ /odd/i;
64 $cflag |= CRTSCTS if grep /rtscts$/, $@;
65 $term->setcflag($cflag); $term->setlflag($lflag);
66 $term->setoflag($oflag); $term->setiflag($iflag);
67 $term->setattr(fileno($self), TCSANOW);
74 $$self->{TERM}->getattr;
75 return $$self->{TERM};
81 my $attr = shift || $$self->{TERM};
82 $attr->setattr(fileno($self), &POSIX::TCSANOW);
88 $self->setattr(delete $$self->{ORIGTERM});
95 if (exists $$self->{ORIGTERM}) {