visaQuery.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python3
  2. import visa
  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(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE)
  46. #waitrsp = inst.wait_on_event(visa.constants.VI_EVENT_SERVICE_REQ, 5000)
  47. #inst.discard_events(visa.constants.VI_EVENT_SERVICE_REQ, visa.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 visa.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 visa.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 send a clear (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(visa.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. assert((retv[1] == visa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
  102. def test_multi_read():
  103. old_chunk_size = inst.chunk_size
  104. longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 10
  105. timeout = 10
  106. x = longstr[0:174]
  107. inst.chunk_size = 50 # Seems chunk size only applies to read but not write
  108. inst.write(x)
  109. # I'm not sure how to request just the remaining bit using a max count... so just read it all.
  110. y = inst.read()
  111. assert (x + "\r\n" == y)
  112. #inst.chunk_size = old_chunk_size
  113. def test_stall_ep0():
  114. usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
  115. inst.read_stb()
  116. # This is an invalid request, should create stall.
  117. try:
  118. retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=60, request_value=0x0000, index=usb_iface, length=0x0001)
  119. assert false
  120. except visa.VisaIOError:
  121. pass
  122. assert (inst.read_stb() == 0)
  123. rm = visa.ResourceManager()
  124. reslist = rm.list_resources("USB?::?*::INSTR")
  125. print(reslist)
  126. if (len(reslist) == 0):
  127. sys.exit()
  128. inst = rm.open_resource(reslist[0]);
  129. inst.timeout = 3000
  130. inst.clear()
  131. print("+ IDN")
  132. test_idn()
  133. print("+test abort in")
  134. test_abort_in()
  135. inst.timeout = 2000
  136. print("+ multi read")
  137. test_multi_read()
  138. print("+ echo delay=0")
  139. inst.write("delay 0")
  140. test_echo(1,175)
  141. print("+ echo delay=2")
  142. inst.write("delay 2")
  143. test_echo(1,175)
  144. print("+ echo delay=150")
  145. inst.write("delay 150")
  146. test_echo(53,76)
  147. test_echo(165,170)
  148. print("+ Read timeout (no MAV)")
  149. test_read_timeout()
  150. print("+ Test EP0 stall recovery")
  151. test_stall_ep0()
  152. print("+ MAV")
  153. test_mav()
  154. print("+ SRQ")
  155. test_srq()
  156. print("+ indicate")
  157. test_indicate()
  158. print("+ TRIG")
  159. test_trig()
  160. # Untested:
  161. # abort bulk out
  162. # LLO, GTL, etc
  163. # Throughput rate?
  164. # Transmitting a message using multiple transfers
  165. inst.close()
  166. print("Test complete")