'''

Created on 2022/02

Author: Jahns_P
Version Number: 1
Date of last change: 2022/02/03
Requires: HMS-X, FW 02.250 or newer
- Installed RsInstrument Python module (see the attached RsInstrument_PythonModule folder Readme.txt)
- Installed VISA e.g. R&S Visa 5.12.x or newer

Description: Example about how to use the receiver mode


General Information:

Please always check this example script for unsuitable setting that may
destroy your Instrument / DUT before connecting it!
This example does not claim to be complete. All information has been
compiled with care. However errors can not be ruled out. 
'''

from RsInstrument import *
from time import sleep

'''Define Variables if needed'''
x = 0 # for counter purposes
response = "" # standard variable for queries
resource = 'ASRL10::INSTR'  # Assign Intrument VISA resource string


'''Check for RsInstrument version and stop if version is less than 1.19'''
RsInstrument.assert_minimum_version("1.19")


'''
The following initialisation has these functions included:
- Resource = assignment of the VISA resource string to Instrument 
- Perform no ID query 
- Perform no Reset on the instrument 
- Options:
- Set Termination character for read and write operations to \R, (ASCI 13 or Carriage Return)
- Enable Logging Mode
- Set Write delay to 10 ms
- Set Read delay to 10 ms
'''
Instrument = RsInstrument(resource, False, False,
                          options="TerminationCharacter = '\n',LoggingMode=On, WriteDelay = " \
                                  "10, ReadDelay = 10 ")

def comprep():
    """ For standard VISA VCP Port communication first """

    Instrument.logger.log_to_console = True # The Log is sent to the console
    Instrument.instrument_status_checking = False # Error check after each command MUST BE OFF for this instrument
    Instrument.visa_timeout = 6000 # Timeout for VISA Read Operations in ms
    Instrument.opc_timeout = 3000 # Timeout for opc-synchronised operations in ms
    Instrument.clear_status() # Clear status register

def comcheck():
    """ Do the first readout and get instrument data """

    idn = Instrument.query_str('*IDN?')
    print(f"\nHello, I am: '{idn}'")                                                # Identification String
    print(f'Visa manufacturer: {Instrument.visa_manufacturer}')
    print(f'Instrument full name: {Instrument.full_instrument_model_name}')



def measprep():
    """ Prepare the measurement setup """
    Instrument.reset()

    #
    # Important!
    # Rx mode is not completely documented in the manual.
    # A complete command list can be requested by querying "SYSTEM:TREE?"
    #

    Instrument.write_str_with_opc('SYST:MODE RMOD') # Change over to Rx mode
    Instrument.write_str_with_opc('RMOD:DET PEAK') # Set Rx mode detector --> Possible values: PEAK, AVG, QPEAK, RMS
    Instrument.write_str_with_opc('RMOD:MTIM 0.5') # Rx mode measurement time in seconds (can not be set for QPEAK)
    Instrument.write_str_with_opc('RMOD:FREQ 100 MHz') # Set receiver frequency
    Instrument.write_str_with_opc('RMOD:FREQ:STEP 100000') # Set step width for Rx mode (will only help for manual purposes.
                                                           # Rather use RMOD:FREQ for a scan.
    Instrument.write_str_with_opc('Band:RBW C120K') # Set bandwidth to CISPR 120 kHz
                                                    # --> Possible values: 100 / 300 Hz, 1 / 3 / 10 / 30 / 100 / 200 / 300 kHz,
                                                    # 1 MHz, C200, C9K, C120K, C1M

    sleep(1)
    #
    # Important!
    # While demodulation is switched on, measurement will not take place.
    #
    #Instrument.write_str_with_opc('RMOD:AUD:DEM ON') # Switch on demodulation (headphone port) --> Possible values: ON, OFF
    #Instrument.write_str_with_opc('RMOD:AUD:MOD FM') # Change demod to FM --> Possible values: AM, FM
    #Instrument.write_str_with_opc('RMOD:AUD:VOL 50') # Adjust audio volume for headphone port --> possible values: 0-100



def close():
    """ Close the VISA session """
    Instrument.close()


def readout():
    """ Readout of the measurement data """

    response = Instrument.query_str('RMOD:DAT?') # Request measurement data --> Is same result like requesting with RMOD:LEV
    print ('\nThe result of the measurement in dBm is ', response, '\n')

    Instrument.write_str_with_opc('AMPL:UNIT DBUV') # Change the display unit --> Possible values: DBM, DBUV
    sleep(1)
    response = Instrument.query_str('RMOD:DAT?') # Request measurement data --> Is same result like requesting with RMOD:LEV
    print ('\nThe result of the measurement in dBuV is ', response, '\n')



########################
#
# Program begins here
#
########################


comprep()
comcheck()
measprep()
readout()
close()



