lib/Quancom.pm
changeset 17 ecc10b50b7a6
parent 15 2d41fac09084
child 18 a6bc8818d069
--- a/lib/Quancom.pm	Tue Jan 06 09:31:17 2009 +0100
+++ b/lib/Quancom.pm	Tue Jan 06 10:56:28 2009 +0100
@@ -29,6 +29,7 @@
 our $VERSION = 0.1;
 
 my $DEFAULT_PORT = 1001;
+my $STX          = "\x02";
 
 sub new {
     my $class = ref $_[0] ? ref shift : shift;
@@ -64,11 +65,22 @@
     my $cmd  = shift;
 
     $self->_tx($cmd);
-    $self->_rx($cmd);
+    $self->_rx;
 
     return $self->{last_result};
 }
 
+sub reset {
+    my $self = shift;
+    $self->cmd("SL 0007 00.00.00.01");
+}
+
+sub full_reset {
+    my $self = shift;
+    $self->reset->ok or return $self->{last_result};
+    $self->cmd("WB 0007 00");
+}
+
 sub TIESCALAR {
     my $class = shift;
     my ($ip)  = @_;
@@ -90,22 +102,25 @@
     my $self = shift;
     my $cmd  = shift;
 
+    $cmd =~ s/[^A-Z\d]//g;
+
     $self->{job} = ++$self->{job} % 255;    # cap the job id on 255;
-    $cmd = "\x02" . sprintf("%02x", $self->{job}) . $cmd;   # add STX and job id
-    $cmd .= sprintf("%02x", unpack("%8C*", $cmd));          # add checksum
+    $cmd = $STX . sprintf("%02x", $self->{job}) . $cmd;    # add STX and job id
+    $cmd .= sprintf("%02x", unpack("%8C*", $cmd));         # add checksum
 
-    warn "sending $cmd\n";
+    $cmd =~ /^.(..)(......)(.*)(..)/;
+    warn "sending $1 $2 $3 ($4)\n";
     $self->{socket}->print($cmd . "\r");
 }
 
 sub _rx {
     my $self = shift;
 
-    local $/ = "\r";    # CR is the delimiter
+    local $/ = "\r";                                       # CR is the delimiter
 
     local $_ = $self->{socket}->getline;
-    $self->{last_result} = new Quancom::Result($_);
-
+    # chomp; warn "<<$_>>\n";
+    return $self->{last_result} = new Quancom::Result($_);
 }
 
 1;
@@ -147,6 +162,18 @@
 all bits on the first relais. Some other (untested) string for setting
 just the lowest bit on the first relais should be "WB010001".
 
+=item B<reset>( )
+
+This resets the device by setting the reset control flag.
+B<Note:> It doesn't reset timeouts etc. To reset these, use
+L<full_reset()>.
+
+=item B<full_reset>( )
+
+This clears the outputs AND resets timeouts by writing zero 
+to all control bits.
+
+
 =item B<last_result>( )
 
 This returns an object containing the last result.