| 1 | package Lab::Bus::VISA; | ||
| 2 | #ABSTRACT: National Instruments VISA bus | ||
| 3 | $Lab::Bus::VISA::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::VISA; | ||
| 8 | use Scalar::Util qw(weaken); | ||
| 9 | use Time::HiRes qw (usleep sleep); | ||
| 10 | use Lab::Bus; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | our @ISA = ("Lab::Bus"); | ||
| 15 | |||
| 16 | our %fields = ( | ||
| 17 | default_rm => undef, | ||
| 18 | type => 'VISA', | ||
| 19 | brutal => 0, # brutal as default? | ||
| 20 | wait_status => 10e-6, # sec; | ||
| 21 | wait_query => 10e-6, # sec; | ||
| 22 | query_length => 300, # bytes | ||
| 23 | query_long_length => 10240, #bytes | ||
| 24 | read_length => 1000 # bytes | ||
| 25 | |||
| 26 | ); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $proto = shift; | ||
| 30 | my $class = ref($proto) || $proto; | ||
| 31 | my $twin = undef; | ||
| 32 | my $self = $class->SUPER::new(@_) | ||
| 33 | ; # getting fields and _permitted from parent class | ||
| 34 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 35 | |||
| 36 | # search for twin in %Lab::Bus::BusList. If there's none, place $self there and weaken it. | ||
| 37 | if ( $class eq __PACKAGE__ ) | ||
| 38 | { # careful - do only if this is not a parent class constructor | ||
| 39 | if ( $twin = $self->_search_twin() ) { | ||
| 40 | undef $self; | ||
| 41 | return $twin; # ...and that's it. | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | # no distinction between VISA resource managers yet - need more than one? | ||
| 45 | $Lab::Bus::BusList{ $self->type() }->{'default'} = $self; | ||
| 46 | weaken( $Lab::Bus::BusList{ $self->type() }->{'default'} ); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | my ( $status, $rm ) = Lab::VISA::viOpenDefaultRM(); | ||
| 51 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 52 | Lab::Exception::VISAError->throw( | ||
| 53 | error => 'Cannot open resource manager: $status\n' ); | ||
| 54 | } | ||
| 55 | $self->default_rm($rm); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _check_resource_name { # @_ = ( $resource_name ) | ||
| 61 | my ( $self, $resname ) = ( shift, shift ); | ||
| 62 | my $found = undef; | ||
| 63 | |||
| 64 | # check for a valid resource name. let's start with GPIB INSTR (NI-VISA Programmer Reference Manual, P. 276) | ||
| 65 | if ( | ||
| 66 | $resname =~ /^GPIB[0-9]*::[0-9]+(::[0-9]+)?(::INSTR)?$/ # GPIB INSTR | ||
| 67 | ) { | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | elsif ( | ||
| 71 | $resname =~ /^ASRL[0-9]+(::INSTR)?$/ # RS232 INSTR | ||
| 72 | ) { | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | elsif ( | ||
| 76 | $resname =~ /^TCPIP0?::[0-9\.]*(::INSTR)?$/ # TCP/IP INSTR | ||
| 77 | ) { | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub connection_new { # @_ = ({ resource_name => $resource_name }) | ||
| 85 | my $self = shift; | ||
| 86 | my $args = undef; | ||
| 87 | my $status = undef; | ||
| 88 | my $connection_handle = undef; | ||
| 89 | if ( ref $_[0] eq 'HASH' ) { | ||
| 90 | $args = shift; | ||
| 91 | } # try to be flexible about options as hash/hashref | ||
| 92 | else { $args = {@_} } | ||
| 93 | $self->{config} = $args; | ||
| 94 | |||
| 95 | my $resource_name = $args->{'resource_name'}; | ||
| 96 | |||
| 97 | Lab::Exception::CorruptParameter->throw( error => | ||
| 98 | 'No resource name given to Lab::Bus::VISA::connection_new().\n' ) | ||
| 99 | if ( !exists $args->{'resource_name'} ); | ||
| 100 | Lab::Exception::CorruptParameter->throw( error => | ||
| 101 | 'Invalid resource name given to Lab::Bus::VISA::connection_new().\n' | ||
| 102 | ) if ( !$self->_check_resource_name( $args->{'resource_name'} ) ); | ||
| 103 | |||
| 104 | ( $status, $connection_handle ) = Lab::VISA::viOpen( | ||
| 105 | $self->default_rm(), $args->{'resource_name'}, | ||
| 106 | $Lab::VISA::VI_NULL, $Lab::VISA::VI_NULL | ||
| 107 | ); | ||
| 108 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 109 | Lab::Exception::VISAError->throw( | ||
| 110 | error => | ||
| 111 | "Cannot open VISA instrument \"$resource_name\". Status: $status", | ||
| 112 | status => $status | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | return $connection_handle; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub connection_read | ||
| 120 | { # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 121 | my $self = shift; | ||
| 122 | my $connection_handle = shift; | ||
| 123 | my $args = undef; | ||
| 124 | if ( ref $_[0] eq 'HASH' ) { | ||
| 125 | $args = shift; | ||
| 126 | } # try to be flexible about options as hash/hashref | ||
| 127 | else { $args = {@_} } | ||
| 128 | |||
| 129 | my $command = $args->{'command'} || undef; | ||
| 130 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 131 | my $result_conv = undef; | ||
| 132 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 133 | my $timeout = $args->{'timeout'} || undef; | ||
| 134 | my $old_timeout_value = undef; | ||
| 135 | |||
| 136 | my $result = undef; | ||
| 137 | my $status = undef; | ||
| 138 | my $read_cnt = undef; | ||
| 139 | |||
| 140 | if ( defined $timeout ) { | ||
| 141 | $self->set_visa_attribute( | ||
| 142 | $connection_handle, | ||
| 143 | $Lab::VISA::VI_ATTR_TMO_VALUE, $timeout * 1e3 | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | |||
| 147 | ( $status, $result, $read_cnt ) | ||
| 148 | = Lab::VISA::viRead( $connection_handle, $read_length ); | ||
| 149 | |||
| 150 | #print "$status,$result,$read_cnt\n"; | ||
| 151 | #exit; | ||
| 152 | |||
| 153 | if ( | ||
| 154 | !( | ||
| 155 | $status == $Lab::VISA::VI_SUCCESS | ||
| 156 | || $status == $Lab::VISA::VI_SUCCESS_TERM_CHAR | ||
| 157 | || $status == $Lab::VISA::VI_ERROR_TMO | ||
| 158 | || $status > 0 | ||
| 159 | ) | ||
| 160 | ) { | ||
| 161 | Lab::Exception::VISAError->throw( | ||
| 162 | error => | ||
| 163 | "Error in Lab::Bus::VISA::connection_read() while executing $command, Status $status", | ||
| 164 | status => $status, | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | elsif ( $status == $Lab::VISA::VI_ERROR_TMO && !$brutal ) { | ||
| 168 | Lab::Exception::VISATimeout->throw( | ||
| 169 | error => | ||
| 170 | "Timeout in Lab::Bus::VISA::connection_read() while executing $command\n", | ||
| 171 | status => $status, | ||
| 172 | command => $command, | ||
| 173 | data => $result, | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | |||
| 177 | if ( defined $timeout ) { | ||
| 178 | $self->set_visa_attribute( | ||
| 179 | $connection_handle, | ||
| 180 | $Lab::VISA::VI_ATTR_TMO_VALUE, $self->config('timeout') * 1e3 | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | $result = substr( $result, 0, $read_cnt ); | ||
| 185 | |||
| 186 | return $result; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub connection_write | ||
| 190 | { # @_ = ( $connection_handle, $args = { command, wait_status } | ||
| 191 | my $self = shift; | ||
| 192 | my $connection_handle = shift; | ||
| 193 | my $args = undef; | ||
| 194 | if ( ref $_[0] eq 'HASH' ) { | ||
| 195 | $args = shift; | ||
| 196 | } # try to be flexible about options as hash/hashref | ||
| 197 | else { $args = {@_} } | ||
| 198 | |||
| 199 | my $command = $args->{'command'} || undef; | ||
| 200 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 201 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 202 | my $wait_status = $args->{'wait_status'} || $self->wait_status(); | ||
| 203 | |||
| 204 | my $result = undef; | ||
| 205 | my $status = undef; | ||
| 206 | my $write_cnt = 0; | ||
| 207 | my $read_cnt = undef; | ||
| 208 | |||
| 209 | if ( !defined $command ) { | ||
| 210 | Lab::Exception::CorruptParameter->throw( | ||
| 211 | error => "No command given to " | ||
| 212 | . __PACKAGE__ | ||
| 213 | . "::connection_write().\n", ); | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | ( $status, $write_cnt ) = Lab::VISA::viWrite( | ||
| 217 | $connection_handle, $command, | ||
| 218 | length($command) | ||
| 219 | ); | ||
| 220 | |||
| 221 | sleep($wait_status); | ||
| 222 | |||
| 223 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 224 | Lab::Exception::VISAError->throw( | ||
| 225 | error => | ||
| 226 | "Error in Lab::Bus::VISA::connection_write() while executing $command, Status $status", | ||
| 227 | status => $status, | ||
| 228 | ); | ||
| 229 | } | ||
| 230 | |||
| 231 | return $write_cnt; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | sub connection_query | ||
| 236 | { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal } | ||
| 237 | my $self = shift; | ||
| 238 | my $connection_handle = shift; | ||
| 239 | my $args = undef; | ||
| 240 | if ( ref $_[0] eq 'HASH' ) { | ||
| 241 | $args = shift; | ||
| 242 | } # try to be flexible about options as hash/hashref | ||
| 243 | else { $args = {@_} } | ||
| 244 | |||
| 245 | my $command = $args->{'command'} || undef; | ||
| 246 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 247 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 248 | my $wait_status = $args->{'wait_status'} || $self->wait_status(); | ||
| 249 | my $wait_query = $args->{'wait_query'} || $self->wait_query(); | ||
| 250 | |||
| 251 | my $result = undef; | ||
| 252 | my $status = undef; | ||
| 253 | my $write_cnt = 0; | ||
| 254 | my $read_cnt = undef; | ||
| 255 | |||
| 256 | $write_cnt = $self->connection_write($args); | ||
| 257 | |||
| 258 | usleep($wait_query) | ||
| 259 | ; #<---ensures that asked data presented from the device | ||
| 260 | |||
| 261 | $result = $self->connection_read($args); | ||
| 262 | return $result; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub connection_clear { | ||
| 266 | my $self = shift; | ||
| 267 | my $connection_handle = shift; | ||
| 268 | |||
| 269 | while (1) { | ||
| 270 | my $result = $self->connection_read( | ||
| 271 | $connection_handle, | ||
| 272 | { timeout => 0.1, brutal => 1 } | ||
| 273 | ); | ||
| 274 | if ( $result == 0 ) { last; } | ||
| 275 | } | ||
| 276 | |||
| 277 | } | ||
| 278 | |||
| 279 | sub serial_poll { | ||
| 280 | my $self = shift; | ||
| 281 | my $connection_handle = shift; | ||
| 282 | |||
| 283 | my ( $ibstatus, $sbyte ) = Lab::VISA::viReadSTB($connection_handle); | ||
| 284 | |||
| 285 | # | ||
| 286 | # TODO: VISA status evaluation | ||
| 287 | # | ||
| 288 | # my $ib_bits=$self->ParseIbstatus($ibstatus); | ||
| 289 | # | ||
| 290 | # if($ib_bits->{'ERR'}==1) { | ||
| 291 | # Lab::Exception::GPIBError->throw( | ||
| 292 | # error => sprintf("ibrsp (serial poll) failed with status %x\n", $ibstatus) . Dumper($ib_bits), | ||
| 293 | # ibsta => $ibstatus, | ||
| 294 | # ibsta_hash => $ib_bits, | ||
| 295 | # ); | ||
| 296 | # } | ||
| 297 | |||
| 298 | return $sbyte; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub timeout { | ||
| 302 | |||
| 303 | my $self = shift; | ||
| 304 | my $connection_handle = shift; | ||
| 305 | my $timeout = shift; | ||
| 306 | |||
| 307 | my $result = Lab::VISA::viSetAttribute( | ||
| 308 | $connection_handle, | ||
| 309 | $Lab::VISA::VI_ATTR_TMO_VALUE, $timeout * 1e3 | ||
| 310 | ); | ||
| 311 | if ( $result != $Lab::VISA::VI_SUCCESS ) { | ||
| 312 | print new Lab::Exception::VISAError( error => | ||
| 313 | "Error while setting Visa Attribute Timeout. $result \n" ); | ||
| 314 | |||
| 315 | } | ||
| 316 | return $result; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub set_visa_attribute { | ||
| 320 | |||
| 321 | my $self = shift; | ||
| 322 | my $connection_handle = shift; | ||
| 323 | my $attribute = shift; | ||
| 324 | my $value = shift; | ||
| 325 | |||
| 326 | if ( defined $value ) { | ||
| 327 | my $result = Lab::VISA::viSetAttribute( | ||
| 328 | $connection_handle, $attribute, | ||
| 329 | $value | ||
| 330 | ); | ||
| 331 | if ( $result != $Lab::VISA::VI_SUCCESS ) { | ||
| 332 | print new Lab::Exception::VISAError( error => | ||
| 333 | "Error while setting Visa Attribute $attribute. $result \n" | ||
| 334 | ); | ||
| 335 | } | ||
| 336 | return $result; | ||
| 337 | } | ||
| 338 | return; | ||
| 339 | } | ||
| 340 | |||
| 341 | # | ||
| 342 | # calls ibclear() on the instrument - how to do on VISA? | ||
| 343 | # | ||
| 344 | #sub connection_clear { | ||
| 345 | # my $self = shift; | ||
| 346 | # my $connection_handle=shift; | ||
| 347 | # | ||
| 348 | # ibclr($connection_handle->{'gpib_handle'}); | ||
| 349 | #} | ||
| 350 | |||
| 351 | # | ||
| 352 | # search and return an instance of the same type in %Lab::Bus::BusList | ||
| 353 | # | ||
| 354 | sub _search_twin { | ||
| 355 | my $self = shift; | ||
| 356 | |||
| 357 | # Only one VISA bus for the moment, stored as "default" | ||
| 358 | if ( !$self->ignore_twins() ) { | ||
| 359 | if ( defined $Lab::Bus::BusList{ $self->type() }->{'default'} ) { | ||
| 360 | return $Lab::Bus::BusList{ $self->type() }->{'default'}; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | return undef; | ||
| 365 | } | ||
| 366 | |||
| 367 | 1; | ||
| 368 | |||
| 369 | |||
| 370 | 1; | ||
| 371 | |||
| 372 | __END__ |