def check_nonexisting_commands(scpi_obj): _print_header("Testing to query commands that doesn't exist") base_cmd = _random_choice(['SOURce', 'BASIcloop', 'ITERative']) sub_cmd = _random_choice(['CURRent', 'VOLTage']) attr = _random_choice(['UPPEr', 'LOWEr', 'VALUe']) fake = "FAKE" ack = 'ACK\r\n' nok = 'NOK\r\n' start_t = _time() pairs = [ # * first level doesn't exist ["{0}:{1}:{2}?".format(fake, sub_cmd, attr), nok], # * intermediate level doesn't exist ["{0}:{1}:{2}?".format(base_cmd, fake, attr), nok], # * Attribute level doesn't exist ["{0}:{1}:{2}?".format(base_cmd, sub_cmd, fake), nok], # * Attribute that doesn't respond ['source:voltage:exception?', nok], # * Unexisting Channel ["CHANnel{0}".format(str(n_channels+3).zfill(2)), nok], # * Channel below the minimum reference ["CHANnel00:VOLTage:UPPEr?", nok], # * Channel above the maximum reference ["CHANnel99:VOLTage:UPPEr?", nok], ] correct, failed = 0, 0 for cmd, expected_answer in pairs: answer = '' try: start_t = _time() answer = _send2input( scpi_obj, cmd, expected_answer=expected_answer) correct += 1 except ValueError as exc: print("\tFake command answer failed: {0}".format(exc)) failed += 1 except Exception as exc: print("\tUnexpected kind of exception! {0}".format(exc)) print_exc() failed += 1 print("\tRequest non-existing command {0}\n" "\tAnswer: {1!r} ({2:g} ms)" "".format(cmd, answer, (_time()-start_t)*1000)) if failed == 0: result = True, "Non-existing commands test PASSED" else: print("Failed {0}/{1}".format(failed, correct+failed)) result = False, "Non-existing commands test FAILED" _print_footer(result[1]) return result
def check_command_queries(scpi_obj): _print_header("Testing to command queries") try: print("Launch tests:") cmd = "*IDN?" answer = _send2input(scpi_obj, cmd) print("\tInstrument identification ({0})\n\tAnswer: {1}" "".format(cmd, answer)) for base_cmd in ['SOURce', 'BASIcloop', 'ITERative']: _print_header("Check {0} part of the tree".format(base_cmd)) _do_check_commands(scpi_obj, base_cmd) for ch in range(1, n_channels+1): base_cmd = "CHANnel{0}".format(str(ch).zfill(2)) _print_header("Check {0} part of the tree".format(base_cmd)) _do_check_commands(scpi_obj, base_cmd) fn = _random_choice(range(1, n_subchannels+1)) inner_cmd = "FUNCtion{0}".format(str(fn).zfill(2)) _print_header("Check {0} + MEAS:{1} part of the tree" "".format(base_cmd, inner_cmd)) _do_check_commands(scpi_obj, base_cmd, inner_cmd) result = True, "Command queries test PASSED" except Exception as exc: print("\tUnexpected kind of exception! {0}".format(e)) print_exc() result = False, "Command queries test FAILED" _print_footer(result[1]) return result
def _build_command2test(): base_cmds = ['SOURce', 'BASIcloop', 'ITERative', 'CHANnel'] sub_cmds = ['CURRent', 'VOLTage'] attrs = ['UPPEr', 'LOWEr', 'VALUe'] base_cmd = _random_choice(base_cmds) if base_cmd in ['CHANnel']: base_cmd = "{0}{1}".format( base_cmd, str(_randint(1, n_channels)).zfill(2)) if _randint(0, 1): base_cmd = "{0}:MEAS:FUNC{1}" \ "".format(base_cmd, str(_randint(1, n_subchannels)).zfill(2)) sub_cmd = _random_choice(sub_cmds) if base_cmd in ['SOURce']: attr = _random_choice(attrs + ['BUFFer']) else: attr = _random_choice(attrs) return "{0}:{1}:{2}?".format(base_cmd, sub_cmd, attr)
def _get_random_string(_len): return ''.join(_random_choice(_letters) for _ in range(_len))
def check_command_writes(scpi_obj): _print_header("Testing to command writes") try: # simple commands --- current_conf_obj = WattrTest() scpi_obj.add_command('source:current:configure', read_cb=current_conf_obj.readTest, write_cb=current_conf_obj.writeTest) voltage_conf_obj = WattrTest() scpi_obj.add_command('source:voltage:configure', read_cb=voltage_conf_obj.readTest, write_cb=voltage_conf_obj.writeTest) for inner in ['current', 'voltage']: _do_write_command(scpi_obj, "source:{0}:configure".format(inner)) _wait(1) # FIXME: remove # channel commands --- _print_header("Testing to channel command writes") base_cmd = 'writable' w_obj = scpi_obj.add_component(base_cmd, scpi_obj.command_tree) ch_cmd = 'channel' ch_obj = scpi_obj.add_channel(ch_cmd, n_channels, w_obj) ch_current_obj = WchannelTest(n_channels) ch_voltage_obj = WchannelTest(n_channels) for (subcomponent, sub_cmd_obj) in [('current', ch_current_obj), ('voltage', ch_voltage_obj)]: subcomponent_obj = scpi_obj.add_component(subcomponent, ch_obj) for (attr_name, attr_func) in [('upper', 'upperLimit'), ('lower', 'lowerLimit'), ('value', 'readTest')]: if hasattr(sub_cmd_obj, attr_func): if attr_name == 'value': attr_obj = scpi_obj.add_attribute( attr_name, subcomponent_obj, read_cb=sub_cmd_obj.readTest, write_cb=sub_cmd_obj.writeTest, default=True) else: cb_func = getattr(sub_cmd_obj, attr_func) attr_obj = scpi_obj.add_attribute( attr_name, subcomponent_obj, cb_func) print("\nChecking one write multiple reads\n") for i in range(n_channels): rnd_ch = _randint(1, n_channels) element = _random_choice(['current', 'voltage']) _do_write_channel_command( scpi_obj, "{0}:{1}".format(base_cmd, ch_cmd), rnd_ch, element, n_channels) _inter_test_wait() print("\nChecking multile writes multiple reads\n") for i in range(n_channels): test_nwrites = _randint(2, n_channels) rnd_chs = [] while len(rnd_chs) < test_nwrites: rnd_ch = _randint(1, n_channels) while rnd_ch in rnd_chs: rnd_ch = _randint(1, n_channels) rnd_chs.append(rnd_ch) element = _random_choice(['current', 'voltage']) values = [_randint(-1000, 1000)]*test_nwrites _do_write_channel_command( scpi_obj, "{0}:{1}".format(base_cmd, ch_cmd), rnd_chs, element, n_channels, values) _inter_test_wait() print("\nChecking write with allowed values limitation\n") selection_cmd = 'source:selection' selection_obj = WattrTest() selection_obj.writeTest(False) scpi_obj.add_command(selection_cmd, read_cb=selection_obj.readTest, write_cb=selection_obj.writeTest, allowed_argins=[True, False]) _do_write_command(scpi_obj, selection_cmd, True) # _do_write_command(scpi_obj, selection_cmd, 'Fals') # _do_write_command(scpi_obj, selection_cmd, 'True') try: _do_write_command(scpi_obj, selection_cmd, 0) except Exception: print("\tLimitation values succeed because it raises an exception " "as expected") else: raise AssertionError("It has been write a value that " "should not be allowed") _inter_test_wait() result = True, "Command writes test PASSED" except Exception as exc: print("\tUnexpected kind of exception! {0}".format(exc)) print_exc() result = False, "Command writes test FAILED" _print_footer(result[1]) return result