visaQuery.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python3
  2. import pyvisa
  3. import time
  4. import sys
  5. def test_idn():
  6. idn = inst.query("*idn?");
  7. assert (idn == "TinyUSB,ModelNumber,SerialNumber,FirmwareVer123456\r\n")
  8. assert (inst.is_4882_compliant)
  9. def test_echo(m,n):
  10. longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 50
  11. t0 = time.monotonic()
  12. #Next try echo from 1 to 175 characters (200 is max buffer size on DUT)
  13. for i in range(m,n):
  14. #print(i)
  15. x = longstr[0:i]
  16. xt = x + inst.write_termination
  17. y = inst.query(x)
  18. #print(x)
  19. #print (":".join("{:02x}".format(ord(c)) for c in xt))
  20. #print (":".join("{:02x}".format(ord(c)) for c in y))
  21. assert(xt == y), f"failed i={i}"
  22. #inst.read_stb();# Just to make USB logging easier by sending a control query (bad thing is that this made things slow)
  23. t = time.monotonic() - t0
  24. print(f" elapsed: {t:0.3} sec")
  25. def test_trig():
  26. # clear SRQ
  27. inst.read_stb()
  28. assert (inst.read_stb() == 0)
  29. inst.assert_trigger()
  30. time.sleep(0.3) # SRQ may have some delay
  31. assert (inst.read_stb() & 0x40), "SRQ not set after 0.3 seconds"
  32. assert (inst.read_stb() == 0)
  33. def test_mav():
  34. inst.write("delay 50")
  35. inst.read_stb() # clear STB
  36. assert (inst.read_stb() == 0)
  37. inst.write("123")
  38. time.sleep(0.3)
  39. assert (inst.read_stb() & 0x10), "MAV not set after 0.5 seconds"
  40. rsp = inst.read()
  41. assert(rsp == "123\r\n")
  42. def test_srq():
  43. assert (inst.read_stb() == 0)
  44. inst.write("123")
  45. #inst.enable_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE)
  46. #waitrsp = inst.wait_on_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, 5000)
  47. #inst.discard_events(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE)
  48. #inst.wait_for_srq()
  49. time.sleep(0.3)
  50. stb = inst.read_stb()
  51. msg = "SRQ not set after 0.5 seconds, was {:02x}".format(stb)
  52. assert (stb == 0x50),msg
  53. assert (inst.read_stb() == 0x10), "SRQ set at second read!"
  54. rsp = inst.read()
  55. assert(rsp == "123\r\n")
  56. def test_read_timeout():
  57. inst.timeout = 500
  58. # First read with no MAV
  59. inst.read_stb()
  60. assert (inst.read_stb() == 0)
  61. inst.write("delay 500")
  62. t0 = time.monotonic()
  63. try:
  64. rsp = inst.read()
  65. assert(False), "Read should have resulted in timeout"
  66. except pyvisa.VisaIOError:
  67. print(" Got expected exception")
  68. t = time.monotonic() - t0
  69. assert ((t*1000.0) > (inst.timeout - 300))
  70. assert ((t*1000.0) < (inst.timeout + 300))
  71. print(f"Delay was {t:0.3}")
  72. # Response is still in queue, so send a clear (to be more helpful to the next test)
  73. inst.clear()
  74. time.sleep(0.3)
  75. assert(0 == (inst.read_stb() & 0x10)), "MAV not reset after clear"
  76. def test_abort_in():
  77. inst.timeout = 200
  78. # First read with no MAV
  79. inst.read_stb()
  80. assert (inst.read_stb() == 0)
  81. inst.write("delay 500")
  82. inst.write("xxx")
  83. t0 = time.monotonic()
  84. try:
  85. rsp = inst.read()
  86. assert(False), "Read should have resulted in timeout"
  87. except pyvisa.VisaIOError:
  88. print(" Got expected exception")
  89. t = time.monotonic() - t0
  90. assert ((t*1000.0) > (inst.timeout - 300))
  91. assert ((t*1000.0) < (inst.timeout + 300))
  92. print(f" Delay was {t:0.3}")
  93. # Response is still in queue, so read it out (to be more helpful to the next test)
  94. inst.timeout = 800
  95. y = inst.read()
  96. assert(y == "xxx\r\n")
  97. def test_indicate():
  98. # perform indicator pulse
  99. usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM)
  100. retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=64, request_value=0x0000, index=usb_iface, length=0x0001)
  101. # pyvisa used to return (statuscode,bytes), but now only returns bytes, so we need to handle both cases
  102. if(isinstance(retv,bytes)):
  103. assert(retv == b'\x01')
  104. else:
  105. assert((retv[1] == pyvisa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
  106. def test_multi_read():
  107. old_chunk_size = inst.chunk_size
  108. longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 10
  109. timeout = 10
  110. x = longstr[0:174]
  111. inst.chunk_size = 50 # Seems chunk size only applies to read but not write
  112. inst.write(x)
  113. # I'm not sure how to request just the remaining bit using a max count... so just read it all.
  114. y = inst.read()
  115. assert (x + "\r\n" == y)
  116. #inst.chunk_size = old_chunk_size
  117. def test_stall_ep0():
  118. usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM)
  119. inst.read_stb()
  120. # This is an invalid request, should create stall.
  121. try:
  122. retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=60, request_value=0x0000, index=usb_iface, length=0x0001)
  123. assert(False)
  124. except pyvisa.VisaIOError:
  125. pass
  126. assert (inst.read_stb() == 0)
  127. rm = pyvisa.ResourceManager()
  128. reslist = rm.list_resources("USB?::?*::INSTR")
  129. print(reslist)
  130. if (len(reslist) == 0):
  131. sys.exit()
  132. inst = rm.open_resource(reslist[0]);
  133. inst.timeout = 3000
  134. inst.clear()
  135. print("+ IDN")
  136. test_idn()
  137. print("+test abort in")
  138. test_abort_in()
  139. inst.timeout = 2000
  140. print("+ multi read")
  141. test_multi_read()
  142. print("+ echo delay=0")
  143. inst.write("delay 0")
  144. test_echo(1,175)
  145. print("+ echo delay=2")
  146. inst.write("delay 2")
  147. test_echo(1,175)
  148. print("+ echo delay=150")
  149. inst.write("delay 150")
  150. test_echo(53,76)
  151. test_echo(165,170)
  152. print("+ Read timeout (no MAV)")
  153. test_read_timeout()
  154. print("+ Test EP0 stall recovery")
  155. test_stall_ep0()
  156. print("+ MAV")
  157. test_mav()
  158. print("+ SRQ")
  159. test_srq()
  160. print("+ indicate")
  161. test_indicate()
  162. print("+ TRIG")
  163. test_trig()
  164. # Untested:
  165. # abort bulk out
  166. # LLO, GTL, etc
  167. # Throughput rate?
  168. # Transmitting a message using multiple transfers
  169. inst.close()
  170. print("Test complete")