File Coverage

blib/lib/Device/WS2000/IO.pm
Criterion Covered Total %
statement 24 96 25.0
branch 2 38 5.2
condition 0 6 0.0
subroutine 8 14 57.1
pod 0 4 0.0
total 34 158 21.5


line stmt bran cond sub pod time code
1             # CVS: $Id: IO.pm,v 1.5 2002/04/20 07:29:23 michael Exp $
2              
3             package Device::WS2000::IO;
4              
5             require 5.005_62;
6 1     1   666 use strict;
  1         2  
  1         28  
7 1     1   4 use warnings;
  1         2  
  1         22  
8 1     1   4 use Carp;
  1         1  
  1         84  
9              
10             require Exporter;
11             require DynaLoader;
12 1     1   545117 use AutoLoader;
  1         1403  
  1         6  
13              
14             our @ISA = qw(Exporter DynaLoader);
15              
16             our %EXPORT_TAGS = ( 'all' => [ qw(open_ws close_ws send_ws read_ws _called)
17             ] );
18              
19             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
20              
21             our @EXPORT = qw(
22             );
23              
24             our $VERSION = '0.01';
25              
26             bootstrap Device::WS2000::IO $VERSION;
27              
28             my $DEBUG = 0;
29              
30             # Preloaded methods go here.
31              
32             sub _called(@) {
33 1 50   1   6 return unless $DEBUG > 0;
34 0         0 my $args = join(',',@_);
35 0         0 printf ("%s(%s)\n", (caller(1))[3],$args);
36             }
37              
38             sub AUTOLOAD {
39             # This AUTOLOAD is used to 'autoload' constants from the constant()
40             # XS function. If a constant is not found then control is passed
41             # to the AUTOLOAD in AutoLoader.
42 0     0   0 _called(@_);
43              
44 0         0 my $constname;
45 0         0 our $AUTOLOAD;
46              
47 0         0 ($constname = $AUTOLOAD) =~ s/.*:://;
48 0 0       0 croak "& not defined" if $constname eq 'constant';
49 0 0       0 my $val = constant($constname, @_ ? $_[0] : 0);
50 0 0       0 if ($! != 0) {
51 1 0 0 1   936 if ($! =~ /Invalid/ || $!{EINVAL}) {
  1         1142  
  1         54  
  0         0  
52 0         0 $AutoLoader::AUTOLOAD = $AUTOLOAD;
53 0         0 goto &AutoLoader::AUTOLOAD;
54             }
55             else {
56 0         0 croak "Your vendor has not defined ws2000 macro $constname";
57             }
58             }
59             {
60 1     1   5 no strict 'refs';
  1         2  
  1         746  
  0         0  
61             # Fixed between 5.005_53 and 5.005_61
62 0 0       0 if ($] >= 5.00561) {
63 0     0   0 *$AUTOLOAD = sub () { $val };
  0         0  
64             }
65             else {
66 0     0   0 *$AUTOLOAD = sub { $val };
  0         0  
67             }
68             }
69 0         0 goto &$AUTOLOAD;
70             }
71              
72             my $fdescr;
73             our @buffer;
74              
75             sub open_ws {
76 1     1 0 13 _called(@_);
77 1         2 my ($port) = @_;
78 1         77 $fdescr = open_port($port);
79 1 50       6 if ($fdescr != -1) {
80 0         0 my ($buf,$nread) = ("",0);
81 0         0 open(TTY, "<&=$fdescr");
82             # WS2000 needs DTR change from Low to High
83 0         0 clr_dtr($fdescr);
84             # sleep(1);
85 0         0 set_dtr($fdescr);
86             # WS2000 reponses with a ETX if ready
87 0         0 sleep(1);
88 0         0 $nread = sysread (TTY,$buf,1);
89 0 0       0 print "Got $nread chars\n" if $DEBUG;
90 0 0 0     0 if ($nread == 1 and ord($buf) == 3) {
91 0         0 return 1;
92             }
93             else {
94 0         0 close (TTY);
95 0         0 close_port($fdescr);
96             }
97             }
98 1         3 return 0;
99             }
100              
101             # send_ws wrapper for the C-funktion
102             sub send_ws {
103 0     0 0   _called(@_);
104 0           my ($cmd,$par) = @_;
105 0           send_command($fdescr,$cmd,$par);
106             }
107              
108              
109             sub read_ws {
110 0     0 0   _called(@_);
111 0           my ($len,$nread,$buffer,$buf,$length);
112 0           $len = 255;
113 0           $buffer=$buf="";
114 0           $length=0;
115 0           $nread = sysread (TTY,$buf,$len);
116 0           while ($nread) {
117 0           $length += $nread;
118 0           $buffer.=$buf;
119 0           $nread = sysread(TTY,$buf,$len);
120             }
121              
122 0           $buffer.=$buf;
123 0 0         print join(" ",unpack("C$length",$buffer)),"\n" if $DEBUG;
124 0           my $tmp;
125 0           $tmp = substr($buffer,0,1);
126 0 0         return undef unless ord($tmp) == 2;
127 0           $tmp = substr($buffer,length($buffer)-1);
128 0 0         return undef unless ord($tmp) == 3;
129              
130 0           $buffer = substr($buffer,1,length($buffer)-2);
131              
132 0 0         print join(" ",unpack("C$length",$buffer)),"\n" if $DEBUG;
133 0           $buffer=~s/\x05\x12/\x02/g;
134 0 0         print join(" ",unpack("C$length",$buffer)),"\n" if $DEBUG;
135 0           $buffer=~s/\x05\x13/\x03/g;
136 0 0         print join(" ",unpack("C$length",$buffer)),"\n" if $DEBUG;
137 0           $buffer=~s/\x05\x15/\x05/g;
138 0 0         print join(" ",unpack("C$length",$buffer)),"\n" if $DEBUG;
139              
140 0           $len = ord(substr($buffer,0,1));
141 0           $buffer = substr($buffer,1,length($buffer)-2);
142              
143 0 0         unless (length($buffer) == $len) {
144 0           print STDERR "incorrect length ",length($buffer),"should be $len\n";
145 0           return undef;
146             }
147 0           @buffer = unpack("C$len",$buffer);
148 0 0         print join(" ",@buffer),"\n" if $DEBUG;
149 0 0         print "END read_ws\n" if $DEBUG;
150 0           return (@buffer);
151             }
152              
153             sub close_ws {
154 0     0 0   _called(@_);
155 0           clr_dtr($fdescr);
156 0           close (TTY);
157 0           close_port($fdescr);
158             }
159              
160              
161             # Autoload methods go after =cut, and are processed by the autosplit program.
162              
163             1;
164             __END__