2 # Module to do serial handling on perl FileHandles
7 package DWeather::Serial;
9 use POSIX qw(:termios_h);
13 use base qw(AnyEvent::Handle);
16 # Linux-specific Baud-Rates (for reference really)
17 use constant B57600 => 0010001;
18 use constant B115200 => 0010002;
19 use constant B230400 => 0010003;
20 use constant B460800 => 0010004;
21 use constant CRTSCTS => 020000000000;
24 # my $h = DWeather::Serial->new("/dev/ttyXXX", 19200 [,cs7] [,odd] [,rtscts]);
26 # all parameters are optional
28 # you are expected to add AE callbacks as required, all this module
29 # does is create the AE::Handle and associates an IO::File handle with it
31 # default is /dev/ttyS0, 9600 8N1 no handshaking
33 # the tty is set to raw mode.
35 # returns a subclassed AE::Handle
40 my $class = ref $pkg || $pkg;
41 my $device = shift || "/dev/ttyS0";
43 my $fh = IO::File->new($device, O_RDWR|O_NOCTTY|O_EXCL|O_NDELAY) || return;
44 my $self = $class->new(fh => $fh);
47 $self->{ORIGTERM} = POSIX::Termios->new();
48 my $term = $self->{TERM} = POSIX::Termios->new();
49 $self->{ORIGTERM} = $self->{ORIGTERM}->getattr(fileno($fh));
50 $term->getattr(fileno($fh));
51 my ($speed) = grep {/^\d+$/} @_;
56 $baud = &{'POSIX::B' . $speed};
58 $term->setispeed($baud);
59 $term->setospeed($baud);
61 my $cflag = $term->getcflag(); my $lflag = $term->getlflag();
62 my $oflag = $term->getoflag(); my $iflag = $term->getiflag();
65 ########################################################################
66 $iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
68 $lflag &= ~(ECHO|ECHONL|ICANON|ISIG);
69 $cflag &= ~(CSIZE|PARENB|HUPCL);
70 #########################################################################
73 $cflag |= CLOCAL|CREAD;
74 $cflag |= (grep {/^cs7$/i} @_) ? CS7 : CS8;
75 if (my ($parity) = grep {/^(odd|even)$/i} $@) {
77 $cflag |= PARODD if $parity =~ /odd/i;
79 $cflag |= CRTSCTS if grep /rtscts$/, $@;
80 $term->setcflag($cflag); $term->setlflag($lflag);
81 $term->setoflag($oflag); $term->setiflag($iflag);
82 $term->setattr(fileno($fh), TCSANOW);
83 $self->{device} = $device;
84 $self->{speed} = $speed;
91 $self->{TERM}->getattr(fileno($self->fh));
98 my $attr = shift || $self->{TERM};
99 $attr->setattr(fileno($self->fh), &POSIX::TCSANOW);
105 $self->setattr($self->{ORIGTERM});