| 1 | package Lab::Instrument::U2000; | ||
| 2 | #ABSTRACT: Agilent U2000 series USB Power Sensor | ||
| 3 | $Lab::Instrument::U2000::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::Instrument; | ||
| 8 | |||
| 9 | our @ISA = ("Lab::Instrument"); | ||
| 10 | |||
| 11 | our %fields = ( | ||
| 12 | supported_connections => ['USBtmc'], | ||
| 13 | |||
| 14 | connection_settings => { | ||
| 15 | usb_vendor => "0957", | ||
| 16 | usb_product => "2a18" | ||
| 17 | }, | ||
| 18 | |||
| 19 | device_settings => { | ||
| 20 | frequency => 10e6, | ||
| 21 | }, | ||
| 22 | |||
| 23 | ); | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $proto = shift; | ||
| 27 | my $class = ref($proto) || $proto; | ||
| 28 | my $self = $class->SUPER::new(@_); | ||
| 29 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 30 | $self->clear(); | ||
| 31 | |||
| 32 | #TODO: Device clear | ||
| 33 | $self->write("SYST:PRES"); # Load presets | ||
| 34 | $self->{trigger_mode} = "AUTO"; | ||
| 35 | $self->{average_on} = "1"; | ||
| 36 | return $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | # template functions for inheriting classes | ||
| 40 | |||
| 41 | sub id { | ||
| 42 | my $self = shift; | ||
| 43 | return $self->query('*IDN?'); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub selftest { | ||
| 47 | my $self = shift; | ||
| 48 | return $self->query("*TST"); | ||
| 49 | } | ||
| 50 | |||
| 51 | #TODO: EXT mode not tested | ||
| 52 | sub set_trigger { | ||
| 53 | my $self = shift; | ||
| 54 | my $type = shift || "AUTO"; #AUTO, BUS, INT, EXT, IMM | ||
| 55 | my $args; | ||
| 56 | if ( ref $_[0] eq 'HASH' ) { $args = shift } | ||
| 57 | else { $args = {@_} } | ||
| 58 | my $delay = $args->{'delay'}; #AUTO, MIN, MAX, DEF, -0.15s to +0.15s | ||
| 59 | my $level = $args->{'level'}; #DEF, MIN, MAX, sensor dependent range in dB | ||
| 60 | my $hysteresis = $args->{'hysteresis'}; #DEF, MIN, MAX, 0 to 3dB | ||
| 61 | my $holdoff = $args->{'holdoff'}; #DEF, MIN, MAX, 1µs to 400ms | ||
| 62 | my $slope = $args->{'edge'}; #POS, NEG | ||
| 63 | |||
| 64 | if ( $type eq "AUTO" ) { | ||
| 65 | $self->write("INIT:CONT ON"); | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | $self->write("INIT:CONT OFF"); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ( $self->{average_on} && ( $type eq "INT" || $type eq "EXT" ) ) { | ||
| 72 | Lab::Exception::CorruptParameter->throw( error => | ||
| 73 | "Can't switch to internal or external trigger while average mode is on. Change mode using set_mode(\"NORM\"). Error in U2000::set_trigger(). \n" | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | if ( $type eq "INT" || $type eq "EXT" || $type eq "IMM" ) { | ||
| 77 | $self->write("TRIG:SOUR $type"); | ||
| 78 | $self->{trigger_mode} = $type; | ||
| 79 | } | ||
| 80 | elsif ( $type eq "BUS" ) { | ||
| 81 | Lab::Exception::CorruptParameter->throw( error => | ||
| 82 | "'BUS' trigger mode is not supported by this library in U2000::set_trigger()\n" | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | elsif ( $type ne "AUTO" ) { | ||
| 86 | Lab::Exception::CorruptParameter->throw( | ||
| 87 | error => "Unknown trigger mode in HP34401A::set_trigger()\n" ); | ||
| 88 | } | ||
| 89 | |||
| 90 | if ( defined($delay) ) { | ||
| 91 | if ( $delay eq "AUTO" ) { | ||
| 92 | $self->write("TRIG:DEL:AUTO ON"); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | $self->write("TRIG:DEL:AUTO OFF"); | ||
| 96 | $self->write("TRIG:DEL $delay"); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | if ( defined($holdoff) ) { | ||
| 101 | $self->write("TRIG:HOLD $holdoff"); | ||
| 102 | } | ||
| 103 | |||
| 104 | if ( defined($level) ) { | ||
| 105 | $self->write("TRIG:LEV $level"); | ||
| 106 | } | ||
| 107 | |||
| 108 | if ( defined($hysteresis) ) { | ||
| 109 | $self->write("TRIG:HYST $hysteresis"); | ||
| 110 | } | ||
| 111 | |||
| 112 | if ( defined($slope) ) { | ||
| 113 | $self->write("TRIG:SLOP $slope"); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub set_power_unit { | ||
| 118 | my $self = shift; | ||
| 119 | my $unit = shift || "DBM"; #DBM, W | ||
| 120 | $self->write("UNIT:POW $unit"); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub set_average { | ||
| 124 | my $self = shift; | ||
| 125 | my $count = shift || "AUTO"; #OFF, AUTO, DEF, MIN, MAX, 1 to 1024 | ||
| 126 | |||
| 127 | if ( $count eq "OFF" ) { | ||
| 128 | $self->write("AVER OFF"); | ||
| 129 | $self->set_mode("NORM"); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | $self->write("AVER ON"); | ||
| 134 | $self->set_mode("AVER"); | ||
| 135 | } | ||
| 136 | |||
| 137 | if ( $count eq "AUTO" ) { | ||
| 138 | $self->write("AVER:COUN:AUTO ON"); | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | #Automatic averaging is disabled by this command | ||
| 142 | $self->write("AVER:COUN $count"); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub set_mode { | ||
| 147 | my $self = shift; | ||
| 148 | my $mode = shift || "AVER"; | ||
| 149 | |||
| 150 | if ( $mode eq "AVER" ) { | ||
| 151 | $self->{trigger_mode} = "AUTO"; | ||
| 152 | } | ||
| 153 | elsif ( $mode eq "NORM" ) { | ||
| 154 | if ( $self->{trigger_mode} eq "IMM" ) { | ||
| 155 | $self->{trigger_mode} = "INT"; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | Lab::Exception::CorruptParameter->throw( | ||
| 160 | error => "Unknown mode in HP34401A::set_mode()\n" ); | ||
| 161 | } | ||
| 162 | $self->{average_on} = $mode eq "AVER"; | ||
| 163 | $self->write("DET:FUNC $mode"); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_step_detect { | ||
| 167 | my $self = shift; | ||
| 168 | my $state = shift || "ON"; # ON, OFF | ||
| 169 | $self->write("AVER:SDET $state"); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub set_frequency { | ||
| 173 | my $self = shift; | ||
| 174 | my $freq = shift || "DEF"; # DEF, MIN, MAX, 1kHz to 1000GHz | ||
| 175 | $self->write("FREQ $freq"); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub set_sample_rate { | ||
| 179 | my $self = shift; | ||
| 180 | my $rate = shift || "NORM"; # MIN, MAX, NORM, DOUBLE, FAST, 1-110 | ||
| 181 | if ( $rate =~ /(NORM,DOUB, FAST)/ ) { | ||
| 182 | $self->write("MRAT $rate"); | ||
| 183 | } | ||
| 184 | elsif ( $rate eq "MIN" || $rate <= 20 ) { | ||
| 185 | $self->write("MRAT NORM"); | ||
| 186 | } | ||
| 187 | elsif ( $rate <= 40 ) { | ||
| 188 | $self->write("MRAT DOUB"); | ||
| 189 | } | ||
| 190 | elsif ( $rate eq "MAX" || $rate <= 110 ) { | ||
| 191 | $self->write("MRAT FAST"); | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | Lab::Exception::CorruptParameter->throw( error => | ||
| 195 | "Unsuppoerted sample rate in HP34401A::set_sample_rate()\n" ); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | #TODO: Device hangs after a read has timed out and a new read was | ||
| 200 | # issued during which the trigger condition is satisifed. | ||
| 201 | # (in INT trigger mode and possibly others as well) | ||
| 202 | sub read { | ||
| 203 | my $self = shift; | ||
| 204 | if ( $self->{trigger_mode} eq "AUTO" ) { | ||
| 205 | |||
| 206 | #No trigger needed for AUTO MODE | ||
| 207 | return $self->query("FETC?"); | ||
| 208 | } | ||
| 209 | elsif ( $self->{trigger_mode} eq "IMM" ) { | ||
| 210 | |||
| 211 | #Automatically send trigger for immediate mode | ||
| 212 | return $self->query("READ?"); | ||
| 213 | } | ||
| 214 | |||
| 215 | #TODO: Check other modes | ||
| 216 | return $self->query("READ?"); | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | sub get_error { | ||
| 221 | my $self = shift; | ||
| 222 | my $current_error = ""; | ||
| 223 | my $all_errors = ""; | ||
| 224 | my $max_errors = 5; | ||
| 225 | while ( $max_errors-- ) { | ||
| 226 | $current_error = $self->query('SYST:ERR?'); | ||
| 227 | if ( $current_error eq "" ) { | ||
| 228 | $all_errors .= "Could not read error message!\n"; | ||
| 229 | last; | ||
| 230 | } | ||
| 231 | if ( $current_error =~ m/^\+?0,/ ) { last; } | ||
| 232 | $all_errors .= $current_error . "\n"; | ||
| 233 | } | ||
| 234 | if ( !$max_errors ) { $all_errors .= "Maximum Error count reached!\n"; } | ||
| 235 | $self->write("*CLS"); #Clear errors | ||
| 236 | chomp($all_errors); | ||
| 237 | return $all_errors; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | __END__ |