1import visa import time ### Agilent N9340B spectrum analyzer instrument_descriptor = 'USB0::0x0957::0xFFEF::CN03490905::INSTR' AFG = visa.instrument(instrument_descriptor) ### I/O timeout for exception raising AFG.timeout = 100 print AFG.ask('*IDN?') ### Reset and clear the AFG status AFG.write('*rst') AFG.write('*cls') ### Set the analyzer center frequency (Hz) AFG.write('SENS:FREQ:CENT 1e6') ### Set SPAN AFG.write('SENS:FREQ:SPAN 2e6') ### Start Freq AFG.write('SENS:FREQ:START 750kHz') ### Stop Freq AFG.write('SENS:FREQ:STOP 970kHz') ### RBW AFG.write('SENS:BAND:RES:AUTO 0') AFG.write('SENS:BAND:RES 1kHz') ### VBW AFG.write('SENS:BAND:VID:AUTO 0') AFG.write('SENS:BAND:VID 300Hz') ### AMPTD AFG.write('SENS:POW:ATT:AUTO 0') AFG.write('SENS:POW:ATT 0dB') ### Ref Level AFG.write('DISPlay:WINDow:TRACe:Y:RLEVel -30dBm') ### Set screen Scale AFG.write('DISPlay:WINDow:TRACe:Y:PDIV 10') ### Set averaging AFG.write('SENSe:AVERage:TRACe1:COUNt 2') ### Turn on averaging AFG.write('SENSe:AVERage:TRACe1:STATe 1') ### Measure loop i = 0 while True: ### Continuous <1> or Single Sweep <0> ### AFG.write('INIT:CONT 0') ### Run measurement immediately AFG.write('INIT:IMM') ### This command causes the instrument to wait until ### all pending commands are completed before ### executing any additional commands AFG.write('*WAI') ### 6 peaks search AFG.write('CALCulate:MARKer:ALL') ### Query and read the marker1 frequency [Hz] print AFG.ask('CALC:MARK1:X?') ### Query and read the marker1 amplitude [dBm] print AFG.ask('CALC:MARK1:Y?') ### Restart averaging AFG.write('SENSe:AVERage:TRACe1:CLEar') ### Number of loop execution times i = i + 1 if ( i > 3): break