ntruchsess 3d20b4a6d4 incorporate perl-firmata (Device::Firmata)
git-svn-id: https://svn.fhem.de/fhem/trunk@2596 2b470e98-0d58-463d-a4d8-8e2adae1ed80
2013-01-29 21:03:38 +00:00

86 lines
1.7 KiB
Perl

package Device::Firmata::IO;
=head1 NAME
Device::Firmata::IO - implement the low level serial IO
=cut
use strict;
use warnings;
use vars qw/ $SERIAL_CLASS /;
use Device::Firmata::Base
ISA => 'Device::Firmata::Base',
FIRMATA_ATTRIBS => {
handle => undef,
baudrate => 57600,
};
$SERIAL_CLASS = $^O eq 'MSWin32' ? 'Win32::Serialport'
: 'Device::SerialPort';
eval "require $SERIAL_CLASS";
=head2 open
=cut
sub open {
# --------------------------------------------------
my ( $pkg, $serial_port, $opts ) = @_;
my $self = ref $pkg ? $pkg : $pkg->new($opts);
my $serial_obj = $SERIAL_CLASS->new( $serial_port, 1, 0 ) or return;
$self->attach($serial_obj,$opts);
$self->{handle}->baudrate($self->{baudrate});
$self->{handle}->databits(8);
$self->{handle}->stopbits(1);
return $self;
}
sub attach {
my ( $pkg, $serial_obj, $opts ) = @_;
my $self = ref $pkg ? $pkg : $pkg->new($opts);
$self->{handle} = $serial_obj;
return $self;
}
=head2 data_write
Dump a bunch of data into the comm port
=cut
sub data_write {
# --------------------------------------------------
my ( $self, $buf ) = @_;
$Device::Firmata::DEBUG and print ">".join(",",map{sprintf"%02x",ord$_}split//,$buf)."\n";
return $self->{handle}->write( $buf );
}
=head2 data_read
We fetch up to $bytes from the comm port
This function is non-blocking
=cut
sub data_read {
# --------------------------------------------------
my ( $self, $bytes ) = @_;
my ( $count, $string ) = $self->{handle}->read($bytes);
if ( $Device::Firmata::DEBUG and $string ) {
print "<".join(",",map{sprintf"%02x",ord$_}split//,$string)."\n";
}
return $string;
}
1;