pythonpyvisagpib

How to obtain a floating point result from a query using SCPI


I've been stuck for a time now trying to get a real(floating point query) power measurement from a peak search(marker) on a FSW Rohde & Schwarz Spectrum Analyzer through Pyvisa. I tried almost every command mentioned in the user manual but I can only manage to obtain an integer value and when I print it in my VSCode terminal I can only see 21 dBm instead of 21.xx dBm.

I also tried to convert it to float data type but it's not working, do I need to configure something on the instrument or am I missing something else?

fsw = rm.open_resource("GPIB::15::INSTR")
fsw.write("DISP:TRAC ON")
fsw.write("*rst; status:preset; *cls")
fsw.write("SENS:FREQ:CENTER %d MHZ" %fc)
fsw.write("SENS:FREQ:SPAN %d MHZ" %span)
fsw.write("INP:ATT 20dB")
fsw.write("FSW,'SENS:BAND:RES %d HZ" %resBW)
fsw.write("SENSE:MARK:MAX:PEAK")
fsw.write("INIT:CONT:OFF")
fsw.write("CALC:MARK:FUNC:FPE:STAT ON")
fsw.write("CALC:MARK:FUNC:FPE:SORT Y")
print(fsw.write("SENS:MARK:MAX:PEAK?"), 'dBm')

Solution

  • The last line of your code example seems to be wrong. The PyVISA write(command) function only writes the SCPI command to the device, but doesn't read the results. Instead, you should use the query(command) function. It returns the value sent back from your instrument as a string. If you print that string, you will see exactly what the instrument sent back. If this string doesn't contain decimal places, it is an issue with your instrument configuration and you might have to configure the measurement resolution of your spectrum analyzer.

    To convert the string value to a float, just initialize a float witht the string value, eg.

    dbm_value = float(fsw.query("SENS:MARK:MAX:PEAK?"))
    print(f"Measured value: {dbm_value}")