“'int'对象没有属性'split':IV-使用横河GS 200在Python中绘制曲线

6xfqseft  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(204)

目前正在尝试用Python中的横河GS200绘制IV曲线。我是在Windows 10上使用Spyder与Python 3. 9的PyVISA新手。我的整个代码如下所示:

import numpy as np
import matplotlib.pyplot as plt
import sys
import os
import time
import pyvisa
import enum

#Values and names
CurrentCompliance = 0.2
CompVolt = 1
sample_name = 'NWL055E'
device_name = 'C4'
comments = 'light_on'
test_name = sample_name+'_'+device_name+comments

rm = pyvisa.ResourceManager()

yokogawa = rm.open_resource('GPIB::1::INSTR')

yokogawa.write("*IDN?")
print("ID of Yokogawa: ")
print( yokogawa.read())

# Setup yokogawa for  current loop
yokogawa.write("*RST")
yokogawa.write("SOUR:FUNC:MODE CURR")  # current source
yokogawa.write("SOUR:CURR 0")          # set current to 0
yokogawa.write('SENS:FUNC "VOLT"')   
yokogawa.write('FORM:ELEM VOLT')
yokogawa.write('SENS:VOLT:RANGE 3')
yokogawa.write("SENS:VOLT:PROT:LEV " + str(CompVolt))  # set voltage compliance
yokogawa.write(":OUTP ON")                             # turn on output
print("Gain Yokogawa initialized ...")

#Reset Source and setup up Yokogawa as voltage source
yokogawa.write("*RST")
time.sleep(0.2)
#yokogawa.write()
yokogawa.write(":SOUR:FUNC:MODE VOLT")
yokogawa.write(":SENS:CURR:PROT:LEV " + str(CurrentCompliance))
yokogawa.write(":SENS:CURR:RANGE:AUTO 1")
yokogawa.write(":OUTP ON")

Isource_max = 7e-6
step = .1e-6
numpoints = 100

Voltage = []
Current = [] 

for V in np.linspace(0, Isource_max, numpoints, endpoint=True):
    print("Voltage set to " + str(V) + " V")
    yokogawa.write(":SOUR:VOLT " + str(V))
    time.sleep(0.1)
    data = yokogawa.write(":READ?") # Returns string as (V,I)
    answer = data.split(",")
    I = eval( answer.pop(1) ) *1e3
    Current.append( I )
    vread = eval( answer.pop(0) )
    Voltage.append(vread)
    
    print("--> Current = " + str(Current[-1]) + ' mA')
   
yokogawa.write(":OUTP OFF")

#Set to Current Source
yokogawa.write(":SOUR:FUNC:MODE curr")
yokogawa.write(":SOUR:CURR " + str(CurrentCompliance))
yokogawa.write(":SENS:VOLT:PROT:LEV " + str(max(Voltage)) )
yokogawa.write(":SENS:VOLT:RANGE:AUTO 1")

yokogawa.write("SYSTEM:KEY 23")
yokogawa.close()
    
#plotting
    
plt.plot(Voltage, Current)
plt.title("IV- curve")
plt.xlabel("Voltage [V]")
plt.ylabel("Current [A]") 
plt.savefig("IV-curve.png")
plt.show()

运行该问题时,我得到一个错误:

AttributeError: 'int' object has no attribute 'split'

该设备通过GPIB到USB连接到我的计算机。问题发生在for循环中,可能在数据行,因为它没有返回字符串。我现在的问题是:有没有办法用几行代码就解决这个问题?或者我的程序内部的逻辑是错误的?有人能帮我吗?
我试着将write命令改为query和ask,但是使用ask:

data = yokogawa.ask(":READ?")

我得到

AttributeError: 'GPIBInstrument' object has no attribute 'ask'

如果我提出质疑

IndexError: pop index out of range

这可能是因为它返回的是整数,所以不返回字符串。
提前感谢并问候

szqfcxe2

szqfcxe21#

错误消息“属性错误:“int”对象没有属性“split”“,因为变量“data”被设置为函数yokogawa.write(“:READ?”)而不是yokogawa.query(“:READ?”)的返回值。
这将导致变量“data”为整数,而整数没有属性“split”,这就是抛出错误的原因。要解决此问题,应将行“data = yokogawa.write(“:READ?”)”更改为“data = yokogawa.query(“:READ?”)"。

相关问题