2023-04-13 11:09:40 +02:00
|
|
|
import pyvisa
|
|
|
|
import numpy as np
|
2023-06-18 17:38:10 +02:00
|
|
|
import pkg_resources
|
2023-04-13 11:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Utility
|
|
|
|
"""
|
2023-06-18 17:38:10 +02:00
|
|
|
|
2023-04-13 11:09:40 +02:00
|
|
|
scripts = {
|
2023-06-18 17:38:10 +02:00
|
|
|
"buffer_reset": pkg_resources.resource_filename("m_teng", "keithley_scripts/buffer_reset.lua"),
|
|
|
|
"smua_reset": pkg_resources.resource_filename("m_teng", "keithley_scripts/smua_reset.lua"),
|
2023-04-13 11:09:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-18 17:38:10 +02:00
|
|
|
def init(beep_success=True):
|
2023-04-13 11:09:40 +02:00
|
|
|
rm = pyvisa.ResourceManager('@py')
|
|
|
|
resources = rm.list_resources()
|
|
|
|
if len(resources) < 1:
|
|
|
|
raise Exception("No resources found.")
|
|
|
|
elif len(resources) == 1:
|
|
|
|
print(f"Opening Resource {resources[0]}")
|
|
|
|
keithley = rm.open_resource(resources[0])
|
|
|
|
if beep_success: keithley.write("beeper.beep(0.5, 1000)")
|
|
|
|
return keithley
|
|
|
|
elif len(resources) > 1:
|
|
|
|
print(f"Resources: {resources}")
|
|
|
|
instr = int(input("Select an instrument (0-based index)"))
|
|
|
|
keithley = rm.open_resource(resources[instr])
|
|
|
|
if beep_success: keithley.write("beeper.beep(0.5, 1000)")
|
|
|
|
return keithley
|
|
|
|
|
2023-07-01 16:59:54 +02:00
|
|
|
def beep(instr, length=0.5, pitch=1000):
|
|
|
|
instr.write(f"beeper.beep({length}, {pitch})")
|
2023-04-13 11:09:40 +02:00
|
|
|
|
2023-06-24 12:28:21 +02:00
|
|
|
def exit(instr):
|
|
|
|
instr.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-13 11:09:40 +02:00
|
|
|
def run_lua(instr, script_path, verbose=False):
|
|
|
|
"""
|
|
|
|
Run a lua script from the host on the instrument
|
|
|
|
@param instr : pyvisa instrument
|
|
|
|
@param script_path : full path to the script
|
|
|
|
"""
|
|
|
|
with open(script_path, "r") as file:
|
|
|
|
script = file.read()
|
|
|
|
if verbose: print(f"Running script: {script_path}")
|
|
|
|
instr.write(script)
|
|
|
|
|
|
|
|
|
|
|
|
def reset(instr, verbose=False):
|
|
|
|
"""
|
|
|
|
Reset smua and its buffers
|
|
|
|
@param instr : pyvisa instrument
|
|
|
|
"""
|
|
|
|
run_lua(instr, scripts["smua_reset"], verbose=verbose)
|
|
|
|
run_lua(instr, scripts["buffer_reset"], verbose=verbose)
|
|
|
|
|
2023-06-18 17:38:10 +02:00
|
|
|
def get_buffer_name(buffer_nr: int):
|
|
|
|
if buffer_nr == 2: return "smua.nvbuffer2"
|
|
|
|
elif buffer_nr == 1: return "smua.nvbuffer1"
|
|
|
|
raise ValueError(f"Invalid buffer_nr: {buffer_nr}")
|
|
|
|
|
|
|
|
def get_buffer_size(instr, buffer_nr=1):
|
|
|
|
n = instr.query(f"print({get_buffer_name(buffer_nr)}.n)").strip("\n")
|
|
|
|
return int(float(n))
|
|
|
|
|
2023-04-13 11:09:40 +02:00
|
|
|
|
2023-04-13 17:50:40 +02:00
|
|
|
def collect_buffer(instr, buffer_nr=1, verbose=False):
|
2023-04-13 11:09:40 +02:00
|
|
|
"""
|
|
|
|
Get the buffer as 2D - np.array
|
|
|
|
@param instr : pyvisa instrument
|
|
|
|
@param buffer_nr : 1 or 2, for smua.nvbuffer1 or 2
|
|
|
|
@returns 2D numpy array:
|
|
|
|
i - ith reading:
|
|
|
|
0: timestamps
|
|
|
|
1: readings
|
|
|
|
"""
|
2023-06-18 17:38:10 +02:00
|
|
|
buffername = get_buffer_name(buffer_nr)
|
2023-04-13 11:09:40 +02:00
|
|
|
# instr.write("format.data = format.DREAL\nformat.byteorder = format.LITTLEENDIAN")
|
|
|
|
# buffer = instr.query_binary_values(f"printbuffer(1, {buffername}.n, {buffername})", datatype='d', container=np.array)
|
|
|
|
instr.write("format.data = format.ASCII\nformat.asciiprecision = 7")
|
|
|
|
timestamps = instr.query_ascii_values(f"printbuffer(1, {buffername}.n, {buffername}.timestamps)", container=np.array)
|
|
|
|
readings = instr.query_ascii_values(f"printbuffer(1, {buffername}.n, {buffername}.readings)", container=np.array)
|
2023-04-13 17:50:40 +02:00
|
|
|
if verbose:
|
|
|
|
print(f"readings from {buffername}: {readings}, \ntimestamps: {timestamps}")
|
2023-04-13 11:09:40 +02:00
|
|
|
buffer = np.vstack((timestamps, readings)).T
|
|
|
|
return buffer
|
|
|
|
|
2023-06-18 17:38:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def collect_buffer_range(instr, range_=(1, -1), buffer_nr=1, verbose=False):
|
|
|
|
"""
|
|
|
|
Get the buffer as 2D - np.array
|
|
|
|
@param instr : pyvisa instrument
|
|
|
|
@param buffer_nr : 1 or 2, for smua.nvbuffer1 or 2
|
|
|
|
@returns 2D numpy array:
|
|
|
|
i - ith reading:
|
|
|
|
0: timestamps
|
|
|
|
1: readings
|
|
|
|
"""
|
|
|
|
buffername = get_buffer_name(buffer_nr)
|
|
|
|
# instr.write("format.data = format.DREAL\nformat.byteorder = format.LITTLEENDIAN")
|
|
|
|
# buffer = instr.query_binary_values(f"printbuffer(1, {buffername}.n, {buffername})", datatype='d', container=np.array)
|
|
|
|
if range_[1] == -1:
|
|
|
|
range_ = (range_[0], f"{buffername}.n")
|
|
|
|
instr.write("format.data = format.ASCII\nformat.asciiprecision = 7")
|
|
|
|
timestamps = instr.query_ascii_values(f"printbuffer({range_[0]}, {range_[1]}, {buffername}.timestamps)", container=np.array)
|
|
|
|
readings = instr.query_ascii_values(f"printbuffer({range_[0]}, {range_[1]}, {buffername}.readings)", container=np.array)
|
|
|
|
if verbose:
|
|
|
|
print(f"readings from {buffername}: {readings}, \ntimestamps: {timestamps}")
|
|
|
|
buffer = np.vstack((timestamps, readings)).T
|
|
|
|
return buffer
|
|
|
|
|
|
|
|
|
|
|
|
|