Skip to content

Instantly share code, notes, and snippets.

@SamThomas
Created July 11, 2012 08:10
Show Gist options
  • Save SamThomas/3088875 to your computer and use it in GitHub Desktop.
Save SamThomas/3088875 to your computer and use it in GitHub Desktop.
Agilent 34401A driver
from SCPI import SCPI
import time
class Agilent34410ADriver(SCPI):
def __init__(self):
SCPI.__init__(self,'COM1','serial')
#self.scpi_comm("SYST:REM")
#self.scpi_comm("*RST")
#self.scpi_comm("CLS")
#self.scpi_comm("IDN?")
self.ResetDevice()
self.DeviceClear()
self.scpi_comm("*CLS")
self.ReadSoftwareVersion()
def configCurrentMeasurement(self):
self.scpi_comm("CONFIGURE:CURRENT:DC") #Take parameter to also be able to select AC
return(True)
def configResistanceMeasurement(self):
self.scpi_comm("CONFIGURE:RESISTANCE") #Take parameter to also be able to select 4W
return(True)
def selectMeasurementFunction(self,function):
values = ['CAPACITANCE','CONTINUITY','CURRENT','DIODE','FREQUENCY','RESISTANCE','TEMPERATURE','VOLTAGE']
return_value = False
if function in values:
return_value = True
function_string = "FUNCTION " + "\"" + function + "\""
self.scpi_comm(function_string)
return(return_value)
def readConfiguration(self):
response = self.scpi_comm("CONFIGURE?")
response = response.replace(' ',',')
conf = response.split(',')
conf_string = "Measurement type: " + conf[0] + "\nRange: " + conf[1] + "\nResolution: " + conf[2]
return(conf_string)
def setAutoInputZ(self, auto=False):
if auto:
self.scpi_comm("VOLT:IMP:AUTO ON")
else:
self.scpi_comm("VOLT:IMP:AUTO OFF")
# measure 5 times per second and return each measures
def read(self):
time.sleep(0.2)
self.scpi_comm("SYST:REM")
value = float(self.scpi_comm("MEAS:VOLT:DC?"))
# value = float(self.scpi_comm("READ?"))
return value
#--------------------------------------------------------------------------------------------------------------------------------
#stand alone test
##if __name__ == "__main__":
## voltmeter=Agilent34410ADriver()
## volt=0.123456
## #volt=voltmeter.read()
## with open('Read_voltage.csv','w') as f:
## f.write("%f" %(volt))
## #entry keyboard to avoid to go out of the .py
## raw_input()
#---------------------------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Name: conversion tension - temperature
# Purpose: calcul de la temperature a partir de la tension mesuree par un thermocouple type K
#
# Author: Samuel Thomas
#
# Created: 02/07/2012
# Copyright: (c) Samuel Thomas 2012
#-------------------------------------------------------------------------------
#------------------------------
import agilent_34410A as dmm
#from agilent_34410A import *
import math
import os
#------------------------------
# Declaration & initialisation variables
driver = dmm.Agilent34410ADriver()
# INIT AUTO CONFIG VOLT
driver.setAutoInputZ(1)
driver.selectMeasurementFunction('VOLTAGE')
i=0
j=0
voltage=0.0
max_i=1000
temperature=0
differential_temperature=0
#voltage= driver.read()
tab1=[]
tab2=[]
# coefficients for Temperature range: -200 to 0C / Voltage range: -5.891 to 0mV
tab1=[0 , 2.51734622*10**-1 , -1.1662878 , -1.0833638 , -8.9773540*10**-1 , -3.7342377*10**-1 , -8.6632643*10**-2 , -1.0450598*10**-2 , -5.1920577*10**-4 , 0 ]
# coefficients for Temperature range: 0 to 500C / Voltage range: 0 to 20.644mV
tab2=[0 , 2.508355*10 , 7.860106*10**-2 , -2.503131*10**-1 , 8.315270*10**-2 , -1.228034*10**-2 , 9.804036*10**-4 , -4.413030*10**-5 , 1.057734*10**-6 , -1.052755*10**-8 ,0 ]
#----------------------------------------------------------------------------------------
minV=-100.0
maxV=100.0
avgV=0
# read loop: print 1000 measures
while i < max_i:
voltage= driver.read()
# Si la tension mesuree est negative, calcul de la temperature avec les coeff contenus dans tab1
if voltage <= 0:
differential_temperature = tab1[0] + tab1[1]*voltage + tab1[2]*voltage**2 + tab1[3]*voltage**3 + tab1[4]*voltage**4 + tab1[5]*voltage**5 + tab1[6]*voltage**6 + tab1[7]*voltage**7 + tab1[8]*voltage**8 + tab1[9]*voltage**9 + tab1[10]*voltage**10
# Si la tension mesuree est negative, calcul de la temperature avec les coeff contenus dans tab2
else:
differential_temperature = tab2[0] + tab2[1]*voltage + tab2[2]*voltage**2 + tab2[3]*voltage**3 + tab2[4]*voltage**4 + tab2[5]*voltage**5 + tab2[6]*voltage**6 + tab2[7]*voltage**7 + tab2[8]*voltage**8 + tab2[9]*voltage**9 + tab2[10]*voltage**10
temperature = differential_temperature + 25
print temperature
## #Test Francois
## # if (minV<=voltage): minV=voltage
## # if (maxV>=voltage): maxV=voltage
## # avgV=avgV+voltage
i+=1
###Affichage test Francois
###print "max,min,avg: " ,maxV, minV, (avgV/i)
import time
import serial
import random
class SCPI:
def __init__(self,device,port):
self.device = device
self.port = port
try:
if self.port == 'file':
f = open(self.device, 'w')
if self.port == 'serial':
# modif-test: on remplace rtscts=true par xonxoff=True
#f = serial.Serial(self.device, 9600, timeout=1,xonxoff=True)
f = serial.Serial(self.device, 9600, timeout=1,rtscts=True)
f.close()
self.debug = False
except:
self.debug = True
def Remote(self):
self.scpi_comm("SYST:REM")
def scpi_comm(self,command):
if self.debug:
return str(random.random())
if self.port == 'file':
f = open(self.device, 'w')
if self.port == 'serial':
#f = serial.Serial(self.device, 9600, timeout=1,xonxoff=True)
f = serial.Serial(self.device, 9600, timeout=1,rtscts=True)
command = command + '\n'
f.write(command)
f.close()
time.sleep(0.01)
return_string = ""
if command.endswith('?') or command.endswith('?\n'):
a = ' '
if self.port == 'file':
f = open(self.device, 'r')
if self.port == 'serial':
f = serial.Serial(self.device, 9600, timeout=1)
return_string = f.readline()
while not (ord('a') == 10):
a = f.read(1)
return_string += a
f.close()
return return_string
def ReadSoftwareVersion(self, short=False):
version_string = self.scpi_comm("*IDN?")
return(version_string)
def ResetDevice(self):
self.scpi_comm("*RST")
return(True)
def DeviceClear(self):
self.scpi_comm("*abort")
return(True)
def ClearErrorQueue(self):
error = self.scpi_comm("*ESR?")
self.scpi_comm("*cls")
return(error)
@SamThomas
Copy link
Author

When I try to do data logging with this code, I have the "-113" error on the Agilent 34401A, which corresponds to:
"Undefined Header: a command was received that is not valid for this multimeter. You may have misspelled the command or it may not be a valid command. If you are using the short form of the command, remember that it may contain up to four letters".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment