element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pico as USB test device - part 4: Python PyVISA test bed
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 5 Feb 2023 9:09 AM Date Created
  • Views 299 views
  • Likes 7 likes
  • Comments 2 comments
Related
Recommended
  • raspberry
  • pico_usbtmc_scpi
  • pico
  • scpi
  • pyvisa

Raspberry Pico as USB test device - part 4: Python PyVISA test bed

Jan Cumps
Jan Cumps
5 Feb 2023
Raspberry Pico as USB test device - part 4: Python PyVISA test bed

In this post, I test the USBTMC and SCPI compliant instrument I developed for the Pico.
I use a PyVISA script to check if the instrument is connected. Then the outputs are switched and validated.

image

The test scenario:

  • check if there's an instrument available
  • validate the *IDN? string
  • reset
  • test all pins

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

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")

visaQuery_20230205.zip

link to all posts

  • Sign in to reply
  • DAB
    DAB 1 month ago

    Good post Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago

    VSCode detected that this was the first Python file I made since the installation. It suggested to install the Python extension.

    image

    This immediately gives colour highlighting, auto-complete, output capture, run and debug functionality. Neat.

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube