From 3d4d37375bbfb0bc88127be3aa79930e865f35b5 Mon Sep 17 00:00:00 2001 From: NConrad Date: Tue, 21 Jun 2022 23:17:50 -0400 Subject: [PATCH] USBTMC: Update test python code to use the new pyvisa namespace, and other misc fixes --- examples/device/usbtmc/visaQuery.py | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/device/usbtmc/visaQuery.py b/examples/device/usbtmc/visaQuery.py index 50a765a03..c4e5ad2b9 100644 --- a/examples/device/usbtmc/visaQuery.py +++ b/examples/device/usbtmc/visaQuery.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import visa +import pyvisa import time import sys @@ -54,9 +54,9 @@ def test_srq(): assert (inst.read_stb() == 0) inst.write("123") - #inst.enable_event(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE) - #waitrsp = inst.wait_on_event(visa.constants.VI_EVENT_SERVICE_REQ, 5000) - #inst.discard_events(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE) + #inst.enable_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE) + #waitrsp = inst.wait_on_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, 5000) + #inst.discard_events(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE) #inst.wait_for_srq() time.sleep(0.3) stb = inst.read_stb() @@ -77,8 +77,8 @@ def test_read_timeout(): t0 = time.monotonic() try: rsp = inst.read() - assert(false), "Read should have resulted in timeout" - except visa.VisaIOError: + assert(False), "Read should have resulted in timeout" + except pyvisa.VisaIOError: print(" Got expected exception") t = time.monotonic() - t0 assert ((t*1000.0) > (inst.timeout - 300)) @@ -99,23 +99,27 @@ def test_abort_in(): t0 = time.monotonic() try: rsp = inst.read() - assert(false), "Read should have resulted in timeout" - except visa.VisaIOError: + assert(False), "Read should have resulted in timeout" + except pyvisa.VisaIOError: print(" Got expected exception") t = time.monotonic() - t0 assert ((t*1000.0) > (inst.timeout - 300)) assert ((t*1000.0) < (inst.timeout + 300)) print(f" Delay was {t:0.3}") - # Response is still in queue, so send a clear (to be more helpful to the next test) + # Response is still in queue, so read it out (to be more helpful to the next test) inst.timeout = 800 y = inst.read() assert(y == "xxx\r\n") def test_indicate(): # perform indicator pulse - usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM) + usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM) retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=64, request_value=0x0000, index=usb_iface, length=0x0001) - assert((retv[1] == visa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}" + # pyvisa used to return (statuscode,bytes), but now only returns bytes, so we need to handle both cases + if(isinstance(retv,bytes)): + assert(retv == b'\x01') + else: + assert((retv[1] == pyvisa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}" def test_multi_read(): @@ -131,19 +135,19 @@ def test_multi_read(): #inst.chunk_size = old_chunk_size def test_stall_ep0(): - usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM) + usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM) inst.read_stb() # This is an invalid request, should create stall. try: retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=60, request_value=0x0000, index=usb_iface, length=0x0001) - assert false - except visa.VisaIOError: + assert(False) + except pyvisa.VisaIOError: pass assert (inst.read_stb() == 0) -rm = visa.ResourceManager() +rm = pyvisa.ResourceManager() reslist = rm.list_resources("USB?::?*::INSTR") print(reslist) @@ -167,7 +171,6 @@ inst.timeout = 2000 print("+ multi read") test_multi_read() - print("+ echo delay=0") inst.write("delay 0") test_echo(1,175)