Example 1: Writing a Simple Script

This example explains how to create a simple script based on the template tmpusrscript.py.

Step 1) Define the ‘Measurement Sequence’

The aim of this script is, to copy several settings from one task to another task.

Therefore, the script must perform the following steps:

  • Connect to the device

  • Querying the settings to be copied

  • Querying a list of all tasks

  • Let the user select the destination Task

  • Switch to the destination task

  • Copy the settings to the destination Task.

  • Output a status message

  • Disconnect from the device

Step 2) Start with a Template

Predefined template files are available for various kind of applications.

The ‘measurement sequence’ as described in ‘step 1)’ can be executed by the device in a few seconds. Therefore we can use the simple template file: tmpusrscript.py

For more information see: Script Templates

Step 3) Adapt the Template File

In this step we have to adapt the template file according to our defined goal.

Note

It is recommended to use an IDE such as PyCharm to create and edit a script. These offer the possibility of debugging the script execution in addition to autocompletion of code.

The Narda Script Launcher API uses four spaces per indentation. The type of indentation must not be changed within a script!

Name the Script and Add a Description.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class CopySettings(UsrScriptBase):
    """User script class"""

    def __init__(self, main_gui, dev=SignalSharkDev()):
        """Initialization. Please leave this code unchanged"""
        super().__init__(main_gui, dev, __file__)

        # Base settings
        self._tab_name = 'Examples'
        self._scr_title = 'Example01 - Copy Settings'
        self._scr_description = 'Copies some settings from one task to another task'
        # self._icon_path = self.script_path.joinpath('NardaScriptLauncher_icon_close.png')
        self._list_prio = 1
        self._nsl_executed_behavior = NSL_Executed_Behaviors.MINIMIZE_NSL

        # Add class variables if needed
        # -------------------------------------------------------------------------------

Code the Defined ‘Measurement Sequence’

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def _run_script(self, args):
    """
    Script main function
    This method is called when a user clicks on the corresponding script button.
    """
    # Connect to the device
    if not self.signalshark.connect():
        self.MessageBoxModal('Cannot connect to the device!', 'Connection error',
                             wx.OK | wx.ICON_ERROR)
        return

    # Clear SCPI error queue
    self.signalshark.scpi.check_error()

    # Ask the user to select the desired parameters
    lst = ['Atten.', 'RBW', 'Fcent', 'FSpan', 'Meas. Time']
    def_sel = self.config.get('def_sel', [0, 1, 2, 3, 4])
    selections = None
    dlg = wx.MultiChoiceDialog(self.frm_dlghelper,
                               'Select parameters to copy.',
                               'Copy Settings', lst)
    dlg.SetSelections(def_sel)
    if self.ShowDlgModalTop(dlg) == wx.ID_OK:
        selections = dlg.GetSelections()
    dlg.Destroy()  # Please do not forget to destroy the created dialog if you do not need it anymore

    # Exit the script if the user has not selected anything or pressed the Cancel button.
    if not selections:
        self.signalshark.disconnect()
        return

    # Store user selection as default value
    self.config['def_sel'] = selections

    # Querying the settings to be copied
    atten = self.signalshark.scpi.sense.get_attenuator()
    rbw = self.signalshark.scpi.spectrum.get_rbw()
    fcent = self.signalshark.scpi.spectrum.get_frequency_center()
    fspan = self.signalshark.scpi.spectrum.get_frequency_span()
    meastime = self.signalshark.scpi.spectrum.get_measurement_time()

    # Query actual task name
    act_task = self.signalshark.scpi.task.get_selected()[1]

    # Querying a list of all available task names
    task_names = self.signalshark.scpi.task.get_names()

    if len(task_names) < 2:
        self.signalshark.disconnect()
        self.MessageBoxModal('Please add another task first!', 'Copy Settings',
                             wx.OK | wx.ICON_INFORMATION)
        return

    # Remove current task name from list
    task_names.remove(act_task)

    # Let the user select the destination Task
    task = ''
    dlg = tb.ListMsgBox(self.frm_dlghelper, 'Task', task_names, '', '', 'Select destination task')
    returnvalue = self.ShowDlgModalTop(dlg)
    if returnvalue == wx.ID_OK:
        task = dlg.value
    dlg.Destroy()

    if task:
        # Switch to the destination task
        self.signalshark.scpi.task.set_select(task)

        # Copy the settings to the destination Task.
        # ['Atten.', 'RBW', 'Fcent', 'FSpan', 'Meas. Time']
        if 0 in selections:
            self.signalshark.scpi.sense.set_attenuator(atten)
        if 1 in selections:
            self.signalshark.scpi.spectrum.set_rbw(rbw)
        if 2 in selections:
            self.signalshark.scpi.spectrum.set_frequency_center(fcent)
        if 3 in selections:
            self.signalshark.scpi.spectrum.set_frequency_span(fspan)
        if 4 in selections:
            self.signalshark.scpi.spectrum.set_measurement_time(meastime)

        # Output a status message
        self.MessageBoxModal('Copy settings done!', 'Copy Settings',
                             wx.OK | wx.ICON_INFORMATION)

    # Disconnect from the device
    self.signalshark.disconnect()

Add Packages to the Import Section of the Module

In this example we use the ‘ListMsgBox’ to let the user select the target task.

However, the ‘ListMsgBox’ is not part of the standard Python library, but is included in the ‘toolbox’ module of the nardascripting API and must therefore be imported first.

To do this, add the following line of code to the import section.

  • import nardascripting.base.toolbox as tb

1
2
3
4
5
6
7
from pathlib import Path
import time
import os
import wx
from nardascripting.base.usrscriptbase import *
from nardascripting.base.signalsharkdev import *
import nardascripting.base.toolbox as tb

Step 4) Debug the Script

It is recommended to use an IDE such as PyCharm to create and edit a script. These offer the possibility of debugging the script execution in addition to autocompletion of code.

The application note “Developing Scripts for SignalShark with PyCharm.pdf”, which can be downloaded from the Narda website, describes how to use the Narda Script Launcher together with PyCharm.

Note

The Narda Script Launcher API uses four spaces per indentation. The type of indentation must not be changed within a script!

You can view and download the complete script here: