Last active
April 22, 2022 11:40
-
-
Save cfangmeier/1859de7d67ab74099a3b to your computer and use it in GitHub Desktop.
Creates an IV curve using Keithley 2410 Sweep Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import serial | |
import matplotlib.pyplot as plt | |
script = """ | |
# hashes make comments | |
# @ on a read specifies the parser for the read data | |
*RST | |
:SENS:FUNC:CONC OFF #Turn off concurrent functions | |
:SOUR:FUNC VOLT #Set supply to voltage | |
:SENS:FUNC 'CURR:DC' #Set measurement to DC current | |
:SENS:CURR:PROT 20E-3 #Set compliance current | |
:SOUR:VOLT:START 0 #Set Start Voltage | |
:SOUR:VOLT:STOP 10 #Set Stop Voltage | |
:SOUR:VOLT:STEP 1 #Set Voltage Step | |
:SOUR:VOLT:MODE SWE #Enable sweep mode | |
:SOUR:SWE:RANG AUTO #Set range to auto | |
:SOUR:SWE:SPAC LIN #Use linear sweep | |
:TRIG:COUN 10 #Number of measurements(should be compatable with START|STOP|STEP) | |
:SOUR:DEL 0.1 #Delay between Voltage set and current measure | |
:FORM:ELEM VOLT,CURR #Format of output | |
:OUTP ON #Enable HV | |
:READ? @IV #Read back measurements | |
:OUTP OFF #Turn off output | |
""" | |
def process_script(): | |
global script | |
script = script.splitlines() | |
script = [line.split('#')[0].strip() for line in script] | |
script = [line for line in script if len(line) > 0] | |
for i, line in enumerate(script): | |
line = line.split('@') | |
if len(line) == 1: | |
script[i] = (line[0]+'\n',None) | |
else: | |
script[i] = (line[0].strip()+'\n',line[1]) | |
def read(ser): | |
data = bytes() | |
endl = '\n'.encode('ascii')[0] | |
while True: | |
data += ser.read(1) | |
if data[-1] == endl: | |
break | |
return data.decode('ascii') | |
def parse(parser, result): | |
if parser == "IV": | |
result = result.split(',') | |
current = [] | |
voltage = [] | |
for i, item in enumerate(result): | |
if not i % 2: | |
voltage.append(float(item)) | |
else: | |
current.append(float(item)) | |
for v, c in zip(voltage, current): | |
print("{0:+10.5e} {1:+10.5e}".format(v,c)) | |
plt.xlabel('Voltage(Volts)') | |
plt.ylabel('Current(Amps)') | |
plt.plot(voltage,current,'.') | |
else: | |
raise NotImplementedError("This parser is not implemented: {0}".format(parser)) | |
def main(): | |
global script | |
process_script() | |
with serial.Serial('/dev/ttyUSB1') as ser: | |
for command, parser in script: | |
print('==>'+command,end='') | |
ser.write(command.encode('ascii')) | |
if parser != None: | |
parse(parser, read(ser)) | |
ser.flushOutput() | |
plt.show() | |
if __name__ == '__main__': | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On 1 1k Resistor, The output is the following
Also creates a scatter-plot using matplotlib