demo3/visa-scpi-example/File.py
2024-12-04 20:27:46 +08:00

110 lines
3.1 KiB
Python

# python script created by FSW: 04:12:2024 14:23:54
import pyvisa as visa
def write_command(instrument, command) :
instrument.write(command)
return process_system_error(instrument)
def write_query(instrument, command) :
buffer = instrument.query(command)
bSuccess = process_system_error(instrument)
return bSuccess, buffer
def process_system_error(instrument) :
bSuccess = True
EsrErrorMask = 0x3C
if ((get_esr(instrument) & EsrErrorMask) != 0) :
print(instrument.query(":SYST:ERR?"))
instrument.write("*CLS")
bSuccess = False
return bSuccess
def get_esr(instrument) :
esr = instrument.query("*ESR?")
return int(esr)
VisaResourceManager = visa.ResourceManager()
# connect to analyzer
Analyzer = VisaResourceManager.open_resource("TCPIP::169.254.177.77::inst0::INSTR")
success = write_command( Analyzer, "*RST" )
success = write_command( Analyzer, "*CLS" )
success = write_command( Analyzer, ":SYST:DISP:UPD ON" )
success = write_command( Analyzer, ":INIT:CONT OFF" )
success = write_command( Analyzer, ":SENS:FREQ:STAR 1000000000" )
success = write_command( Analyzer, ":SENS:FREQ:STOP 2000000000" )
success = write_command( Analyzer, ":DISP:WIND:TRAC:Y:SCAL:RLEV -30" )
success = write_command( Analyzer, ":DISP:WIND:TRAC:Y:SCAL:RLEV 30" )
success = write_command( Analyzer, ":SENS:BAND:RES:AUTO OFF" )
success = write_command( Analyzer, ":SENS:BAND:RES:AUTO ON" )
success = write_command( Analyzer, ":SENS:BAND:VID:AUTO OFF" )
success = write_command( Analyzer, ":SENS:BAND:VID:AUTO ON" )
success = write_command( Analyzer, ":SENS:FREQ:MODE SWE" )
success = write_command( Analyzer, ":DISP:WIND1:SIZE LARG" )
success = write_command( Analyzer, ":CALC1:MARK1:FUNC:STR:STAT ON" )
success = write_command( Analyzer, ":CALC1:MARK1:FUNC:STR:STAT OFF" )
success = write_command( Analyzer, ":CALC:MARK:FUNC:POW:SEL CN" )
success = write_command( Analyzer, ":SENS:POW:ACH:BAND:CHAN 300000" )
success = write_command( Analyzer, ":SENS:POW:ACH:PRES CN" )
success = write_command( Analyzer, ":CALC:MARK1:STAT ON" )
success = write_command( Analyzer, ":CALC:MARK1:X 1200000000" )
success = write_command( Analyzer, ":CALC:MARK:FUNC:POW:SEL CN" )
success = write_command( Analyzer, ":SENS:POW:ACH:PRES CN" )
success = write_command( Analyzer, ":SENS:POW:ACH:PRES CN" )
success = write_command( Analyzer, ":CALC:MARK:FUNC:POW:SEL CN0" )
success = write_command( Analyzer, ":CALC:MARK:FUNC:POW:SEL CN" )
success = write_command( Analyzer, ":SENS:FREQ:SPAN 1000000" )
success = write_command( Analyzer, ":SENS:FREQ:SPAN 1000000000" )
success = write_command( Analyzer, ":CALC:MARK1:STAT ON" )
success = write_command( Analyzer, ":CALC:MARK1:X 1200000000" )
success = write_command( Analyzer, ":CALC:DELT2:STAT ON" )
success = write_command( Analyzer, ":CALC:DELT2:X 1600000000" )
success = write_command( Analyzer, ":INIT:SPUR" )
success = write_command( Analyzer, ":MMEM:NAME 'C:\R_S\Instr\user\FSW_ScreenShot_2024-12-04_14-22-12.PNG'" )
success = write_command( Analyzer, ":HCOP:IMM1" )
# back to local mode
success = write_command(Analyzer, "@LOC")
# cleanup
Analyzer.close()
VisaResourceManager.close()