| 1 | package Lab::Bus::VICP; | ||
| 2 | #ABSTRACT: VICP bus | ||
| 3 | $Lab::Bus::VICP::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Scalar::Util qw(weaken); | ||
| 8 | use Time::HiRes qw (usleep sleep); | ||
| 9 | use Lab::Bus; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Carp; | ||
| 12 | use IO::Socket::INET; | ||
| 13 | use IO::Select; | ||
| 14 | use Socket qw( SOCK_STREAM SHUT_RDWR MSG_OOB TCP_NODELAY ); | ||
| 15 | use Clone qw(clone); | ||
| 16 | |||
| 17 | $Data::Dumper::Useqq = 1; | ||
| 18 | |||
| 19 | our @ISA = ("Lab::Bus"); | ||
| 20 | |||
| 21 | # GPIB status bit vector : | ||
| 22 | # global variable ibsta and wait mask | ||
| 23 | use constant { | ||
| 24 | ERR => ( 1 << 15 ), # Error detected 0x8000 | ||
| 25 | TIMO => ( 1 << 14 ), # Timeout 0x4000 | ||
| 26 | EOI => ( 1 << 13 ), # EOI or EOS detected 0x2000 | ||
| 27 | SRQI => ( 1 << 12 ), # SRQ detected by CIC 0x1000 | ||
| 28 | RQS => ( 1 << 11 ), # Device needs service 0x0800 | ||
| 29 | SPOLL => ( 1 << 10 ), # Board has been serially polled 0x0400 | ||
| 30 | CMPL => ( 1 << 8 ), # I/O completed 0x0100 | ||
| 31 | REM => ( 1 << 6 ), # Remote state 0x0040 | ||
| 32 | CIC => ( 1 << 5 ), # Controller-in-Charge 0x0020 | ||
| 33 | ATN => ( 1 << 4 ), # Attention asserted 0x0010 | ||
| 34 | TACS => ( 1 << 3 ), # Talker active 0x0008 | ||
| 35 | LACS => ( 1 << 2 ), # Listener active 0x0004 | ||
| 36 | DTAS => ( 1 << 1 ), # Device trigger state 0x0002 | ||
| 37 | DCAS => ( 1 << 0 ), # Device clear state 0x0001 | ||
| 38 | }; | ||
| 39 | |||
| 40 | # GPIB error codes : | ||
| 41 | # iberr | ||
| 42 | use constant { | ||
| 43 | EDVR => 0, # System error | ||
| 44 | ECIC => 1, # Function requires GPIB board to be CIC | ||
| 45 | ENOL => 2, # Write function detected no Listeners | ||
| 46 | EADR => 3, # Interface board not addressed correctly | ||
| 47 | EARG => 4, # Invalid argument to function call | ||
| 48 | ESAC => 5, # Function requires GPIB board to be SAC | ||
| 49 | EABO => 6, # I/O operation aborted | ||
| 50 | ENEB => 7, # Non-existent interface board | ||
| 51 | EDMA => 8, # Error performing DMA | ||
| 52 | EOIP => 10, # I/O operation started before previous operation completed | ||
| 53 | ECAP => 11, # No capability for intended operation | ||
| 54 | EFSO => 12, # File system operation error | ||
| 55 | EBUS => 14, # Command error during device call | ||
| 56 | ESTB => 15, # Serial poll status byte lost | ||
| 57 | ESRQ => 16, # SRQ remains asserted | ||
| 58 | ETAB => 20, # The return buffer is full. | ||
| 59 | ELCK => 21, # Address or board is locked. | ||
| 60 | }; | ||
| 61 | |||
| 62 | # VICP header 'Operation' bits | ||
| 63 | use constant { | ||
| 64 | OPERATION_DATA => 0x80, | ||
| 65 | OPERATION_REMOTE => 0x40, | ||
| 66 | OPERATION_LOCKOUT => 0x20, | ||
| 67 | OPERATION_CLEAR => 0x10, | ||
| 68 | OPERATION_SRQ => 0x08, | ||
| 69 | OPERATION_REQSERIALPOLL => 0x04, | ||
| 70 | OPERATION_EOI => 0x01, | ||
| 71 | }; | ||
| 72 | |||
| 73 | use constant { | ||
| 74 | HEADER_VERSION1 => 0x01, # header version | ||
| 75 | SERVER_PORT_NUM => 1861, # port # for lecroy-vicp | ||
| 76 | IO_NET_HEADER_SIZE => 8, # size of network header | ||
| 77 | SMALL_DATA_BUFSIZE => 8192, # small buffer, combined header+data | ||
| 78 | }; | ||
| 79 | |||
| 80 | our %fields = ( | ||
| 81 | type => 'VICP', | ||
| 82 | brutal => 0, # brutal as default? | ||
| 83 | wait_query => 5, # sec; | ||
| 84 | read_length => 1000, # bytes | ||
| 85 | query_length => 300, # bytes | ||
| 86 | query_long_length => 10240, #bytes | ||
| 87 | |||
| 88 | remote_port => SERVER_PORT_NUM, | ||
| 89 | remote_addr => undef, | ||
| 90 | proto => 'tcp', | ||
| 91 | timeout => 10, | ||
| 92 | |||
| 93 | _state => 'NetWaitHeader', | ||
| 94 | _remote => 0, | ||
| 95 | _lockout => 0, | ||
| 96 | _iberr => 0, | ||
| 97 | _ibsta => 0, | ||
| 98 | _ibcntl => 0, | ||
| 99 | _errflag => 0, | ||
| 100 | _nextseq => 1, | ||
| 101 | _lastseq => 1, | ||
| 102 | _flushunread => 1, | ||
| 103 | _version1a => 0, | ||
| 104 | _maxblocksize => 512, | ||
| 105 | _maxcommandbuf => 256, | ||
| 106 | ); | ||
| 107 | |||
| 108 | sub new { | ||
| 109 | my $proto = shift; | ||
| 110 | my $class = ref($proto) || $proto; | ||
| 111 | my $self = $class->SUPER::new(@_) | ||
| 112 | ; # getting fields and _permitted from parent class | ||
| 113 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 114 | |||
| 115 | $self->remote_addr( $self->config('remote_addr') ) | ||
| 116 | if defined $self->config('remote_addr'); | ||
| 117 | $self->remote_port( $self->config('remote_port') ) | ||
| 118 | if defined $self->config('remote_port'); | ||
| 119 | $self->proto( $self->config('proto') ) | ||
| 120 | if defined $self->config('proto'); | ||
| 121 | $self->timeout( $self->config('timeout') ) | ||
| 122 | if defined $self->config('timeout'); | ||
| 123 | |||
| 124 | # only one VICP connection/device, so don't do 'twins' stuff | ||
| 125 | |||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub connection_new { # { | ||
| 130 | my $self = shift; | ||
| 131 | my $args = undef; | ||
| 132 | if ( ref $_[0] eq 'HASH' ) { | ||
| 133 | $args = shift; | ||
| 134 | } # try to be flexible about options as hash/hashref | ||
| 135 | else { $args = {@_} } | ||
| 136 | |||
| 137 | croak("remote_addr not specified") unless defined $args->{'remote_addr'}; | ||
| 138 | |||
| 139 | my $client = IO::Socket::INET->new( | ||
| 140 | PeerAddr => $args->{'remote_addr'} || $self->remote_addr(), | ||
| 141 | PeerPort => $args->{'remote_port'} || $self->remote_port(), | ||
| 142 | Proto => $args->{'proto'} || $self->proto(), | ||
| 143 | Timeout => $args->{'timeout'} || $self->timeout(), | ||
| 144 | Type => SOCK_STREAM, | ||
| 145 | ); | ||
| 146 | croak("Could not create socket client: $!") unless $client; | ||
| 147 | |||
| 148 | #$client->autoflush(1); | ||
| 149 | $client->sockopt(TCP_NODELAY); | ||
| 150 | |||
| 151 | sleep(5); | ||
| 152 | croak("did not connect") unless $client->connected(); | ||
| 153 | |||
| 154 | my $connection_handle = { valid => 1, type => "VICP", socket => $client }; | ||
| 155 | return $connection_handle; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub connect { | ||
| 159 | my $self = shift; | ||
| 160 | my $connection_handle = shift; | ||
| 161 | my $nch = $self->connection_new( remote_addr => $self->remote_addr() ); | ||
| 162 | $connection_handle->{socket} = $nch->{socket}; | ||
| 163 | $connection_handle->{valid} = $nch->{valid}; | ||
| 164 | $connection_handle->{type} = $nch->{type}; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub disconnect { | ||
| 168 | my $self = shift; | ||
| 169 | my $connection_handle = shift; | ||
| 170 | |||
| 171 | return unless defined($connection_handle); | ||
| 172 | $connection_handle->{socket}->shutdown(2); | ||
| 173 | } | ||
| 174 | |||
| 175 | # VICP v1a uses an 8 bit 'sequence number' to keep in sync | ||
| 176 | |||
| 177 | sub _nextSeq { | ||
| 178 | my $self = shift; | ||
| 179 | my $eoi = shift || 0; | ||
| 180 | |||
| 181 | $self->{_lastseq} = $self->{_nextseq}; | ||
| 182 | if ($eoi) { | ||
| 183 | $self->{_nextseq}++; | ||
| 184 | $self->{_nextseq} = 1 if $self->{_nextseq} > 0xFF; | ||
| 185 | } | ||
| 186 | return $self->{_lastseq}; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _lastSeq { | ||
| 190 | my $self = shift; | ||
| 191 | return $self->{_lastseq}; | ||
| 192 | } | ||
| 193 | |||
| 194 | # Lecroy VICP has a really weird bug, where commands need to be | ||
| 195 | # an even number of bytes. Pad with spaces. | ||
| 196 | |||
| 197 | sub connection_write | ||
| 198 | { # @_ = ( $connection_handle, $args = { command, wait_status } | ||
| 199 | my $self = shift; | ||
| 200 | my $connection_handle = shift; | ||
| 201 | my $args = undef; | ||
| 202 | if ( ref $_[0] eq 'HASH' ) { | ||
| 203 | $args = shift; | ||
| 204 | } # try to be flexible about options as hash/hashref | ||
| 205 | else { $args = {@_} } | ||
| 206 | |||
| 207 | my $command = $args->{'command'} || undef; | ||
| 208 | my $brutal = $args->{'brutal'} || 0; | ||
| 209 | my $eoi = $args->{'eoi'} || 1; | ||
| 210 | my $clr = $args->{'clr'} || 0; | ||
| 211 | my $poll = $args->{'poll'} || 0; | ||
| 212 | |||
| 213 | croak("no command") unless defined $command; | ||
| 214 | croak("not connected") unless $connection_handle->{socket}->connected(); | ||
| 215 | |||
| 216 | my $nb = $args->{'length'} || length($command); | ||
| 217 | croak("zero length command") unless $nb > 0; | ||
| 218 | |||
| 219 | my $cmdlen = $nb; | ||
| 220 | |||
| 221 | if ($eoi) { # make sure proper termination char, EVEN length | ||
| 222 | $command = substr( $command, 0, $nb ); | ||
| 223 | $command =~ s/(\r\n|\n\r|\r|\n)$//; | ||
| 224 | $command .= ' ' if ( length($command) & 1 ) == 0; | ||
| 225 | $command .= "\n"; | ||
| 226 | $nb = length($command); | ||
| 227 | } | ||
| 228 | |||
| 229 | if ( $self->{_flushunread} && $self->{_state} ne 'NetWaitHeader' ) { | ||
| 230 | $self->connection_read( $connection_handle, flush => 1 ); | ||
| 231 | } | ||
| 232 | |||
| 233 | $self->{_ibsta} &= (RQS); | ||
| 234 | $self->{_ibcntl} = 0; | ||
| 235 | $self->{_iberr} = 0; | ||
| 236 | |||
| 237 | # prepare and send header | ||
| 238 | my $hdr = OPERATION_DATA; | ||
| 239 | $hdr |= OPERATION_EOI if $eoi; | ||
| 240 | $hdr |= OPERATION_REMOTE if $self->{_remote}; | ||
| 241 | $hdr |= OPERATION_CLEAR if $clr; | ||
| 242 | $hdr |= OPERATION_REQSERIALPOLL if $poll; | ||
| 243 | |||
| 244 | my $hbuf = pack( | ||
| 245 | 'CCCCN', $hdr, HEADER_VERSION1, $self->_nextSeq($eoi), 0, | ||
| 246 | $nb | ||
| 247 | ); | ||
| 248 | |||
| 249 | my $sent; | ||
| 250 | if ( $nb >= SMALL_DATA_BUFSIZE ) { | ||
| 251 | $sent = $connection_handle->{socket} | ||
| 252 | ->send( $hbuf, IO_NET_HEADER_SIZE, 0 ); | ||
| 253 | if ( !defined($sent) || $sent != IO_NET_HEADER_SIZE ) { | ||
| 254 | carp("error sending header packet: $!"); | ||
| 255 | $self->{_errflag} = 1; | ||
| 256 | $self->{_ibsta} |= ERR; | ||
| 257 | return 0; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | else { # if the packet is small combine header with data | ||
| 261 | $command = $hbuf . $command; | ||
| 262 | $nb += IO_NET_HEADER_SIZE; | ||
| 263 | } | ||
| 264 | |||
| 265 | $sent = $connection_handle->{socket}->send( $command, $nb, 0 ); | ||
| 266 | if ( !defined($sent) || $sent != $nb ) { | ||
| 267 | carp("error sending header packet: $! sent $sent != $nb ?"); | ||
| 268 | $self->{_errflag} = 1; | ||
| 269 | $self->{_ibsta} |= ERR; | ||
| 270 | return 0; | ||
| 271 | } | ||
| 272 | $self->{_ibsta} = CMPL | CIC | TACS; | ||
| 273 | $self->{_ibcntl} = $cmdlen; | ||
| 274 | |||
| 275 | return 1; | ||
| 276 | } | ||
| 277 | |||
| 278 | # | ||
| 279 | # read the header portion of response | ||
| 280 | |||
| 281 | sub _readHeader { | ||
| 282 | my $self = shift; | ||
| 283 | my $connection_handle = shift; | ||
| 284 | my $args = undef; | ||
| 285 | if ( ref $_[0] eq 'HASH' ) { | ||
| 286 | $args = shift; | ||
| 287 | } # try to be flexible about options as hash/hashref | ||
| 288 | else { $args = {@_} } | ||
| 289 | |||
| 290 | my $brutal = $args->{'brutal'} || 0; | ||
| 291 | my $timeout = $args->{'timeout'} || $self->timeout(); | ||
| 292 | |||
| 293 | my $hdr = { | ||
| 294 | SIZE => 0, | ||
| 295 | SEQ => undef, | ||
| 296 | EOI => 0, | ||
| 297 | SRQ => 0, | ||
| 298 | BUF => '', | ||
| 299 | }; | ||
| 300 | |||
| 301 | my $sel = IO::Select->new( $connection_handle->{socket} ); | ||
| 302 | |||
| 303 | my (@ready) = $sel->can_read($timeout); | ||
| 304 | return undef if ( $#ready < 0 ); | ||
| 305 | |||
| 306 | my $nb = 0; | ||
| 307 | my $header = ''; | ||
| 308 | while ( $nb < 8 ) { | ||
| 309 | (@ready) = $sel->can_read($timeout); | ||
| 310 | last if $#ready < 0; | ||
| 311 | my $buf; | ||
| 312 | $connection_handle->{socket} | ||
| 313 | ->recv( $buf, IO_NET_HEADER_SIZE- $nb, 0 ); | ||
| 314 | |||
| 315 | # print STDERR "hdr buf=",Dumper($buf),"\n"; | ||
| 316 | if ( length($buf) > 0 ) { | ||
| 317 | $nb += length($buf); | ||
| 318 | $header .= $buf; | ||
| 319 | } | ||
| 320 | last if $nb == 0; | ||
| 321 | } | ||
| 322 | $sel->remove( $connection_handle->{socket} ); | ||
| 323 | |||
| 324 | if ( $nb > IO_NET_HEADER_SIZE ) { | ||
| 325 | $hdr->{BUF} = substr( $header, IO_NET_HEADER_SIZE ); | ||
| 326 | $nb = IO_NET_HEADER_SIZE; | ||
| 327 | } | ||
| 328 | |||
| 329 | if ( $nb == IO_NET_HEADER_SIZE ) { | ||
| 330 | my ( $op, $ver, $seq, $zero, $len ) = unpack( 'CCCCN', $header ); | ||
| 331 | $hdr->{SIZE} = $len; | ||
| 332 | if ( !( ( $op & OPERATION_DATA ) && ( $ver == HEADER_VERSION1 ) ) ) { | ||
| 333 | carp("Invalid header"); | ||
| 334 | $self->{_errflag} = 1; | ||
| 335 | $self->disconnect($connection_handle); | ||
| 336 | $self->connect($connection_handle); | ||
| 337 | return undef; | ||
| 338 | } | ||
| 339 | $hdr->{EOI} = ( $op & OPERATION_EOI ) == 0 ? 0 : 1; | ||
| 340 | $hdr->{SRQ} = ( $op & OPERATION_SRQ ) == 0 ? 0 : 1; | ||
| 341 | $hdr->{SEQ} = $seq; | ||
| 342 | } | ||
| 343 | else { | ||
| 344 | # error state out of sync | ||
| 345 | $self->disconnect($connection_handle); | ||
| 346 | $self->connect($connection_handle); | ||
| 347 | return undef; | ||
| 348 | } | ||
| 349 | return $hdr; | ||
| 350 | } | ||
| 351 | |||
| 352 | # dump data until next header is found | ||
| 353 | sub _dumpdata { | ||
| 354 | my $self = shift; | ||
| 355 | my $connection_handle = shift; | ||
| 356 | my $nb = shift; | ||
| 357 | |||
| 358 | carp("Unread response, dumping $nb bytes"); | ||
| 359 | while ( $nb > 0 ) { | ||
| 360 | my $nr = $self->{_maxblocksize}; | ||
| 361 | $nr = $nb if $nr > $nb; | ||
| 362 | my $buf; | ||
| 363 | last | ||
| 364 | unless | ||
| 365 | defined( $connection_handle->{socket}->recv( $buf, $nr, 0 ) ); | ||
| 366 | $nb -= length($buf); | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | sub _first { | ||
| 371 | my $s = shift; | ||
| 372 | my $n = length($s); | ||
| 373 | $n = 20 if $n > 20; | ||
| 374 | return substr( $s, 0, $n ); | ||
| 375 | } | ||
| 376 | |||
| 377 | # | ||
| 378 | # Todo: Evaluate $ibstatus: http://linux-gpib.sourceforge.net/doc_html/r634.html | ||
| 379 | # | ||
| 380 | sub connection_read | ||
| 381 | { # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 382 | my $self = shift; | ||
| 383 | my $connection_handle = shift; | ||
| 384 | my $args = undef; | ||
| 385 | if ( ref $_[0] eq 'HASH' ) { | ||
| 386 | $args = shift; | ||
| 387 | } # try to be flexible about options as hash/hashref | ||
| 388 | else { $args = {@_} } | ||
| 389 | |||
| 390 | my $command = $args->{'command'} || undef; | ||
| 391 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 392 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 393 | my $flush = $args->{'flush'} || 0; | ||
| 394 | my $timeout = $args->{'timeout'} || $self->timeout(); | ||
| 395 | $read_length = undef; | ||
| 396 | |||
| 397 | if ( !$connection_handle->{socket}->connected() ) { | ||
| 398 | carp("socket not connected"); | ||
| 399 | return undef; | ||
| 400 | } | ||
| 401 | |||
| 402 | my $result = ''; | ||
| 403 | my $eoi = 0; | ||
| 404 | my $srq = 0; | ||
| 405 | |||
| 406 | $self->{_ibsta} &= (RQS); | ||
| 407 | $self->{_ibcntl} = 0; | ||
| 408 | $self->{_iberr} = 0; | ||
| 409 | |||
| 410 | my $nb; | ||
| 411 | my $hdr; | ||
| 412 | my $size; | ||
| 413 | my $read = 0; | ||
| 414 | |||
| 415 | while (1) { | ||
| 416 | if ( $self->{_state} eq 'NetWaitHeader' ) { | ||
| 417 | |||
| 418 | $hdr = $self->_readHeader( $connection_handle, $args ); | ||
| 419 | if ( defined($hdr) ) { | ||
| 420 | $self->{_state} = 'NetWaitData'; | ||
| 421 | $nb = 0; | ||
| 422 | $eoi = $hdr->{EOI}; | ||
| 423 | $size = $hdr->{SIZE}; | ||
| 424 | |||
| 425 | #$read += length($hdr->{BUF}); | ||
| 426 | #$result .= $hdr->{BUF}; # in case appended to header | ||
| 427 | |||
| 428 | my $seq = $hdr->{SEQ}; | ||
| 429 | |||
| 430 | # print STDERR "hdr tot read=$read ",Dumper($hdr,$result),"\n"; | ||
| 431 | |||
| 432 | # flush old stuff? | ||
| 433 | if ( $self->{_flushread} | ||
| 434 | && $seq != 0 | ||
| 435 | && $self->_lastseq() > $seq ) { | ||
| 436 | $self->_dumpdata( $connection_handle, $hdr->{SIZE} ); | ||
| 437 | $self->{_state} = 'NetWaitHeader'; | ||
| 438 | } | ||
| 439 | |||
| 440 | # vicp version 1a has nonzero seq # | ||
| 441 | if ( $seq != 0 ) { | ||
| 442 | $self->{_version1a} = 1; | ||
| 443 | } | ||
| 444 | else { | ||
| 445 | $self->{_version1a} = 0; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | $self->{_ibsta} |= ERR; | ||
| 450 | $self->{_iberr} = TIMO; | ||
| 451 | last; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | if ( $self->{_state} eq 'NetWaitData' ) { | ||
| 456 | |||
| 457 | if ($flush) { | ||
| 458 | $self->_dumpdata( $connection_handle, $size - $read ); | ||
| 459 | $self->{_state} = 'NetWaitHeader'; | ||
| 460 | last; | ||
| 461 | } | ||
| 462 | |||
| 463 | my $sel = IO::Select->new( $connection_handle->{socket} ); | ||
| 464 | |||
| 465 | my (@ready) = $sel->can_read($timeout); | ||
| 466 | if ( $#ready < 0 ) { | ||
| 467 | carp("timeout on socket read:$!"); | ||
| 468 | $self->{_ibsta} |= ERR; | ||
| 469 | $self->{_iberr} = TIMO; | ||
| 470 | $sel->remove( $connection_handle->{socket} ); | ||
| 471 | return undef; | ||
| 472 | } | ||
| 473 | my $got = 0; | ||
| 474 | while (1) { | ||
| 475 | last if $got >= $size; | ||
| 476 | my $buf = ''; | ||
| 477 | $nb = $size - $got; | ||
| 478 | $nb = $self->{_maxblocksize} if $nb > $self->{_maxblocksize}; | ||
| 479 | |||
| 480 | (@ready) = $sel->can_read($timeout); | ||
| 481 | if ( $#ready < 0 ) { | ||
| 482 | carp("timeout on socket read:$!"); | ||
| 483 | $self->{_ibsta} |= ERR; | ||
| 484 | $self->{_iberr} = TIMO; | ||
| 485 | $sel->remove( $connection_handle->{socket} ); | ||
| 486 | return undef; | ||
| 487 | } | ||
| 488 | |||
| 489 | $connection_handle->{socket}->recv( $buf, $nb, 0 ); | ||
| 490 | |||
| 491 | # print STDERR "try read $nb, got:",Dumper(_first($buf)),"\n"; | ||
| 492 | if ( length($buf) < 1 ) { | ||
| 493 | carp("socket error on recv $!"); | ||
| 494 | $self->{_errflag} = 1; | ||
| 495 | $self->{_state} = 'NetError'; | ||
| 496 | $self->{_ibsta} |= ERR; | ||
| 497 | last; | ||
| 498 | } | ||
| 499 | $result .= $buf; | ||
| 500 | $read += length($buf); | ||
| 501 | $got += length($buf); | ||
| 502 | } | ||
| 503 | $sel->remove( $connection_handle->{socket} ); | ||
| 504 | |||
| 505 | $self->{_state} = 'NetWaitHeader'; | ||
| 506 | if ( $hdr->{SRQ} ) { # update srq status, discard packet | ||
| 507 | print "SRQ!\n"; | ||
| 508 | if ( substr( $result, 0, 1 ) eq '1' ) { | ||
| 509 | $self->{_ibsta} |= (RQS); | ||
| 510 | } | ||
| 511 | else { | ||
| 512 | $self->{_ibsta} &= ~(RQS); | ||
| 513 | } | ||
| 514 | $result = ''; | ||
| 515 | next; | ||
| 516 | } | ||
| 517 | if ($eoi) { | ||
| 518 | $self->{_ibsta} |= EOI; | ||
| 519 | last; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | if ( $self->{_state} eq 'NetError' ) { | ||
| 523 | $self->{_state} = 'NetWaitHeader'; | ||
| 524 | last; | ||
| 525 | } | ||
| 526 | |||
| 527 | # print STDERR "look for more packets? eoi=$eoi\n"; | ||
| 528 | last if $eoi; | ||
| 529 | } | ||
| 530 | $self->{_ibcntl} = length($result); | ||
| 531 | |||
| 532 | # no timeout, regular return | ||
| 533 | return $result; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub connection_query | ||
| 537 | { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal } | ||
| 538 | my $self = shift; | ||
| 539 | my $connection_handle = shift; | ||
| 540 | my $args = undef; | ||
| 541 | if ( ref $_[0] eq 'HASH' ) { | ||
| 542 | $args = shift; | ||
| 543 | } # try to be flexible about options as hash/hashref | ||
| 544 | else { $args = {@_} } | ||
| 545 | |||
| 546 | my $command = $args->{'command'} || undef; | ||
| 547 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 548 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 549 | my $wait_query = $args->{'wait_query'} || $self->wait_query(); | ||
| 550 | my $result = undef; | ||
| 551 | |||
| 552 | $self->connection_write( $connection_handle, $args ); | ||
| 553 | |||
| 554 | $args->{'timeout'} = $wait_query; | ||
| 555 | $result = $self->connection_read( $connection_handle, $args ); | ||
| 556 | return $result; | ||
| 557 | } | ||
| 558 | |||
| 559 | sub connection_settermchar { # @_ = ( $connection_handle, $termchar | ||
| 560 | # do nothing | ||
| 561 | return 1; | ||
| 562 | } | ||
| 563 | |||
| 564 | sub connection_enabletermchar { # @_ = ( $connection_handle, 0/1 off/on | ||
| 565 | # do nothing | ||
| 566 | return 1; | ||
| 567 | } | ||
| 568 | |||
| 569 | sub serial_poll { | ||
| 570 | my $self = shift; | ||
| 571 | my $connection_handle = shift; | ||
| 572 | my $sbyte = undef; | ||
| 573 | |||
| 574 | my $ibstatus = ibrsp( $connection_handle->{'gpib_handle'}, $sbyte ); | ||
| 575 | |||
| 576 | my $ib_bits = $self->ParseIbstatus($ibstatus); | ||
| 577 | |||
| 578 | if ( $ib_bits->{'ERR'} == 1 ) { | ||
| 579 | Lab::Exception::GPIBError->throw( | ||
| 580 | error => sprintf( | ||
| 581 | "ibrsp (serial poll) failed with status %x\n", | ||
| 582 | $ibstatus | ||
| 583 | ) | ||
| 584 | . Dumper($ib_bits), | ||
| 585 | ibsta => $ibstatus, | ||
| 586 | ibsta_hash => $ib_bits, | ||
| 587 | ); | ||
| 588 | } | ||
| 589 | |||
| 590 | return $sbyte; | ||
| 591 | } | ||
| 592 | |||
| 593 | sub connection_clear { | ||
| 594 | my $self = shift; | ||
| 595 | my $connection_handle = shift; | ||
| 596 | |||
| 597 | } | ||
| 598 | |||
| 599 | sub timeout { | ||
| 600 | my $self = shift; | ||
| 601 | my $connection_handle = shift; | ||
| 602 | return $self->{timeout} unless defined $connection_handle; | ||
| 603 | my $timo = shift; | ||
| 604 | return $self->{timeout} unless defined $timo; | ||
| 605 | |||
| 606 | if ( !defined($timo) | ||
| 607 | || $timo !~ /^\s*\+?(\d+|\d+\.\d*|\.\d+)(e[\+\-]?\d+)?\s*/i ) { | ||
| 608 | carp("bad value for timeout"); | ||
| 609 | return; | ||
| 610 | } | ||
| 611 | $timo += 0; | ||
| 612 | $self->{timeout} = $timo; | ||
| 613 | $connection_handle->{socket}->timeout($timo); | ||
| 614 | } | ||
| 615 | |||
| 616 | sub ParseIbstatus | ||
| 617 | { # Ibstatus http://linux-gpib.sourceforge.net/doc_html/r634.html | ||
| 618 | my $self = shift; | ||
| 619 | my $ibstatus = shift; # 16 Bit int | ||
| 620 | my @ibbits = (); | ||
| 621 | |||
| 622 | if ( $ibstatus !~ /[0-9]*/ || $ibstatus < 0 || $ibstatus > 0xFFFF ) | ||
| 623 | { # should be a 16 bit integer | ||
| 624 | carp("Got an invalid Ibstatus"); | ||
| 625 | return undef; | ||
| 626 | } | ||
| 627 | |||
| 628 | for ( my $i = 0; $i < 16; $i++ ) { | ||
| 629 | $ibbits[$i] = 0x0001 & ( $ibstatus >> $i ); | ||
| 630 | } | ||
| 631 | |||
| 632 | my %Ib = (); | ||
| 633 | ( | ||
| 634 | $Ib{'DCAS'}, $Ib{'DTAS'}, $Ib{'LACS'}, $Ib{'TACS'}, | ||
| 635 | $Ib{'ATN'}, $Ib{'CIC'}, $Ib{'REM'}, $Ib{'LOK'}, | ||
| 636 | $Ib{'CMPL'}, $Ib{'EVENT'}, $Ib{'SPOLL'}, $Ib{'RQS'}, | ||
| 637 | $Ib{'SRQI'}, $Ib{'END'}, $Ib{'TIMO'}, $Ib{'ERR'} | ||
| 638 | ) = @ibbits; | ||
| 639 | |||
| 640 | return \%Ib; | ||
| 641 | |||
| 642 | } # return: ($ERR, $TIMO, $END, $SRQI, $RQS, $SPOLL, $EVENT, $CMPL, $LOK, $REM, $CIC, $ATN, $TACS, $LACS, $DTAS, $DCAS) | ||
| 643 | |||
| 644 | sub VerboseIbstatus { | ||
| 645 | my $self = shift; | ||
| 646 | my $ibstatus = shift; | ||
| 647 | my $ibstatus_verbose = ""; | ||
| 648 | |||
| 649 | if ( ref( \$ibstatus ) =~ /SCALAR/ ) { | ||
| 650 | $ibstatus = $self->ParseIbstatus($ibstatus); | ||
| 651 | } | ||
| 652 | elsif ( ref($ibstatus) !~ /HASH/ ) { | ||
| 653 | carp("invalid ibstatus parameter"); | ||
| 654 | return undef; | ||
| 655 | } | ||
| 656 | |||
| 657 | while ( my ( $k, $v ) = each %$ibstatus ) { | ||
| 658 | $ibstatus_verbose .= "$k: $v\n"; | ||
| 659 | } | ||
| 660 | |||
| 661 | return $ibstatus_verbose; | ||
| 662 | } | ||
| 663 | |||
| 664 | 1; | ||
| 665 | |||
| 666 | |||
| 667 | 1; | ||
| 668 | |||
| 669 | __END__ |