Examples
For more examples, visit our Rohde & Schwarz Github repository.
"""Getting started - how to work with RsSmbv Python package.
This example performs basic RF settings on an SMBV100B instrument.
It shows the RsSmbv calls and their corresponding SCPI commands.
Notice that the python RsSmbv interfaces track the SCPI commands syntax."""
from RsSmbv import *
# Open the session
smbv = RsSmbv('TCPIP::10.112.0.228::HISLIP')
# Greetings, stranger...
print(f'Hello, I am: {smbv.utilities.idn_string}')
# OUTPut:STATe ON
smbv.output.state.set_value(True)
# SOURce:FREQuency:MODE CW
smbv.source.frequency.set_mode(enums.FreqMode.CW)
# SOURce:POWer:LEVel:IMMediate:AMPLitude -20
smbv.source.power.level.immediate.set_amplitude(-20)
# SOURce:FREQuency:FIXed 223000000
smbv.source.frequency.fixed.set_value(223E6)
# SOURce:POWer:PEP?
pep = smbv.source.power.get_pep()
print(f'PEP level: {pep} dBm')
# Close the session
smbv.close()
"""Basic example of importing the package, initializing the session and performing basic generator settings."""
from RsSmbv import *
RsSmbv.assert_minimum_version('4.80.2')
smbv = RsSmbv('TCPIP::10.112.0.228::HISLIP')
# smcv = RsSmbv('TCPIP::10.112.1.179::5025::SOCKET', options='SelectVisa=SocketIo') # No VISA needed
print(f'Driver Info: {smbv.utilities.driver_version}')
print(f'Instrument: {smbv.utilities.idn_string}')
# Instrument options are properly parsed, and sorted (k-options first)
print(f'Instrument options: {",".join(smbv.utilities.instrument_options)}')
# Driver's instrument status checking ( SYST:ERR? ) after each command (default value is True):
smbv.utilities.instrument_status_checking = True
smbv.output.state.set_value(True)
smbv.source.frequency.set_mode(enums.FreqMode.CW)
smbv.source.power.level.immediate.set_amplitude(-20)
smbv.source.frequency.fixed.set_value(223E6)
print(f'PEP level: {smbv.source.power.get_pep()} dBm')
# You can still use the direct SCPI interface:
response = smbv.utilities.query_str('*IDN?')
print(f'Direct SCPI response on *IDN?: {response}')
smbv.close()
"""The example:
- creates waveform file from a csv-file with I/Q pairs
- sends the file to the SMBV 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 RsSmbv import *
RsSmbv.assert_minimum_version('4.80.2')
smbv = RsSmbv('TCPIP::10.112.1.73::HISLIP')
print(smbv.utilities.idn_string)
smbv.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 = smbv.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
smbv.arb_files.send_waveform_file_to_instrument(pc_wv_file, instr_wv_file)
# Selecting the waveform and load it in ARB
smbv.source.bb.arbitrary.waveform.set_select(instr_wv_file)
# Turning on the ARB baseband
smbv.source.bb.arbitrary.set_state(True)
# Turning on the RF output state state
smbv.output.state.set_value(True)
smbv.close()
"""The example:
- creates waveform file from two i_data and q_data vectors
- sends the file to the SMBV 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 RsSmbv import *
RsSmbv.assert_minimum_version('4.80.2')
smbv = RsSmbv('TCPIP::10.112.1.73::HISLIP')
print(smbv.utilities.idn_string)
smbv.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 = 100e6
# wave clock
wave_freq = 25e6
# 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
samples_count = 200
time_step = 1 / clock_freq
time_vector = [x * time_step for x in range(samples_count)]
# I-component an Q-component data
i_data = [np.cos(2 * np.pi * wave_freq * tv) * scale_factor for tv in time_vector]
q_data = [np.sin(2 * np.pi * wave_freq * tv) * scale_factor for tv in time_vector]
# Take those samples and create a wv file, send it to the instrument with the name instr_wv_file (not auto-scaled)
result = smbv.arb_files.create_waveform_file_from_samples(i_data, q_data, pc_wv_file, clock_freq=clock_freq, auto_scale=False, comment='Created from I/Q vectors')
smbv.arb_files.send_waveform_file_to_instrument(pc_wv_file, instr_wv_file)
# Selecting the waveform and load it in the ARB
smbv.source.bb.arbitrary.waveform.set_select(instr_wv_file)
smbv.source.frequency.set_frequency(1.1E9)
smbv.source.power.level.immediate.set_amplitude(-11.1)
# Turning on the ARB baseband
smbv.source.bb.arbitrary.set_state(True)
# Turning on the RF out state
smbv.output.state.set_value(True)
smbv.close()
# This example shows how to work with commands that have many repeated capabilities (numeric suffixes)
# The example does not demonstrate any valid instrument settings, rather the instrument driver general rules of working with the repeated capabilities
from RsSmbv import *
RsSmbv.assert_minimum_version('4.80.2')
smbv = RsSmbv('TCPIP::10.112.0.228::HISLIP', True, True)
print(f'Driver Info: {smbv.utilities.driver_version}')
print(f'Instrument: {smbv.utilities.idn_string}')
# Switching the error checking off to avoid errors from invalid parameter settings
smbv.utilities.instrument_status_checking = False
smbv.source.bb.nr5G.set_state(True)
# Setting commands with many repeated capabilities:
# [SOURce<HW>]:BB:NR5G:SCHed:CELL<CH>:SUBF<ST>:USER:BWPart:ALLoc:APMap:COL<S2US>:ROW<S3US>:IMAGinary
# Option 1: explicit definition:
# sending SOURce1:BB:NR5G:SCHed:CELL1:SUBF3:USER0:BWPart1:ALLoc0:APMap:COL0:ROW3:IMAGinary 10.0
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.row.imaginary.set(10.0, repcap.CellNull.Nr1, repcap.SubframeNull.Nr3, repcap.UserNull.Nr0, repcap.BwPartNull.Nr1, repcap.AllocationNull.Nr0, repcap.ColumnNull.Nr0, repcap.RowNull.Nr3)
# Option 2: only some arguments with keywords, others keep their default values:
# sending SOURce1:BB:NR5G:SCHed:CELL1:SUBF3:USER0:BWPart1:ALLoc0:APMap:COL0:ROW3:IMAGinary 10.0
# Default values for skipped repCaps:
# - repcap.UserNull.Nr0
# - repcap.Allocation.Nr0
# - repcap.ColumnNull.Nr0
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.row.imaginary.set(10.0, cellNull=repcap.CellNull.Nr1, subframeNull=repcap.SubframeNull.Nr3, bwPartNull=repcap.BwPartNull.Nr1, rowNull=repcap.RowNull.Nr3)
# Option 3: default values are set in the group interfaces, and then left to default in the method call:
smbv.source.bb.nr5G.scheduling.cell.repcap_cellNull_set(repcap.CellNull.Nr1)
smbv.source.bb.nr5G.scheduling.cell.subf.repcap_subframeNull_set(repcap.SubframeNull.Nr3)
smbv.source.bb.nr5G.scheduling.cell.subf.user.repcap_userNull_set(repcap.UserNull.Nr0)
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.repcap_bwPartNull_set(repcap.BwPartNull.Nr1)
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.repcap_allocationNull_set(repcap.AllocationNull.Nr0)
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.repcap_columnNull_set(repcap.ColumnNull.Nr0)
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.row.repcap_rowNull_set(repcap.RowNull.Nr3)
# and then just use the set() method without repeated capabilities:
# sending SOURce1:BB:NR5G:SCHed:CELL1:SUBF3:USER0:BWPart1:ALLoc0:APMap:COL0:ROW3:IMAGinary 10.0
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.row.imaginary.set(10.0)
# We can clone the cell interface and change the default cell from Nr1 to Nr2 without affecting the origin interface:
cell_nr2 = smbv.source.bb.nr5G.scheduling.cell.clone()
cell_nr2.repcap_cellNull_set(repcap.CellNull.Nr2)
# Now we have an independent object cell_nr2, and can set the same command for cell Nr2
# All other repcap default values are the same:
# repcap.SubframeNull.Nr3
# repcap.ColumnNull.Nr2
# repcap.RowNull.Nr3
# sending SOURce1:BB:NR5G:SCHed:CELL2:SUBF3:USER:BWPart:ALLoc:APMap:COL2:ROW3:IMAGinary 10.0
cell_nr2.subf.user.bwPart.alloc.apMap.col.row.imaginary.set(10.0)
# Option 4: Combination of Options 1, 2 and 3 - we use the default values from the group interfaces and explicitly define some of them:
# Here we change the channel to 5 and Column to 4, all others are default from the group
# sending SOURce1:BB:NR5G:SCHed:CELL5:SUBF3:USER:BWPart:ALLoc:APMap:COL4:ROW3:IMAGinary 10.0
smbv.source.bb.nr5G.scheduling.cell.subf.user.bwPart.alloc.apMap.col.row.imaginary.set(10.0, cellNull=repcap.CellNull.Nr5, columnNull=repcap.ColumnNull.Nr4)
smbv.close()
"""Example showing how you can transfer a big file to the instrument and from the instrument with showing the progress.
Since the SMBV100B 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 RsSmbv 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)
RsSmbv.assert_minimum_version('4.80.2')
smbv = RsSmbv('TCPIP::10.112.1.73::HISLIP')
print(smbv.utilities.idn_string)
smbv.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
smbv.events.on_write_handler = my_transfer_handler
smbv.utilities.data_chunk_size = x1mb
print(f'Sending file to the instrument...')
smbv.utilities.send_file_from_pc_to_instrument(pc_file, instr_file)
smbv.events.on_write_handler = None
print(f'Receiving file from the instrument...')
smbv.events.on_read_handler = my_transfer_handler
smbv.utilities.read_file_from_instrument_to_pc(instr_file, pc_file_back)
smbv.events.on_read_handler = None
smbv.close()