Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # Light Strike reader
- #
- # Rich Whiffen <http://rich.whiffen.org> AND
- # portions are/where Copyright (C) 2007 Kenneth L. Root. <http://the-b.org>
- # I stole almost all of this from http://the-b.org/Usb-uirt-config.pl
- #
- # And since Kenneth's code was, so to is my hacks on top of it:
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- use strict;
- use warnings;
- use Device::SerialPort;
- use Getopt::Long;
- use Pod::Usage;
- use constant TIMEOUT_DEFAULT => 100;
- use constant TRANSMITTING => 0x20;
- use constant CMDOK => 0x21;
- use constant CSERROR => 0x80;
- use constant TOERROR => 0x81;
- use constant CMDERROR => 0x82;
- my %config = (
- 'port' => '/dev/ttyUSB0',
- 'slot' => 0
- );
- my $port;
- my $slots = -1;
- sub openPort {
- my($portName) = @_;
- my $port = new Device::SerialPort($portName)
- || die "Can't open $portName: $!\n";
- $port->read_char_time(0);
- $port->read_const_time(1000);
- return $port;
- }
- sub setUIRMode {
- sendCommand(0x20);
- my @res = readCommand(1);
- die "Cannot set UIR mode" if ($res[0] != CMDOK);
- }
- sub printCommand {
- my ($prefix, @cmd) = @_;
- print "$prefix: ";
- map { printf "0x%02x ", $_ } @cmd;
- print "\n";
- }
- sub readCommand {
- my($numBytes) = @_;
- my $timeout = TIMEOUT_DEFAULT;
- my $buffer;
- my $chars = 0;
- while ($timeout > 0) {
- my ($count, $saw) = $port->read(255);
- if ($count > 0) {
- $chars += $count;
- $buffer .= $saw;
- if ($chars >= $numBytes) {
- my @cmd = map { ord } unpack("(Z)*", $buffer);
- printCommand("recv", @cmd) if $config{'debug'};
- return @cmd;
- }
- } else {
- $timeout--;
- }
- }
- if ($timeout == 0) {
- die "Couldn't communicate with USB-UIRT device after ". TIMEOUT_DEFAULT ." seconds.\n";
- }
- }
- print "Opening port: ". $config{'port'} ."\n";
- $port = openPort($config{'port'});
- print "Opened port ". $config{'port'} ."\n";
- #if (exists $config{'get-code'}) {
- # setUIRMode;
- print "Shoot the gun at the IR Sensor\n";
- my @res = readCommand(60);
- printCommand("IR Code", @res);
- exit(0);
- #}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement