Skip to content
Browse files

Condition the line, and add optional I/O dumping.

  • Loading branch information...
1 parent fb5180a commit 5a9bdb86ad98b1007b6af81eb6897f271cfab79e @rcaputo committed Aug 5, 2012
Showing with 67 additions and 6 deletions.
  1. +67 −6 lib/Device/Firmdata/Role/IO/Serial.pm
View
73 lib/Device/Firmdata/Role/IO/Serial.pm
@@ -1,6 +1,10 @@
package Device::Firmdata::Role::IO::Serial;
use Moose::Role;
+use POSIX;
+use Time::HiRes qw(time);
+
+use constant DEBUG => 0;
with 'Device::Firmdata::Role::IO';
@@ -14,19 +18,64 @@ sub build_fh {
my $fh;
die "Could not open $path for read/write" unless open($fh, '+>', $path);
-
+ binmode($fh);
+
+ my $tio = POSIX::Termios->new();
+ $tio->getattr(fileno $fh);
+
+ $tio->setlflag(0);
+
+ my $cflag = $tio->getcflag();
+ $cflag = (
+ CLOCAL | # ignore modem status lines
+ CREAD | # enable receiver
+ HUPCL # hang up on last close
+ );
+ $cflag |= CS8; # 8 bits
+ $cflag &= ~PARENB; # turn off parity enable
+ $cflag &= ~CSTOPB; # turn off 2 stop bits (enable 1 stop)
+ $tio->setcflag($cflag);
+
+ $tio->setiflag(
+ IGNBRK | # ignore break
+ IGNPAR | # ignore parity errors
+ INPCK # enable parity checking
+ );
+
+ $tio->setoflag(0);
+
+ # Non-blocking mode. Return whatever is there.
+ $tio->setcc(VMIN, 0);
+ $tio->setcc(VTIME, 0);
+
+ $tio->setispeed(57600);
+ $tio->setospeed(57600);
+
+ # Apply Termios to the device now.
+ $tio->setattr(fileno($fh), TCSANOW);
+
return $fh;
}
sub read {
my ($self, $wantBytes) = @_;
my $bytesLeft = $wantBytes;
my $readBytes = 0;
- my $outputBuf;
-
+ my $outputBuf;
+
+ my $fh = $self->fh();
+ my $tio = POSIX::Termios->new();
+ $tio->getattr(fileno $fh);
+
while($bytesLeft > 0) {
my $readBuf;
- my $bytesRead = sysread($self->fh, $readBuf, $bytesLeft);
+
+ # Blocking until there are enough bytes.
+ # Remove this for a non-blocking library.
+ $tio->setcc(VMIN, ($bytesLeft > 255) ? 255 : $bytesLeft);
+ $tio->setattr(fileno($fh), TCSANOW);
+
+ my $bytesRead = sysread($fh, $readBuf, $bytesLeft);
if ($bytesRead == -1) {
die "Could not read from fh: $!";
@@ -40,15 +89,27 @@ sub read {
if ($bytesLeft < 0) {
die "read too much data";
}
-
+
+ if (DEBUG) {
+ my $output_ascii = $outputBuf;
+ $output_ascii =~ s/([^ -~])/sprintf '\\x%02x', ord $1/eg;
+ warn sprintf "%.3f <-- %s\n", time(), $output_ascii;
+ }
+
return $outputBuf;
}
sub write {
my ($self, $content) = @_;
my $length = length($content);
my $bytesLeft = length($content);
-
+
+ if (DEBUG) {
+ my $output_ascii = $content;
+ $output_ascii =~ s/([^ -~])/sprintf '\\x%02x', ord $1/eg;
+ warn sprintf "%.3f --> %s\n", time(), $output_ascii;
+ }
+
while($bytesLeft > 0) {
my $bytesSent = syswrite($self->fh, $content, $bytesLeft, $length - $bytesLeft);

0 comments on commit 5a9bdb8

Please sign in to comment.
Something went wrong with that request. Please try again.