In this post, I test the USBTMC and SCPI compliant instrument I developed for the Pico. |
The test scenario:
- check if there's an instrument available
- validate the *IDN? string
- reset
- test all pins
Helper functions
*IDN? validation:
def test_idn():
isPico = False
idn = inst.query("*idn?");
print(idn)
# check if the IDN string matches the expected pattern. Allow for different serial number
if(re.search("PICO-PI,LABSWITCH,\d+,01.00\r\n", idn) != None):
isPico = (inst.is_4882_compliant)
return isPico
control a pin:
def set_pin(pin, on):
cmd = "DIGI:OUTP{} {}".format(pin, on)
inst.write(cmd)
get pin status:
def get_pin(pin):
cmd = "DIGI:OUTP{}?".format(pin)
return inst.query(cmd)
test a pin:
def test_pin(pin):
print("Status of pin {} before setting: {}".format(pin, get_pin(pin)))
set_pin(pin,1)
print("Status of pin {} after setting: {}".format(pin, get_pin(pin)))
time.sleep(1)
set_pin(pin,0)
print("Status of pin {} after resetting: {}".format(pin, get_pin(pin)))
The test program
The test code uses the functions above to validate settings, and loop over the pins:
#!/usr/bin/env python3
import pyvisa as visa
import time
import sys
# the functions from the previous section sit here
rm = visa.ResourceManager()
reslist = rm.list_resources("USB?::?*::INSTR")
print(reslist)
if (len(reslist) == 0):
sys.exit()
i = 0
inst = None
while i < len(reslist):
inst = rm.open_resource(reslist[i]);
inst.timeout = 3000
inst.clear()
print("+ IDN")
if(test_idn()):
print("Instrument found")
# reset
inst.write("*RST")
# test 3 pins
j = 0
while j < 3:
test_pin(j)
j += 1
print("SCPI errors during test: "+ inst.query("SYST:ERR:COUNT?"))
inst.close()
i += 1
print("Test complete")
The skeleton of this test is taken from the TinyUSB USBTMC Python unit test.
Test output:
PS C:\Users\jancu\Documents\Pico\my_examples\scpi_switch_usbtmc> & C:/Users/jancu/AppData/Local/Microsoft/WindowsApps/python3.9.exe c:/Users/jancu/Documents/Pico/my_examples/scpi_switch_usbtmc/test/visaQuery.py
('USB0::0xCAFE::0x4000::123456::INSTR',)
+ IDN
PICO-PI,LABSWITCH,0,01.00
Instrument found
Status of pin 0 before setting: 0
Status of pin 0 after setting: 1
Status of pin 0 after resetting: 0
Status of pin 1 before setting: 0
Status of pin 1 after setting: 1
Status of pin 1 after resetting: 0
Status of pin 2 before setting: 0
Status of pin 2 after setting: 1
Status of pin 2 after resetting: 0
SCPI errors during test: 0
Test complete
PS C:\Users\jancu\Documents\Pico\my_examples\scpi_switch_usbtmc>
During the execution, each of the 3 pins will be set high for a second, then set back to low. You can test that with LEDs or a DMM.
Python code
#!/usr/bin/env python3 import pyvisa as visa import time import sys import re def test_idn(): isPico = False idn = inst.query("*idn?"); print(idn) # check if the IDN string matches the expected pattern. Allow for different serial number if(re.search("PICO-PI,LABSWITCH,\d+,01.00\r\n", idn) != None): isPico = (inst.is_4882_compliant) return isPico def set_pin(pin, on): cmd = "DIGI:OUTP{} {}".format(pin, on) inst.write(cmd) def get_pin(pin): cmd = "DIGI:OUTP{}?".format(pin) return inst.query(cmd) def test_pin(pin): print("Status of pin {} before setting: {}".format(pin, get_pin(pin))) set_pin(pin,1) print("Status of pin {} after setting: {}".format(pin, get_pin(pin))) time.sleep(1) set_pin(pin,0) print("Status of pin {} after resetting: {}".format(pin, get_pin(pin))) rm = visa.ResourceManager() reslist = rm.list_resources("USB?::?*::INSTR") print(reslist) if (len(reslist) == 0): sys.exit() i = 0 inst = None while i < len(reslist): inst = rm.open_resource(reslist[i]); inst.timeout = 3000 inst.clear() print("+ IDN") if(test_idn()): print("Instrument found") # reset inst.write("*RST") # test 3 pins j = 0 while j < 3: test_pin(j) j += 1 print("SCPI errors during test: "+ inst.query("SYST:ERR:COUNT?")) inst.close() i += 1 print("Test complete")