| 1 | package Lab::Moose::Connection::LinuxGPIB; | ||
| 2 | $Lab::Moose::Connection::LinuxGPIB::VERSION = '3.931'; | ||
| 3 | #ABSTRACT: Connection back end to the LinuxGpib library and kernel drivers | ||
| 4 | |||
| 5 | use v5.20; | ||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | use Moose; | ||
| 10 | use MooseX::Params::Validate; | ||
| 11 | use Moose::Util::TypeConstraints qw(enum); | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | use Lab::Moose::Instrument qw/timeout_param read_length_param/; | ||
| 15 | |||
| 16 | use Time::HiRes qw/gettimeofday tv_interval/; | ||
| 17 | |||
| 18 | use YAML::XS; | ||
| 19 | |||
| 20 | use namespace::autoclean; | ||
| 21 | |||
| 22 | use LinuxGpib qw/ | ||
| 23 | ibdev | ||
| 24 | ibrd | ||
| 25 | ibwrt | ||
| 26 | ibtmo | ||
| 27 | ibclr | ||
| 28 | ibconfig | ||
| 29 | /; | ||
| 30 | |||
| 31 | |||
| 32 | has pad => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => enum( [ ( 0 .. 30 ) ] ), | ||
| 35 | predicate => 'has_pad', | ||
| 36 | writer => '_pad' | ||
| 37 | ); | ||
| 38 | |||
| 39 | has gpib_address => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => enum( [ ( 0 .. 30 ) ] ), | ||
| 42 | predicate => 'has_gpib_address' | ||
| 43 | ); | ||
| 44 | |||
| 45 | has sad => ( | ||
| 46 | is => 'ro', | ||
| 47 | isa => enum( [ 0, ( 96 .. 126 ) ] ), | ||
| 48 | default => 0, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has board_index => ( | ||
| 52 | is => 'ro', | ||
| 53 | isa => 'Int', | ||
| 54 | default => 0, | ||
| 55 | ); | ||
| 56 | |||
| 57 | # Timeout set on controller | ||
| 58 | has current_timeout => ( | ||
| 59 | is => 'ro', | ||
| 60 | isa => 'Num', | ||
| 61 | init_arg => undef, | ||
| 62 | writer => '_current_timeout', | ||
| 63 | ); | ||
| 64 | |||
| 65 | has device_descriptor => ( | ||
| 66 | is => 'ro', | ||
| 67 | isa => 'Int', | ||
| 68 | init_arg => undef, | ||
| 69 | writer => '_device_descriptor' | ||
| 70 | ); | ||
| 71 | |||
| 72 | sub BUILD { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | if ( $self->has_gpib_address() ) { | ||
| 76 | $self->_pad( $self->gpib_address() ); | ||
| 77 | } | ||
| 78 | |||
| 79 | if ( not $self->has_pad() ) { | ||
| 80 | croak "no primary GPIB address provided"; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $timeout = $self->timeout; | ||
| 84 | my $ibtmo_value = _timeout_to_ibtmo($timeout); | ||
| 85 | $self->_current_timeout($timeout); | ||
| 86 | |||
| 87 | my $device_descriptor = ibdev( | ||
| 88 | $self->board_index, | ||
| 89 | $self->pad, | ||
| 90 | $self->sad, | ||
| 91 | $ibtmo_value, | ||
| 92 | 1, # Assert EOI line after transfer. | ||
| 93 | 0 # Do not use eos character. | ||
| 94 | ); | ||
| 95 | |||
| 96 | if ( $device_descriptor < 0 ) { | ||
| 97 | croak( | ||
| 98 | "ibdev failed with params:\n", | ||
| 99 | Dump( | ||
| 100 | { | ||
| 101 | board_index => $self->board_index, | ||
| 102 | pad => $self->pad, | ||
| 103 | sad => $self->sad, | ||
| 104 | timeout => $self->timeout, | ||
| 105 | timo => $ibtmo_value, | ||
| 106 | } | ||
| 107 | ) | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | |||
| 111 | $self->_device_descriptor($device_descriptor); | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub Read { | ||
| 117 | my ( $self, %args ) = validated_hash( | ||
| 118 | \@_, | ||
| 119 | timeout_param, | ||
| 120 | read_length_param, | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $timeout = $self->_timeout_arg(%args); | ||
| 124 | |||
| 125 | my $result_string = ""; | ||
| 126 | |||
| 127 | $self->_set_timeout( timeout => $timeout ); | ||
| 128 | |||
| 129 | my $ibsta_hash; | ||
| 130 | |||
| 131 | my $start_time = [gettimeofday]; | ||
| 132 | |||
| 133 | while (1) { | ||
| 134 | my $elapsed_time = tv_interval($start_time); | ||
| 135 | |||
| 136 | if ( $elapsed_time > $self->current_timeout() ) { | ||
| 137 | croak( | ||
| 138 | "timeout in Read with args:\n", | ||
| 139 | Dump( \%args ) | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | my $buffer; | ||
| 144 | |||
| 145 | my $ibsta = ibrd( | ||
| 146 | $self->device_descriptor, | ||
| 147 | $buffer, | ||
| 148 | 32768 # buffer length | ||
| 149 | ); | ||
| 150 | |||
| 151 | $self->_croak_on_err( | ||
| 152 | ibsta => $ibsta, | ||
| 153 | name => "ibrd with args:\n" . Dump( \%args ) | ||
| 154 | ); | ||
| 155 | |||
| 156 | $result_string .= $buffer; | ||
| 157 | |||
| 158 | $ibsta_hash = _ibsta_to_hash($ibsta); | ||
| 159 | if ( $ibsta_hash->{END} ) { | ||
| 160 | last; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | return $result_string; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub Write { | ||
| 169 | my ( $self, %args ) = validated_hash( | ||
| 170 | \@_, | ||
| 171 | timeout_param, | ||
| 172 | command => { isa => 'Str' }, | ||
| 173 | ); | ||
| 174 | my $command = $args{command}; | ||
| 175 | my $timeout = $self->_timeout_arg(%args); | ||
| 176 | |||
| 177 | $self->_set_timeout( timeout => $timeout ); | ||
| 178 | |||
| 179 | my $ibsta = ibwrt( | ||
| 180 | $self->device_descriptor, | ||
| 181 | $command, | ||
| 182 | length($command) | ||
| 183 | ); | ||
| 184 | |||
| 185 | $self->_croak_on_err( | ||
| 186 | ibsta => $ibsta, | ||
| 187 | name => "ibwrt with args:\n" . Dump( \%args ) | ||
| 188 | ); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub Clear { | ||
| 193 | my ( $self, %args ) = validated_hash( | ||
| 194 | \@_, | ||
| 195 | timeout_param, | ||
| 196 | ); | ||
| 197 | |||
| 198 | my $timeout = $self->_timeout_arg(%args); | ||
| 199 | |||
| 200 | $self->_set_timeout( timeout => $timeout ); | ||
| 201 | |||
| 202 | my $ibsta = ibclr( $self->device_descriptor() ); | ||
| 203 | |||
| 204 | $self->_croak_on_err( ibsta => $ibsta, name => 'ibclr' ); | ||
| 205 | |||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | sub set_termchar { | ||
| 210 | my ( $self, %args ) = validated_hash( | ||
| 211 | \@_, | ||
| 212 | timeout_param, | ||
| 213 | termchar => { isa => 'Str' }, | ||
| 214 | ); | ||
| 215 | my $timeout = $self->_timeout_arg(%args); | ||
| 216 | $self->_set_timeout( timeout => $timeout ); | ||
| 217 | my $termchar = ord( $args{termchar} ); | ||
| 218 | my $ibsta = ibconfig( $self->device_descriptor, 0xf, $termchar ); | ||
| 219 | $self->_croak_on_err( ibsta => $ibsta, name => 'ibconfig set_termchar' ); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | # With default LinuxGPIB configuration, the END bit of ibsta will be set | ||
| 224 | # when the eos char is received (see IbcEndBitIsNormal mode of ibconfig). | ||
| 225 | |||
| 226 | sub enable_read_termchar { | ||
| 227 | my ( $self, %args ) = validated_hash( | ||
| 228 | \@_, | ||
| 229 | timeout_param | ||
| 230 | ); | ||
| 231 | my $timeout = $self->_timeout_arg(%args); | ||
| 232 | $self->_set_timeout( timeout => $timeout ); | ||
| 233 | my $ibsta = ibconfig( $self->device_descriptor, 0xc, 1 ); | ||
| 234 | $self->_croak_on_err( | ||
| 235 | ibsta => $ibsta, | ||
| 236 | name => 'ibconfig enable_termchar' | ||
| 237 | ); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub _set_timeout { | ||
| 241 | my ( $self, %args ) = validated_hash( | ||
| 242 | \@_, | ||
| 243 | timeout => { isa => 'Num' }, | ||
| 244 | ); | ||
| 245 | |||
| 246 | my $timeout = $args{timeout}; | ||
| 247 | my $ibtmo_value = _timeout_to_ibtmo($timeout); | ||
| 248 | my $current_timeout = $self->current_timeout(); | ||
| 249 | my $current_ibtmo_value = _timeout_to_ibtmo($current_timeout); | ||
| 250 | |||
| 251 | if ( $ibtmo_value != $current_ibtmo_value ) { | ||
| 252 | my $ibsta = ibtmo( $self->device_descriptor, $ibtmo_value ); | ||
| 253 | $self->_croak_on_err( ibsta => $ibsta, name => 'ibtmo' ); | ||
| 254 | $self->_current_timeout($timeout); | ||
| 255 | } | ||
| 256 | |||
| 257 | } | ||
| 258 | |||
| 259 | sub _croak_on_err { | ||
| 260 | my ( $self, %args ) = validated_hash( | ||
| 261 | \@_, | ||
| 262 | ibsta => { isa => 'Int' }, | ||
| 263 | name => { isa => 'Str', optional => 1 }, | ||
| 264 | ); | ||
| 265 | my $ibsta = $args{ibsta}; | ||
| 266 | my $name = $args{name}; | ||
| 267 | my $hash = _ibsta_to_hash($ibsta); | ||
| 268 | if ( $hash->{ERR} ) { | ||
| 269 | croak( | ||
| 270 | "LinuxGPIB error:\n$name\nibsta bits:\n", | ||
| 271 | Dump($hash) | ||
| 272 | ); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _timeout_to_ibtmo { | ||
| 277 | my $timeout = shift; | ||
| 278 | |||
| 279 | # See http://linux-gpib.sourceforge.net/doc_html/r2225.html | ||
| 280 | return | ||
| 281 | $timeout == 0 ? 0 | ||
| 282 | : $timeout <= 10e-6 ? 1 | ||
| 283 | : $timeout <= 30e-6 ? 2 | ||
| 284 | : $timeout <= 100e-6 ? 3 | ||
| 285 | : $timeout <= 300e-6 ? 4 | ||
| 286 | : $timeout <= 1e-3 ? 5 | ||
| 287 | : $timeout <= 3e-3 ? 6 | ||
| 288 | : $timeout <= 10e-3 ? 7 | ||
| 289 | : $timeout <= 30e-3 ? 8 | ||
| 290 | : $timeout <= 100e-3 ? 9 | ||
| 291 | : $timeout <= 300e-3 ? 10 | ||
| 292 | : $timeout <= 1 ? 11 | ||
| 293 | : $timeout <= 3 ? 12 | ||
| 294 | : $timeout <= 10 ? 13 | ||
| 295 | : $timeout <= 30 ? 14 | ||
| 296 | : $timeout <= 100 ? 15 | ||
| 297 | : $timeout <= 300 ? 16 | ||
| 298 | : 17; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub _ibsta_to_hash { | ||
| 302 | my $ibsta = shift; | ||
| 303 | |||
| 304 | my $hash = {}; | ||
| 305 | my @bits = qw/ | ||
| 306 | DCAS DTAS LACS TACS ATN CIC REM LOK | ||
| 307 | CMPL EVENT SPOLL RQS SRQI END TIMO ERR | ||
| 308 | /; | ||
| 309 | foreach my $i ( 0 .. $#bits ) { | ||
| 310 | my $name = $bits[$i]; | ||
| 311 | my $bit = 1 << $i; | ||
| 312 | if ( $ibsta & $bit ) { | ||
| 313 | $hash->{$name} = 1; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | return $hash; | ||
| 317 | } | ||
| 318 | |||
| 319 | with 'Lab::Moose::Connection'; | ||
| 320 | |||
| 321 | __PACKAGE__->meta->make_immutable(); | ||
| 322 | |||
| 323 | 1; | ||
| 324 | |||
| 325 | __END__ |