example01.py

  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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python

##############################################################################
##
## Copyright (C) 2021 Narda Safety Test Solutions GmbH.
## Contact: http://www.narda-sts.de
##
## Redistribution and use in source and binary forms,
## with or without modification, are permitted provided that the following
## conditions are met:
##
## 1.) Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
##
## 2.) Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in the
## documentation and/or other materials provided with the distribution.
##
## 3.) Neither the name of the copyright holder nor the names of its
## contributors may be used to endorse or promote products derived from this
## software without specific prior written permission.
##
## 4.) EXPORT CONTROL
## The Software, including technical data /cryptographic software,
## may be subject to, German, European Union and U.S. export controls and
## may be subject to import or export controls in other countries.
## The Licensee agrees to strictly comply with all applicable import and
## export regulations. He specifically agrees, that he must not disclose or
## otherwise export or re-export the Licensed Software or any part thereof
## delivered under this end user license agreement (EULA) to any country
## (including a national or resident of such country) without a valid export
## or import license. Please be aware that the Software may contain
## US-Content, therefor the Licensee represent and warrant that he is not a
## citizen of, or otherwise located within, an embargoed nation (including
## without limitation Iran, Syria, Sudan, Cuba, North Korea) and that he is
## not otherwise prohibited under the Export Laws from receiving the Software.
## All rights to Use the Software are granted on condition that such rights
## are forfeited if the Licensee fails to comply with
## the terms of this Agreement.
##
## 5.) SEVERABILITY
## Should any provision of this Agreement be or become invalid, ineffective
## or unenforceable, the remaining provisions of this Agreement shall be
## valid. The parties agree to replace the invalid, ineffective or
## unenforceable provision by a valid, effective and enforceable provision
## which best meets the commercial intention of the parties.
## The same shall apply in case of omissions.
##
## 6.) APPLICABLE LAW AND PLACE OF JURISTICATION
## This Agreement shall be constituted under the law of Germany.
## The United Nations Convention on the International Sale of Goods
## shall not apply to this Agreement. The Place of Jurisdiction for any
## dispute between the Parties shall be Tübingen (Germany).
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Narda Safety Test Solutions GmbH
## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
##
##############################################################################


from pathlib import Path
import time
import os
import wx
from nardascripting.base.signalsharkdev import *
from nardascripting.base.usrscriptbase import *
import nardascripting.base.toolbox as tb


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
        # -------------------------------------------------------------------------------

    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()

download example01.zip