Examples

For more examples, visit our Rohde & Schwarz Github repository.

"""Getting started - how to work with RsSgt Python package.
This example performs basic RF settings on an SGT100A instrument.
It shows the RsSgt calls and their corresponding SCPI commands.
Notice that the python RsSgt interfaces track the SCPI commands syntax."""

from RsSgt import *

# Open the session
sgt = RsSgt('TCPIP::10.112.0.228::HISLIP')
# Greetings, stranger...
print(f'Hello, I am: {sgt.utilities.idn_string}')

#   OUTPut:STATe ON
sgt.output.state.set_value(True)

#   SOURce:POWer:LEVel:IMMediate:AMPLitude -25
sgt.source.power.level.immediate.set_amplitude(-25)

#   SOURce:FREQuency:FIXed 1100000000
sgt.source.frequency.fixed.set_value(1.1E9)

#         SOURce:POWer:PEP?
pep = sgt.source.power.get_pep()
print(f'PEP level: {pep} dBm')

# Close the session
sgt.close()
"""Basic example of importing the package, initializing the session and performing basic generator settings."""

from RsSgt import *


sgt = RsSgt('TCPIP::10.112.1.73::HISLIP')
# sgt = RsSgt('TCPIP::10.214.1.57::5025::SOCKET', options='SelectVisa=SocketIo') # No VISA needed
print(f'Driver Info: {sgt.utilities.driver_version}')
print(f'Instrument: {sgt.utilities.idn_string}')

# Instrument options are properly parsed, and sorted (k-options first)
print(f'Instrument options: {",".join(sgt.utilities.instrument_options)}')

# Driver's instrument status checking ( SYST:ERR? ) after each command (default value is True):
sgt.utilities.instrument_status_checking = True

sgt.output.state.set_value(True)
sgt.source.power.level.immediate.set_amplitude(-20)
sgt.source.frequency.fixed.set_value(223E6)
print(f'PEP level: {sgt.source.power.get_pep()} dBm')

# You can still use the direct SCPI interface:
response = sgt.utilities.query_str('*IDN?')
print(f'Direct SCPI response on *IDN?: {response}')
sgt.close()
"""The example:
 - creates waveform file from a csv-file with I/Q pairs
 - sends the file to the SGT100A instrument
 - activates the waveform
 You have the option of auto-scaling the samples to the full range with the parameter 'auto_scale'
"""

import numpy as np
from RsSgt import *

RsSgt.assert_minimum_version('4.70.1')
sgt = RsSgt('TCPIP::10.214.1.57::HISLIP')
print(sgt.utilities.idn_string)
sgt.utilities.reset()

pc_csv_file = r'c:\temp\arbFileExample.csv'
pc_wv_file = r'c:\temp\arbFileExample.wv'
instr_wv_file = '/var/user/InstrDemoFile.wv'

# Skip this part if you have a csv-file available

# Samples clock
clock_freq = 600e6
# wave clock
wave_freq = 120e6
# Scale factor - change it to less or more that 1 if you want to see the autoscaling capability of the create_waveform_file...() methods
scale_factor = 0.43
time_vector = np.arange(0, 50 / wave_freq, 1 / clock_freq)
# I-component an Q-component data
i_data = np.cos(2 * np.pi * wave_freq * time_vector) * scale_factor
q_data = np.sin(2 * np.pi * wave_freq * time_vector) * scale_factor

with open(pc_csv_file, 'w') as file:
    for x in range(len(i_data)):
        file.write(f'{i_data[x]},{q_data[x]}\n')

# Take that csv-file with the IQ-samples and create a wv file
result = sgt.arb_files.create_waveform_file_from_samples_file(pc_csv_file, pc_wv_file, clock_freq=100E6, auto_scale=False, comment='Created from a csv file')
print(result)

# Send to the instrument
sgt.arb_files.send_waveform_file_to_instrument(pc_wv_file, instr_wv_file)

# Selecting the waveform and load it in ARB
sgt.source.bb.arbitrary.waveform.set_select(instr_wv_file)

# Turning on the ARB baseband
sgt.source.bb.arbitrary.set_state(True)

# Turning on the RF output state state
sgt.output.state.set_value(True)

sgt.close()
"""The example:
 - creates waveform file from two i_data and q_data vectors
 - sends the file to the SGT100A instrument
 - activates the waveform
 You have the option of auto-scaling the samples to the full range with the parameter 'auto_scale'
"""

import numpy as np
from RsSgt import *

RsSgt.assert_minimum_version('4.70.1')
sgt = RsSgt('TCPIP::10.214.1.57::HISLIP')
print(sgt.utilities.idn_string)
sgt.utilities.reset()

pc_wv_file = r'c:\temp\arbFileExample.wv'
instr_wv_file = '/var/user/InstrDemoFile.wv'

# Creating the I/Q vectors as lists: i_data / q_data
# Samples clock
clock_freq = 600e6
# wave clock
wave_freq = 120e6
# Scale factor - change it to less or more that 1 if you want to see the autoscaling capability of the create_waveform_file...() methods
scale_factor = 0.8
time_vector = np.arange(0, 50 / wave_freq, 1 / clock_freq)
# I-component an Q-component data
i_data = np.cos(2 * np.pi * wave_freq * time_vector) * scale_factor
q_data = np.sin(2 * np.pi * wave_freq * time_vector) * scale_factor

# Take those samples and create a wv file, send it to the instrument with the name instr_wv_file (not auto-scaled)
result = sgt.arb_files.create_waveform_file_from_samples(i_data, q_data, pc_wv_file, clock_freq=100E6, auto_scale=False, comment='Created from I/Q vectors')
sgt.arb_files.send_waveform_file_to_instrument(pc_wv_file, instr_wv_file)

# Selecting the waveform and load it in the ARB
sgt.source.bb.arbitrary.waveform.set_select(instr_wv_file)
sgt.source.frequency.fixed.set_value(1.1E9)
sgt.source.power.level.immediate.set_amplitude(-11.1)
# Turning on the ARB baseband
sgt.source.bb.arbitrary.set_state(True)
# Turning on the RF out state
sgt.output.state.set_value(True)

sgt.close()
"""Example showing how you can transfer a big file to the instrument and from the instrument with showing the progress.
Since the SGT100A is quite fast on data transfer, we slow it down by waiting for 100ms between each chunk transfer (1MB)
This way we see the transfer progress better and we do not need a file that is so big - let's take cca 20MB.
For big files, use the example without the time.sleep(0.1)"""

import time
import numpy as np
from RsSgt import *


def my_transfer_handler(args):
    """Function called each time a chunk of data is transferred"""
    total_size = args.total_size if args.total_size is not None else "unknown"
    print(f"Context: '{args.context}{'with opc' if args.opc_sync else ''}', "
          f"chunk {args.chunk_ix}, "
          f"transferred {args.transferred_size} bytes, "
          f"total size {total_size}, "
          f"direction {'reading' if args.reading else 'writing'}, "
          f"data '{args.data}'")
    if args.end_of_transfer:
        print('End of Transfer')
    # Slow down the transfer by 200ms to see the progress better
    time.sleep(0.1)


sgt = RsSgt('TCPIP::10.214.1.57::HISLIP')
print(sgt.utilities.idn_string)
sgt.utilities.reset()

pc_file = r'c:\temp\bigFile.bin'
instr_file = '/var/user/bigFileInstr.bin'
pc_file_back = r'c:\temp\bigFileBack.bin'

# Generate a random file of 20MB size
x1mb = 1024 * 1024
with open(pc_file, 'wb') as file:
    for x in range(20):
        file.write(np.random.bytes(x1mb))

# Send the file to the instrument with events
sgt.events.on_write_handler = my_transfer_handler
sgt.utilities.data_chunk_size = x1mb
print(f'Sending file to the instrument...')
sgt.utilities.send_file_from_pc_to_instrument(pc_file, instr_file)
sgt.events.on_write_handler = None
print(f'Receiving file from the instrument...')
sgt.events.on_read_handler = my_transfer_handler
sgt.utilities.read_file_from_instrument_to_pc(instr_file, pc_file_back)
sgt.events.on_read_handler = None
sgt.close()