| 1 | package Lab::Moose::Connection::LinuxGPIB; | ||
| 2 | $Lab::Moose::Connection::LinuxGPIB::VERSION = '3.881'; | ||
| 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__ | ||
| 326 | |||
| 327 | =pod | ||
| 328 | |||
| 329 | =encoding UTF-8 | ||
| 330 | |||
| 331 | =head1 NAME | ||
| 332 | |||
| 333 | Lab::Moose::Connection::LinuxGPIB - Connection back end to the LinuxGpib library and kernel drivers | ||
| 334 | |||
| 335 | =head1 VERSION | ||
| 336 | |||
| 337 | version 3.881 | ||
| 338 | |||
| 339 | =head1 SYNOPSIS | ||
| 340 | |||
| 341 | use Lab::Moose | ||
| 342 | |||
| 343 | my $instrument = instrument( | ||
| 344 | type => 'random_instrument', | ||
| 345 | connection_type => 'LinuxGPIB', | ||
| 346 | # use primary address '1' and no secondary addressing. | ||
| 347 | connection_options => {pad => 1, timeout => 3}, | ||
| 348 | ); | ||
| 349 | |||
| 350 | =head1 DESCRIPTION | ||
| 351 | |||
| 352 | This module provides a connection interface to | ||
| 353 | L<Linux-GPIB|http://linux-gpib.sourceforge.net/>. See | ||
| 354 | L<Lab::Measurement::Backends> for more information on Linux-GPIB and its Perl | ||
| 355 | backend. | ||
| 356 | |||
| 357 | =head1 METHODS | ||
| 358 | |||
| 359 | =head2 new | ||
| 360 | |||
| 361 | The constructor takes the following attributes. The only required attribute is | ||
| 362 | B<pad>. | ||
| 363 | |||
| 364 | =head3 pad (or gpib_address for backwards compatibility with | ||
| 365 | L<Lab::Connection::LinuxGPIB>) | ||
| 366 | |||
| 367 | Primary address of the device. Required. | ||
| 368 | |||
| 369 | =head3 sad | ||
| 370 | |||
| 371 | Secondary address of the device. Default is 0 (Do not use secondary | ||
| 372 | addressing). Valid values are ( 96 .. 126 ). | ||
| 373 | |||
| 374 | =head3 board_index | ||
| 375 | |||
| 376 | Board index as provided in your F</etc/gpib.conf>. Default is 0. | ||
| 377 | |||
| 378 | =head3 timeout | ||
| 379 | |||
| 380 | Connection timeout in seconds. Default is 1. LinuxGPIB provides the following | ||
| 381 | timeout values. The value given to the constructor will be rounded upwards to a | ||
| 382 | valid timeout. | ||
| 383 | |||
| 384 | =over | ||
| 385 | |||
| 386 | =item | ||
| 387 | |||
| 388 | 0 (never timeout) | ||
| 389 | |||
| 390 | =item | ||
| 391 | |||
| 392 | 10e-6 | ||
| 393 | |||
| 394 | =item | ||
| 395 | |||
| 396 | 30e-6 | ||
| 397 | |||
| 398 | =item | ||
| 399 | |||
| 400 | 100e-6 | ||
| 401 | |||
| 402 | =item | ||
| 403 | |||
| 404 | 300e-6 | ||
| 405 | |||
| 406 | =item | ||
| 407 | |||
| 408 | 1e-3 | ||
| 409 | |||
| 410 | =item | ||
| 411 | |||
| 412 | 3e-3 | ||
| 413 | |||
| 414 | =item | ||
| 415 | |||
| 416 | 10e-3 | ||
| 417 | |||
| 418 | =item | ||
| 419 | |||
| 420 | 30e-3 | ||
| 421 | |||
| 422 | =item | ||
| 423 | |||
| 424 | 100e-3 | ||
| 425 | |||
| 426 | =item | ||
| 427 | |||
| 428 | 300e-3 | ||
| 429 | |||
| 430 | =item | ||
| 431 | |||
| 432 | 1 | ||
| 433 | |||
| 434 | =item | ||
| 435 | |||
| 436 | 3 | ||
| 437 | |||
| 438 | =item | ||
| 439 | |||
| 440 | 10 | ||
| 441 | |||
| 442 | =item | ||
| 443 | |||
| 444 | 30 | ||
| 445 | |||
| 446 | =item | ||
| 447 | |||
| 448 | 100 | ||
| 449 | |||
| 450 | =item | ||
| 451 | |||
| 452 | 300 | ||
| 453 | |||
| 454 | =item | ||
| 455 | |||
| 456 | 1000 | ||
| 457 | |||
| 458 | =back | ||
| 459 | |||
| 460 | =head1 METHODS | ||
| 461 | |||
| 462 | All methods croak if they take longer than the timeout. | ||
| 463 | |||
| 464 | The 'timeout' argument is always optional. If given, this overrides the | ||
| 465 | connection's timeout attribute. | ||
| 466 | |||
| 467 | =head2 Read | ||
| 468 | |||
| 469 | my $data = $connection->Read(); | ||
| 470 | |||
| 471 | Call ibread on the connection. Read an arbitrary amount of data until the 'END' | ||
| 472 | bit is set in C<ibsta>. Croak on read errors. | ||
| 473 | |||
| 474 | The read may requires multiple calls to ibrd. In this case, it will still croak | ||
| 475 | if the total time of operation does not exceed the timeout. | ||
| 476 | |||
| 477 | =head2 Write | ||
| 478 | |||
| 479 | $connection->Write(command => "*CLS"); | ||
| 480 | |||
| 481 | Takes one mandatory argument 'command'. Write this string to the connection. | ||
| 482 | Croak on write error. | ||
| 483 | |||
| 484 | =head2 Clear | ||
| 485 | |||
| 486 | $connection->Clear(); | ||
| 487 | |||
| 488 | Call device clear (ibclr) on the connection. | ||
| 489 | |||
| 490 | =head2 set_termchar | ||
| 491 | |||
| 492 | $connection->set_termchar(termchar => "\r"); | ||
| 493 | |||
| 494 | Set the end-of-string byte | ||
| 495 | |||
| 496 | =head2 enable_read_termchar | ||
| 497 | |||
| 498 | $connection->enable_read_termchar(); | ||
| 499 | |||
| 500 | Enable termination of reads when eos character is received. | ||
| 501 | |||
| 502 | =head1 COPYRIGHT AND LICENSE | ||
| 503 | |||
| 504 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 505 | |||
| 506 | Copyright 2016 Simon Reinhardt | ||
| 507 | 2017 Andreas K. Huettel, Simon Reinhardt | ||
| 508 | 2019 Simon Reinhardt | ||
| 509 | 2020 Andreas K. Huettel | ||
| 510 | |||
| 511 | |||
| 512 | This is free software; you can redistribute it and/or modify it under | ||
| 513 | the same terms as the Perl 5 programming language system itself. | ||
| 514 | |||
| 515 | =cut |