More complex scenarios imply using Python in non-interactive mode.
Here is an example showing very basic usage of python programming language to control the instrument.
#!/usr/bin/env python
# REPLACE WITH THE IP ADDRESS OR YOUR INSTRUMENT!
# (See Settings -> Settings -> User options -> Network -> Ethernet IP address)
INSTRUMENT_IP = '192.168.0.25'
import pyvisa as visa
rm = visa.ResourceManager()
def example(resource_str):
instr = rm.open_resource(resource_str) # Connect to the instrument
instr.timeout = 5000 # VISA I/O operations timeout, in milliseconds
idn = instr.query('*IDN?') # Send '*IDN?' and read response.
print('Running example 1 with {} at {}'.format(idn.strip(), resource_str))
print('In this example frequency measurement is performed on input A')
instr.write('*RST;*CLS') # Reset to default settings, clear error and message queues.
# Configure the measurement: simple frequency measurement, with timeout.
# (Pay attention to double and single quotes)
instr.write(':SYSTEM:CONFIGURE "Function=Frequency A; SampleCount=10; SampleInterval=0.01; '
'Timeout=On; TimeoutTime=1.0"')
# It's a good practice to check for errors.
err = instr.query(':SYST:ERR?')
if err.strip() != '0,"No error"':
raise Exception('Error configuring measurement: {}'.format(err))
instr.write(':INIT')
# Waiting (in a blocking manner) for measurement completion.
# *OPC? query responds back only when all pending operations (measurement in this case) are completed.
# Measurement will be complete when all requested samples are collected or if timeout occurs.
instr.query('*OPC?') # No need to use response here, because it is always '1' by SCPI standard.
data_str = instr.query(':FETCH:ARRAY? MAX, A') # Will return a string of comma-separated numbers
data_str = data_str.strip() # to remove \n at the end
if len(data_str) > 0:
data = list(map(float, data_str.split(','))) # Convert the string to python array
else:
data = []
# Display measurement results
print('Results: {}'.format(data if data else 'no data (signal not connected?)'))
instr.close()
if __name__ == '__main__':
resource_str = 'TCPIP::{}::hislip0::INSTR'.format(INSTRUMENT_IP)
try:
example(resource_str)
except visa.VisaIOError as e:
print('Error occurred: {}'.format(e))