| 1 | package Lab::Instrument::Keithley2000; | ||
| 2 | #ABSTRACT: Keithley 2000 digital multimeter | ||
| 3 | $Lab::Instrument::Keithley2000::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::Instrument; | ||
| 8 | use Time::HiRes qw (usleep); | ||
| 9 | |||
| 10 | our @ISA = ('Lab::Instrument'); | ||
| 11 | |||
| 12 | our %fields = ( | ||
| 13 | supported_connections => | ||
| 14 | [ 'VISA', 'VISA_GPIB', 'GPIB', 'RS232', 'VISA_RS232', 'DEBUG' ], | ||
| 15 | |||
| 16 | # default settings for the supported connections | ||
| 17 | connection_settings => { | ||
| 18 | gpib_board => undef, | ||
| 19 | gpib_address => undef, | ||
| 20 | timeout => 2, | ||
| 21 | }, | ||
| 22 | |||
| 23 | device_settings => {}, | ||
| 24 | |||
| 25 | device_cache => { | ||
| 26 | |||
| 27 | } | ||
| 28 | |||
| 29 | ); | ||
| 30 | |||
| 31 | # ---------------------- Init DMM -------------------------------------------------------- | ||
| 32 | sub new { | ||
| 33 | my $proto = shift; | ||
| 34 | my $class = ref($proto) || $proto; | ||
| 35 | my $self = $class->SUPER::new(@_); | ||
| 36 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 37 | $self->write('INIT:CONT OFF'); | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub reset { # basic | ||
| 42 | my $self = shift; | ||
| 43 | $self->write("*RST"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | # ----------------------- Config DMM ------------------------------------------------------ | ||
| 48 | |||
| 49 | sub set_function { # basic | ||
| 50 | my $self = shift; | ||
| 51 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 52 | |||
| 53 | if ( $function | ||
| 54 | =~ /\b(PERIOD|period|PER|per|FREQUENCY|frequency|FREQ|freq|TEMPERATURE|temperature|TEMP|temp|DIODE|diode|DIOD|diod","CURRENT|current|CURR|curr|CURRENT:AC|current:ac|CURR:AC|curr:ac","CURRENT:DC|current:dc|CURR:DC|curr:dc|VOLTAGE|voltage|VOLT|volt|VOLTAGE:AC|voltage:ac|VOLT:AC|volt:ac|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc|RESISTANCE|resisitance|RES|res|FRESISTANCE|fresistance|FRES|fres)\b/ | ||
| 55 | ) { | ||
| 56 | return $self->query( | ||
| 57 | sprintf( ":SENSE:FUNCTION '%s'; FUNCTION?", $function ) ); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | Lab::Exception::CorruptParameter->throw( error => | ||
| 61 | "unexpected value in sub config_function. Function can be CURRENT:AC, CURRENT:DC, VOLTAGE:AC, VOLTAGE:DC, RESISTANCE, FRESISTANCE, PERIOD, FREQUENCY, TEMPERATURE, DIODE" | ||
| 62 | ); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get_function { | ||
| 67 | my $self = shift; | ||
| 68 | my $function = $self->query(":SENSE:FUNCTION?"); | ||
| 69 | return substr( $function, 1, -1 ); # cut off quotes "" | ||
| 70 | } | ||
| 71 | |||
| 72 | sub set_range { # basic | ||
| 73 | my $self = shift; | ||
| 74 | my ( $range, $function ) | ||
| 75 | = $self->_check_args( \@_, [ 'range', 'function' ] ); | ||
| 76 | |||
| 77 | # return settings | ||
| 78 | |||
| 79 | if ( not defined $function ) { | ||
| 80 | $function = $self->get_function(); | ||
| 81 | } | ||
| 82 | |||
| 83 | #set range | ||
| 84 | if ( $function | ||
| 85 | =~ /\b(CURRENT|current|CURR|curr|CURRENT:AC|current:ac|CURR:AC|curr:ac","CURRENT:DC|current:dc|CURR:DC|curr:dc)\b/ | ||
| 86 | ) { | ||
| 87 | if ( $range =~ /\b(AUTO|auto|MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 88 | |||
| 89 | #pass | ||
| 90 | } | ||
| 91 | elsif ( ( $range >= 0 && $range <= 3.03 ) ) { | ||
| 92 | $range = sprintf( "%.2f", $range ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | Lab::Exception::CorruptParameter->throw( error => | ||
| 96 | "unexpected value in sub config_range for 'RANGE'. Must be between 0 and 3.03." | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | elsif ( $function =~ /\b(VOLTAGE:AC|voltage:ac|VOLT:AC|volt:ac)\b/ ) { | ||
| 102 | if ( $range =~ /\b(AUTO|auto|MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 103 | |||
| 104 | #pass | ||
| 105 | } | ||
| 106 | elsif ( ( $range >= 0 && $range <= 757.5 ) ) { | ||
| 107 | $range = sprintf( "%.1f", $range ); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | Lab::Exception::CorruptParameter->throw( error => | ||
| 111 | "unexpected value in sub config_range for 'RANGE'. Must be between 0 and 757.5." | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | elsif ( $function | ||
| 116 | =~ /\b(VOLTAGE|voltage|VOLT|volt|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc)\b/ | ||
| 117 | ) { | ||
| 118 | if ( $range =~ /\b(AUTO|auto|MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 119 | |||
| 120 | #pass | ||
| 121 | } | ||
| 122 | elsif ( ( $range >= 0 && $range <= 1010 ) ) { | ||
| 123 | $range = sprintf( "%.1f", $range ); | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | Lab::Exception::CorruptParameter->throw( error => | ||
| 127 | "unexpected value in sub config_range for 'RANGE'. Must be between 0 and 1010." | ||
| 128 | ); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | elsif ( $function | ||
| 132 | =~ /\b(RESISTANCE|resisitance|RES|res|FRESISTANCE|fresistance|FRES|fres)\b/ | ||
| 133 | ) { | ||
| 134 | if ( $range =~ /\b(AUTO|auto|MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 135 | |||
| 136 | #pass | ||
| 137 | } | ||
| 138 | elsif ( ( $range >= 0 && $range <= 101e6 ) ) { | ||
| 139 | $range = sprintf( "%d", $range ); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | Lab::Exception::CorruptParameter->throw( error => | ||
| 143 | "unexpected value in sub config_range for 'RANGE'. Must be between 0 and 101E6." | ||
| 144 | ); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | elsif ( $function =~ /\b(DIODE|DIOD|diode|diod)\b/ ) { | ||
| 148 | $function = "DIOD:CURRENT"; | ||
| 149 | if ( $range < 0 || $range > 1e-3 ) { | ||
| 150 | Lab::Exception::CorruptParameter->throw( error => | ||
| 151 | "unexpected value in sub config_range for 'RANGE'. Must be between 0 and 1E-3." | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | Lab::Exception::CorruptParameter->throw( error => | ||
| 157 | "unexpected value in sub set_range. Function can be CURRENT:AC, CURRENT:DC, VOLTAGE:AC, VOLTAGE:DC, RESISTANCE, FRESISTANCE" | ||
| 158 | ); | ||
| 159 | } | ||
| 160 | |||
| 161 | # set range | ||
| 162 | if ( $range =~ /\b(AUTO|auto)\b/ ) { | ||
| 163 | $self->write( sprintf( ":SENSE:%s:RANGE:AUTO ON", $function ) ); | ||
| 164 | } | ||
| 165 | elsif ( $range =~ /\b(MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 166 | $self->write( sprintf( ":SENSE:%s:RANGE %s", $function, $range ) ); | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $self->write( sprintf( ":SENSE:%s:RANGE %.2f", $function, $range ) ); | ||
| 170 | } | ||
| 171 | return; | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | sub get_range { | ||
| 176 | my $self = shift; | ||
| 177 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 178 | |||
| 179 | if ( not defined $function ) { | ||
| 180 | $function = $self->get_function(); | ||
| 181 | } | ||
| 182 | if ( | ||
| 183 | not $function | ||
| 184 | =~ /\b(PERIOD|period|PER|per|FREQUENCY|frequency|FREQ|freq|TEMPERATURE|temperature|TEMP|temp|DIODE|diode|DIOD|diod","CURRENT|current|CURR|curr|CURRENT:AC|current:ac|CURR:AC|curr:ac","CURRENT:DC|current:dc|CURR:DC|curr:dc|VOLTAGE|voltage|VOLT|volt|VOLTAGE:AC|voltage:ac|VOLT:AC|volt:ac|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc|RESISTANCE|resisitance|RES|res|FRESISTANCE|fresistance|FRES|fres)\b/ | ||
| 185 | ) { | ||
| 186 | Lab::Exception::CorruptParameter->throw( | ||
| 187 | error => "unexpected value for FUNCTION in sub get_range." ); | ||
| 188 | } | ||
| 189 | |||
| 190 | my $range = $self->query(":SENSE:$function:RANGE?"); | ||
| 191 | return $range; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub set_nplc { # basic | ||
| 195 | my $self = shift; | ||
| 196 | my ( $nplc, $function ) | ||
| 197 | = $self->_check_args( \@_, [ 'nplc', 'function' ] ); | ||
| 198 | |||
| 199 | # return settings if no new values are given | ||
| 200 | if ( not defined $function ) { | ||
| 201 | $function = $self->get_function(); | ||
| 202 | } | ||
| 203 | |||
| 204 | if ( ( $nplc < 0.01 && $nplc > 10 ) | ||
| 205 | and not $nplc =~ /\b(MAX|max|MIN|min|DEF|def)\b/ ) { | ||
| 206 | Lab::Exception::CorruptParameter->throw( error => | ||
| 207 | "unexpected value for NPLC in sub set_sense_nplc. Expected values are between 0.01 and 1000 POWER LINE CYCLES or MIN/MAX/DEF." | ||
| 208 | ); | ||
| 209 | } | ||
| 210 | |||
| 211 | if ( $function | ||
| 212 | =~ /\b(CURRENT|CURR|current|curr|VOLTAGE|VOLT|voltage|volt|RESISTANCE|RES|resistance|res)\b/ | ||
| 213 | ) { | ||
| 214 | if ( $nplc =~ /\b(MAX|max|MIN|min|DEF|def)\b/ ) { | ||
| 215 | return $self->query( | ||
| 216 | sprintf( ":SENSE:%s:NPLC %s; NPLC?", $function, $nplc ) ); | ||
| 217 | } | ||
| 218 | elsif ( $nplc =~ /\b\d+(e\d+|E\d+|exp\d+|EXP\d+)?\b/ ) { | ||
| 219 | return $self->query( | ||
| 220 | sprintf( ":SENSE:%s:NPLC %e; NPLC?", $function, $nplc ) ); | ||
| 221 | } | ||
| 222 | else { | ||
| 223 | Lab::Exception::CorruptParameter->throw( error => | ||
| 224 | "unexpected value for NPLC in sub set_sense_nplc. Expected values are between 0.01 and 10 POWER LINE CYCLES or MIN/MAX/DEF." | ||
| 225 | ); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | Lab::Exception::CorruptParameter->throw( error => | ||
| 230 | "unexpected value for FUNCTION in sub set_sense_nplc. Expected values are CURRENT:AC, CURRENT:DC, VOLTAGE:AC, VOLTAGE:DC, RESISTANCE, FRESISTANCE, TEMPERATURE" | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | sub get_nplc { | ||
| 236 | my $self = shift; | ||
| 237 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 238 | |||
| 239 | if ( not defined $function ) { | ||
| 240 | $function = $self->get_function(); | ||
| 241 | } | ||
| 242 | if ( | ||
| 243 | not $function | ||
| 244 | =~ /\b(PERIOD|period|PER|per|FREQUENCY|frequency|FREQ|freq|TEMPERATURE|temperature|TEMP|temp|DIODE|diode|DIOD|diod","CURRENT|current|CURR|curr|CURRENT:AC|current:ac|CURR:AC|curr:ac","CURRENT:DC|current:dc|CURR:DC|curr:dc|VOLTAGE|voltage|VOLT|volt|VOLTAGE:AC|voltage:ac|VOLT:AC|volt:ac|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc|RESISTANCE|resisitance|RES|res|FRESISTANCE|fresistance|FRES|fres)\b/ | ||
| 245 | ) { | ||
| 246 | Lab::Exception::CorruptParameter->throw( | ||
| 247 | error => "unexpected value for FUNCTION in sub get_nplc." ); | ||
| 248 | } | ||
| 249 | |||
| 250 | my $nplc = $self->query(":SENSE:$function:NPLC?"); | ||
| 251 | |||
| 252 | return $nplc; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub set_averaging { # advanced | ||
| 256 | my $self = shift; | ||
| 257 | my ( $count, $mode, $function ) | ||
| 258 | = $self->_check_args( \@_, [ 'count', 'mode', 'function' ] ); | ||
| 259 | |||
| 260 | if ( not defined $function ) { | ||
| 261 | $function = $self->get_function(); # get selected function | ||
| 262 | } | ||
| 263 | |||
| 264 | if ( not defined $mode ) { | ||
| 265 | $mode = "REPEAT"; | ||
| 266 | } # set REPeating as standard value; MOVing would be 2nd option | ||
| 267 | |||
| 268 | if ( $mode =~ /\b(REPEAT|repeat|MOVING|moving)\b/ ) { | ||
| 269 | if ( $count >= 0.5 and $count <= 100.5 ) { | ||
| 270 | |||
| 271 | # set averaging | ||
| 272 | $self->write(":SENSE:$function:AVERAGE:STATE ON"); | ||
| 273 | $self->write(":SENSE:$function:AVERAGE:TCONTROL $mode"); | ||
| 274 | |||
| 275 | if ( $count =~ /\b(MIN|min|MAX|max|DEF|def)\b/ | ||
| 276 | or $count =~ /\b\d+(e\d+|E\d+|exp\d+|EXP\d+)?\b/ ) { | ||
| 277 | $count | ||
| 278 | = $self->query( | ||
| 279 | ":SENSE:$function:AVERAGE:COUNT $count; STATE?; COUNT?; TCONTROL?" | ||
| 280 | ); | ||
| 281 | my $result; | ||
| 282 | ( | ||
| 283 | $result->{'state'}, $result->{'count'}, | ||
| 284 | $result->{'tcontrol'} | ||
| 285 | ) = split( /;/, $count ); | ||
| 286 | return $result; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | elsif ( $count =~ /\b(OFF|off)\b/ or $count == 0 ) { | ||
| 290 | $self->write(":SENSE:$function:AVERAGE:STATE OFF") | ||
| 291 | ; # switch OFF Averaging | ||
| 292 | $count = $self->query( | ||
| 293 | ":SENSE:$function:AVERAGE:STATE?; COUNT?; TCONTROL?"); | ||
| 294 | my $result; | ||
| 295 | ( $result->{'state'}, $result->{'count'}, $result->{'tcontrol'} ) | ||
| 296 | = split( /;/, $count ); | ||
| 297 | return $result; | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | Lab::Exception::CorruptParameter->throw( error => | ||
| 301 | "unexpected value for COUNT in sub set_averaging. Expected values are between 1 ... 100 or MIN/MAX/DEF/OFF." | ||
| 302 | ); | ||
| 303 | } | ||
| 304 | |||
| 305 | } | ||
| 306 | else { | ||
| 307 | Lab::Exception::CorruptParameter->throw( error => | ||
| 308 | "unexpected value for FILTERMODE in sub set_averaging. Expected values are REPEAT and MOVING." | ||
| 309 | ); | ||
| 310 | } | ||
| 311 | |||
| 312 | } | ||
| 313 | |||
| 314 | sub get_averaging { | ||
| 315 | my $self = shift; | ||
| 316 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 317 | |||
| 318 | if ( not defined $function ) { | ||
| 319 | $function = $self->get_function(); | ||
| 320 | } | ||
| 321 | |||
| 322 | my $count | ||
| 323 | = $self->query(":SENSE:$function:AVERAGE:STATE?; COUNT?; TCONTROL?"); | ||
| 324 | my $result; | ||
| 325 | ( $result->{'state'}, $result->{'count'}, $result->{'tcontrol'} ) | ||
| 326 | = split( /;/, $count ); | ||
| 327 | return $result; | ||
| 328 | |||
| 329 | } | ||
| 330 | |||
| 331 | # ----------------------------------------- MEASUREMENT ---------------------------------- | ||
| 332 | |||
| 333 | sub get_value { # basic | ||
| 334 | my $self = shift; | ||
| 335 | my ($function) = $self->_check_args( \@_, ['function'] ); | ||
| 336 | |||
| 337 | if ( not defined $function ) { | ||
| 338 | $self->device_cache()->{value} = $self->query(':READ?'); | ||
| 339 | return $self->device_cache()->{value}; | ||
| 340 | |||
| 341 | } | ||
| 342 | elsif ( $function | ||
| 343 | =~ /\b(PERIOD|period|PER|per|FREQUENCY|frequency|FREQ|freq|TEMPERATURE|temperature|TEMP|temp|DIODE|diode|DIOD|diod|CURRENT|current|CURR|curr|CURRENT:AC|current:ac|CURR:AC|curr:ac|CURRENT:DC|current:dc|CURR:DC|curr:dc|VOLTAGE|voltage|VOLT|volt|VOLTAGE:AC|voltage:ac|VOLT:AC|volt:ac|VOLTAGE:DC|voltage:dc|VOLT:DC|volt:dc|RESISTANCE|resisitance|RES|res|FRESISTANCE|fresistance|FRES|fres)\b/ | ||
| 344 | ) { | ||
| 345 | my $cmd = sprintf( ":MEASURE:%s?", $function ); | ||
| 346 | $self->device_cache()->{value} = $self->query($cmd); | ||
| 347 | return $self->device_cache()->{value}; | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | Lab::Exception::CorruptParameter->throw( error => | ||
| 351 | "unexpected value for 'function' in sub measure. Function can be CURRENT:AC, CURRENT:DC, VOLTAGE:AC, VOLTAGE:DC, RESISTANCE, FRESISTANCE, PERIOD, FREQUENCY, TEMPERATURE, DIODE" | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | sub config_measurement { # basic | ||
| 357 | my $self = shift; | ||
| 358 | |||
| 359 | my ( $function, $nop, $time, $range, $trigger ) = $self->_check_args( | ||
| 360 | \@_, | ||
| 361 | [ 'function', 'nop', 'time', 'range', 'trigger' ] | ||
| 362 | ); | ||
| 363 | |||
| 364 | # check input data | ||
| 365 | if ( not defined $trigger ) { | ||
| 366 | $trigger = 'BUS'; | ||
| 367 | } | ||
| 368 | if ( not defined $range ) { | ||
| 369 | $range = 'DEF'; | ||
| 370 | } | ||
| 371 | if ( not defined $time ) { | ||
| 372 | Lab::Exception::CorruptParameter->throw( error => | ||
| 373 | "too view arguments given in sub config_measurement. Expected arguments are FUNCTION, #POINTS, TIME, <RANGE>, <TRIGGERSOURCE>" | ||
| 374 | ); | ||
| 375 | } | ||
| 376 | |||
| 377 | $self->set_function($function); | ||
| 378 | print "sub config_measurement: set FUNCTION: " | ||
| 379 | . $self->set_function() . "\n"; | ||
| 380 | |||
| 381 | $self->set_range( $function, $range ); | ||
| 382 | print "sub config_measurement: set RANGE: " . $self->set_range() . "\n"; | ||
| 383 | |||
| 384 | my $nplc = ( $time * 50 ) / $nop; | ||
| 385 | if ( $nplc < 0.01 ) { | ||
| 386 | Lab::Exception::CorruptParameter->throw( error => | ||
| 387 | "unexpected value for TIME in sub config_measurement. Expected values are between 0.5 ... 50000 sec." | ||
| 388 | ); | ||
| 389 | } | ||
| 390 | $self->set_nplc( $function, $nplc ); | ||
| 391 | print "sub config_measurement: set NPLC: " . $self->set_nplc() . "\n"; | ||
| 392 | |||
| 393 | $self->_init_buffer($nop); | ||
| 394 | print "sub config_measurement: init BUFFER: " | ||
| 395 | . $self->_init_buffer($nop) . "\n"; | ||
| 396 | |||
| 397 | $self->_init_trigger($trigger); | ||
| 398 | print "sub config_measurement: init TRIGGER: " | ||
| 399 | . $self->_init_trigger($trigger) . "\n"; | ||
| 400 | |||
| 401 | return $nplc; | ||
| 402 | |||
| 403 | } | ||
| 404 | |||
| 405 | sub trg { # basic | ||
| 406 | my $self = shift; | ||
| 407 | $self->write("*TRG"); | ||
| 408 | } | ||
| 409 | |||
| 410 | sub abort { # basic | ||
| 411 | my $self = shift; | ||
| 412 | $self->write("ABORT"); | ||
| 413 | } | ||
| 414 | |||
| 415 | sub wait { # basic | ||
| 416 | my $self = shift; | ||
| 417 | my $timeout = shift; | ||
| 418 | |||
| 419 | if ( not defined $timeout ) { | ||
| 420 | $timeout = 100; | ||
| 421 | } | ||
| 422 | |||
| 423 | my $status = Lab::VISA::viSetAttribute( | ||
| 424 | $self->{vi}->{instr}, | ||
| 425 | $Lab::VISA::VI_ATTR_TMO_VALUE, $timeout | ||
| 426 | ); | ||
| 427 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 428 | Lab::Exception::CorruptParameter->throw( | ||
| 429 | error => "Error while setting baud: $status" ); | ||
| 430 | } | ||
| 431 | |||
| 432 | print "waiting for data ... \n"; | ||
| 433 | while (1) { | ||
| 434 | if ( $self->query(":STATUS:OPERATION:CONDITION?") >= 1024 ) { | ||
| 435 | last; | ||
| 436 | } # check if measurement has been finished | ||
| 437 | else { usleep(1e3); } | ||
| 438 | } | ||
| 439 | |||
| 440 | my $status = Lab::VISA::viSetAttribute( | ||
| 441 | $self->{vi}->{instr}, | ||
| 442 | $Lab::VISA::VI_ATTR_TMO_VALUE, 3000 | ||
| 443 | ); | ||
| 444 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 445 | Lab::Exception::CorruptParameter->throw( | ||
| 446 | error => "Error while setting baud: $status" ); | ||
| 447 | } | ||
| 448 | |||
| 449 | } | ||
| 450 | |||
| 451 | sub active { # basic | ||
| 452 | my $self = shift; | ||
| 453 | my ($timeout) = $self->_check_args( \@_, ['value'] ); | ||
| 454 | |||
| 455 | if ( not defined $timeout ) { | ||
| 456 | $timeout = 100; | ||
| 457 | } | ||
| 458 | |||
| 459 | my $status = Lab::VISA::viSetAttribute( | ||
| 460 | $self->{vi}->{instr}, | ||
| 461 | $Lab::VISA::VI_ATTR_TMO_VALUE, $timeout | ||
| 462 | ); | ||
| 463 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 464 | Lab::Exception::CorruptParameter->throw( | ||
| 465 | error => "Error while setting baud: $status" ); | ||
| 466 | } | ||
| 467 | |||
| 468 | # check if measurement has been finished | ||
| 469 | if ( $self->query(":STATUS:OPERATION:CONDITION?") >= 1024 ) { | ||
| 470 | return 0; | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | return 1; | ||
| 474 | } | ||
| 475 | |||
| 476 | my $status = Lab::VISA::viSetAttribute( | ||
| 477 | $self->{vi}->{instr}, | ||
| 478 | $Lab::VISA::VI_ATTR_TMO_VALUE, 3000 | ||
| 479 | ); | ||
| 480 | if ( $status != $Lab::VISA::VI_SUCCESS ) { | ||
| 481 | Lab::Exception::CorruptParameter->throw( | ||
| 482 | error => "Error while setting baud: $status" ); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | sub get_data { # basic | ||
| 487 | my $self = shift; | ||
| 488 | return $self->_read_buffer(); | ||
| 489 | } | ||
| 490 | |||
| 491 | # ------------------------------------ DATA BUFFER ---------------------------------------- | ||
| 492 | |||
| 493 | sub _clear_buffer { # internal | ||
| 494 | my $self = shift; | ||
| 495 | $self->write(":DATA:CLEAR"); | ||
| 496 | return $self->query(":DATA:FREE?"); | ||
| 497 | } | ||
| 498 | |||
| 499 | sub _init_buffer { # internal | ||
| 500 | my $self = shift; | ||
| 501 | my ($nop) = $self->_check_args( \@_, ['value'] ); | ||
| 502 | |||
| 503 | $self->_clear_buffer(); | ||
| 504 | |||
| 505 | if ( $nop >= 2 && $nop <= 1024 ) { | ||
| 506 | $self->write("*CLS"); | ||
| 507 | $self->write(":STATUS:OPERATION:ENABLE 16") | ||
| 508 | ; # enable status bit for measuring/idle status | ||
| 509 | $self->write("INIT:CONT OFF"); # set DMM to IDLE-state | ||
| 510 | $self->_init_trigger("BUS") | ||
| 511 | ; # trigger-count = 1, trigger-delay = 0s, trigger-source = IMM/EXT/TIM/MAN/BUS | ||
| 512 | $self->_set_triggercount(1); | ||
| 513 | $self->_set_triggerdelay(0); | ||
| 514 | my $return_nop = $self->_set_samplecount($nop); | ||
| 515 | $self->write(":INIT"); # set DMM from IDLE to WAIT-FOR_TRIGGER status | ||
| 516 | return $return_nop; | ||
| 517 | } | ||
| 518 | else { | ||
| 519 | Lab::Exception::CorruptParameter->throw( error => | ||
| 520 | "unexpected value in sub set_nop_for_buffer. Must be between 2 and 1024." | ||
| 521 | ); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | sub _read_buffer { # basic | ||
| 526 | my $self = shift; | ||
| 527 | my ($print) = $self->_check_args( \@_, ['value'] ); | ||
| 528 | |||
| 529 | # wait until data are available | ||
| 530 | $self->wait(); | ||
| 531 | |||
| 532 | #read data | ||
| 533 | $self->write(":FORMAT:DATA ASCII; :FORMAT:ELEMENTS READING") | ||
| 534 | ; # select Format for reading DATA | ||
| 535 | my $data = $self->{vi}->LongQuery(":DATA:DATA?"); | ||
| 536 | chomp $data; | ||
| 537 | my @data = split( ",", $data ); | ||
| 538 | |||
| 539 | #print data | ||
| 540 | if ( $print eq "PRINT" ) { | ||
| 541 | foreach my $item (@data) { print $item. "\n"; } | ||
| 542 | } | ||
| 543 | |||
| 544 | return @data; | ||
| 545 | } | ||
| 546 | |||
| 547 | # -------------------------------------- TRIGGER ---------------------------------------------- | ||
| 548 | |||
| 549 | sub _init_trigger { # internal | ||
| 550 | my $self = shift; | ||
| 551 | my ($source) = $self->_check_args( \@_, ['source'] ); | ||
| 552 | |||
| 553 | if ( not defined $source ) { | ||
| 554 | $source = "BUS"; | ||
| 555 | } # set BUS as default trigger source | ||
| 556 | |||
| 557 | $self->_set_triggercount("DEF"); # DEF = 1 | ||
| 558 | $self->_set_triggerdelay("DEF"); # DEF = 0 | ||
| 559 | $self->_set_triggersource("BUS"); | ||
| 560 | |||
| 561 | return "trigger initiated"; | ||
| 562 | |||
| 563 | } | ||
| 564 | |||
| 565 | sub _set_triggersource { # internal | ||
| 566 | my $self = shift; | ||
| 567 | my ($triggersource) = $self->_check_args( \@_, ['source'] ); | ||
| 568 | |||
| 569 | #return setting | ||
| 570 | if ( not defined $triggersource ) { | ||
| 571 | $triggersource = $self->query(":TRIGGER:SOURCE?"); | ||
| 572 | chomp($triggersource); | ||
| 573 | return $triggersource; | ||
| 574 | } | ||
| 575 | |||
| 576 | #set triggersoource | ||
| 577 | if ( $triggersource =~ /\b(IMM|imm|EXT|ext|TIM|tim|MAN|man|BUS|bus)\b/ ) { | ||
| 578 | return $self->query( | ||
| 579 | sprintf( ":TRIGGER:SOURCE %s; SOURCE?", $triggersource ) ); | ||
| 580 | } | ||
| 581 | else { | ||
| 582 | Lab::Exception::CorruptParameter->throw( error => | ||
| 583 | "unexpected value for SOURCE in sub _init_trigger. Must be IMM, EXT, TIM, MAN or BUS." | ||
| 584 | ); | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 588 | sub _set_samplecount { # internal | ||
| 589 | my $self = shift; | ||
| 590 | my ($samplecount) = $self->_check_args( \@_, ['value'] ); | ||
| 591 | |||
| 592 | #return setting | ||
| 593 | if ( not defined $samplecount ) { | ||
| 594 | $samplecount = $self->query(":SAMPLE:COUNT?"); | ||
| 595 | chomp($samplecount); | ||
| 596 | return $samplecount; | ||
| 597 | } | ||
| 598 | |||
| 599 | #set samplecount | ||
| 600 | if ( $samplecount >= 1 && $samplecount <= 1024 ) { | ||
| 601 | return $self->query( | ||
| 602 | sprintf( ":SAMPLE:COUNT %d; COUNT?", $samplecount ) ); | ||
| 603 | } | ||
| 604 | else { | ||
| 605 | Lab::Exception::CorruptParameter->throw( error => | ||
| 606 | "unexpected value for SAMPLECOUNT in sub _set_samplecount. Must be between 1 and 1024." | ||
| 607 | ); | ||
| 608 | } | ||
| 609 | |||
| 610 | } | ||
| 611 | |||
| 612 | sub _set_triggercount { # internal | ||
| 613 | my $self = shift; | ||
| 614 | my ($triggercount) = $self->_check_args( \@_, ['value'] ); | ||
| 615 | |||
| 616 | #return setting | ||
| 617 | if ( not defined $triggercount ) { | ||
| 618 | $triggercount = $self->query(":TRIGGER:COUNT?"); | ||
| 619 | chomp($triggercount); | ||
| 620 | return $triggercount; | ||
| 621 | } | ||
| 622 | |||
| 623 | #set triggercount | ||
| 624 | if ( ( $triggercount >= 1 && $triggercount <= 1024 ) | ||
| 625 | or $triggercount =~ /\b(MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 626 | return $self->query(":TRIGGER:COUNT $triggercount; COUNT?"); | ||
| 627 | } | ||
| 628 | else { | ||
| 629 | Lab::Exception::CorruptParameter->throw( error => | ||
| 630 | "unexpected value for TRIGGERCOUNT in sub _set_triggercount. Must be between 1 and 1024 or MIN/MAX/DEF." | ||
| 631 | ); | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | sub _set_triggerdelay { # internal | ||
| 636 | my $self = shift; | ||
| 637 | my ($triggerdelay) = $self->_check_args( \@_, ['value'] ); | ||
| 638 | |||
| 639 | #return setting | ||
| 640 | if ( not defined $triggerdelay ) { | ||
| 641 | $triggerdelay = $self->query(":TRIGGER:DELAY?"); | ||
| 642 | chomp($triggerdelay); | ||
| 643 | return $triggerdelay; | ||
| 644 | } | ||
| 645 | |||
| 646 | #set triggerdelay | ||
| 647 | if ( ( $triggerdelay >= 0 && $triggerdelay <= 999999.999 ) | ||
| 648 | or $triggerdelay =~ /\b(MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 649 | return $self->query(":TRIGGER:DELAY $triggerdelay; DELAY?"); | ||
| 650 | } | ||
| 651 | else { | ||
| 652 | Lab::Exception::CorruptParameter->throw( error => | ||
| 653 | "unexpected value for TRIGGERDELAY in sub _set_triggerdelay. Must be between 0 and 999999.999sec or MIN/MAX/DEF." | ||
| 654 | ); | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | sub set_timer { # advanced | ||
| 659 | my $self = shift; | ||
| 660 | my ($timer) = $self->_check_args( \@_, ['value'] ); | ||
| 661 | |||
| 662 | #return setting | ||
| 663 | if ( not defined $timer ) { | ||
| 664 | $timer = $self->query(":TRIGGER:TIMER?"); | ||
| 665 | chomp($timer); | ||
| 666 | return $timer; | ||
| 667 | } | ||
| 668 | |||
| 669 | #set timer | ||
| 670 | if ( ( $timer >= 1e-3 && $timer <= 999999.999 ) | ||
| 671 | or $timer =~ /\b(MIN|min|MAX|max|DEF|def)\b/ ) { | ||
| 672 | return $self->query(":TRIGGER:TIMER $timer; TIMER?"); | ||
| 673 | } | ||
| 674 | else { | ||
| 675 | Lab::Exception::CorruptParameter->throw( error => | ||
| 676 | "unexpected value for TIMER in sub set_timer. Must be between 0 and 999999.999sec or MIN/MAX/DEF." | ||
| 677 | ); | ||
| 678 | } | ||
| 679 | } | ||
| 680 | |||
| 681 | # -----------------------------------------DISPLAY -------------------------------- | ||
| 682 | |||
| 683 | sub display { # basic | ||
| 684 | my $self = shift; | ||
| 685 | my ($data) = $self->_check_args( \@_, ['state'] ); | ||
| 686 | |||
| 687 | if ( not defined $data ) { | ||
| 688 | return $self->_display_text(); | ||
| 689 | } | ||
| 690 | elsif ( $data =~ /\b(ON|on)\b/ ) { | ||
| 691 | return $self->_display_on(); | ||
| 692 | } | ||
| 693 | elsif ( $data =~ /\b(OFF|off)\b/ ) { | ||
| 694 | return $self->_display_off(); | ||
| 695 | } | ||
| 696 | elsif ( $data =~ /\b(CLEAR|clear)\b/ ) { | ||
| 697 | return $self->_display_clear(); | ||
| 698 | } | ||
| 699 | else { | ||
| 700 | return $self->_display_text($data); | ||
| 701 | } | ||
| 702 | |||
| 703 | } | ||
| 704 | |||
| 705 | sub display_on { # for internal/advanced use only | ||
| 706 | my $self = shift; | ||
| 707 | $self->write(":DISPLAY:ENABLE ON"); | ||
| 708 | } | ||
| 709 | |||
| 710 | sub display_off { # for internal/advanced use only | ||
| 711 | my $self = shift; | ||
| 712 | $self->write(":DISPLAY:ENABLE OFF") | ||
| 713 | ; # when display is disabled, the instrument operates at a higher speed. Frontpanel commands are frozen. | ||
| 714 | } | ||
| 715 | |||
| 716 | sub display_text { # for internal/advanced use only | ||
| 717 | my $self = shift; | ||
| 718 | my ($text) = $self->_check_args( \@_, ['text'] ); | ||
| 719 | |||
| 720 | if ($text) { | ||
| 721 | chomp( $text | ||
| 722 | = $self->query("DISPLAY:TEXT:DATA '$text'; STATE 1; DATA?") ); | ||
| 723 | $text =~ s/\"//g; | ||
| 724 | return $text; | ||
| 725 | } | ||
| 726 | else { | ||
| 727 | chomp( $text = $self->query("DISPLAY:TEXT:DATA?") ); | ||
| 728 | $text =~ s/\"//g; | ||
| 729 | return $text; | ||
| 730 | } | ||
| 731 | } | ||
| 732 | |||
| 733 | sub display_clear { # for internal/advanced use only | ||
| 734 | my $self = shift; | ||
| 735 | $self->write("DISPlay:TEXT:STATE 0"); | ||
| 736 | } | ||
| 737 | |||
| 738 | # ---------------------------------------------------------------------------------------- | ||
| 739 | |||
| 740 | sub beep { | ||
| 741 | my $self = shift; | ||
| 742 | $self->write("BEEP"); | ||
| 743 | } | ||
| 744 | |||
| 745 | 1; | ||
| 746 | |||
| 747 | __END__ |