Send wind even if it hasn't changed
[dweather.git] / DWeather / lib / DWeather / Serial.pm
index 44da24f7cbe7a2fde0db1f00822deb8d852d22c7..ccf31ca7d124693b48202a1867a215d14082cdb1 100644 (file)
@@ -2,15 +2,17 @@
 # Module to do serial handling on perl FileHandles
 #
 
-use strict;
-
-package Serial;
+package DWeather::Serial;
 
 use POSIX qw(:termios_h);
 use Fcntl;
+use Scalar::Util qw(weaken);
 
-our @ISA = qw(IO::File);
 
+@ISA = qw(IO::File);
+$VERSION = 1.3;
+
+use strict;
 
 # Linux-specific Baud-Rates
 use constant B57600 => 0010001;
@@ -29,7 +31,7 @@ sub new
 
        # get my attributes
        $$self->{ORIGTERM} = POSIX::Termios->new();
-       my $term =  $$self->{TERM} = POSIX::Termios->new();
+       my $term = POSIX::Termios->new();
        $$self->{ORIGTERM}->getattr(fileno($self));
        $term->getattr(fileno($self));
        my ($speed) = grep {/^\d+$/} @_; 
@@ -63,6 +65,8 @@ sub new
        $term->setcflag($cflag); $term->setlflag($lflag);
        $term->setoflag($oflag); $term->setiflag($iflag);
        $term->setattr(fileno($self), TCSANOW);
+       $$self->{TERM} = $term;
+       
        return $self;
 }
 
@@ -77,14 +81,22 @@ sub setattr
 {
        my $self = shift;
        my $attr = shift || $$self->{TERM};
-       $attr->setattr(fileno($self), &POSIX::TCSANOW);
+       $attr->setattr(fileno($self), &POSIX::TCSANOW) if fileno($self);
 }
 
 sub close
 {
        my $self = shift;
-       $self->setattr($$self->{ORIGTERM});
+       $self->setattr(delete $$self->{ORIGTERM}) if fileno($self) && $$self->{ORIGTERM};
        $self->SUPER::close;
 }
 
+sub DESTROY
+{
+       my $self = shift;
+       if (exists $$self->{ORIGTERM}) {
+               $self->close;
+       }
+}
+
 1;