problem with usbtmc-communication

Jean Dubois jeandubois314 at gmail.com
Tue Dec 4 11:12:35 EST 2012


On 4 dec, 15:33, w... at mac.com wrote:
> On Dec 4, 2012, at 7:14 AM, Jean Dubois <jeandubois... at gmail.com> wrote:
>
>
>
> > The following test program which tries to communicate with a Keithley
> > 2200 programmable power supply using usbtmc in Python does not work as
> > expected. I have connected a 10 ohm resistor to its terminals and I
> > apply 0.025A, 0.050A, 0.075A en 0.1A,
> > I then measure the current and the voltage en write them in a file
> > De data produced looks like this:
> > 0.00544643 0.254061; first current value is wrong, voltage value is
> > correct
> > 0.0250807 0.509289; second current value is wrong, but it corresponds
> > to the first, second voltage is correct
> > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to
> > the second, 3rd voltage is right
> > 0.075099 1.01792; 4th current value is wrong,  it corresponds to the
> > 3rd, 4th voltage is right
> >                            4th correct current value is missing
>
> > But is should be (numerical inaccuracy taking into account)(these data
> > were produced by a similar octave-program):
> > 0.0248947 0.254047
> > 0.0499105 0.509258
> > 0.0749044 0.764001
> > 0.0998926 1.01828
>
> > Here is the python-program:
> > #!/usr/bin/python
> > import time
> > import os
> > import sys
> > measurementcurr=''
> > measurementvolt=''
> > timesleepdefault=1
> > filename ='mydata.txt'
> > usbkeith = open('/dev/usbtmc1','r+')
> > usbkeith.flush()
> > usbkeith.write("*IDN?\n")
> > #strip blank line:
> > identification=usbkeith.readline().strip()
> > print 'Found device: ',identification
> > usbkeith.write("SYST:REM" + "\n")
> > usbkeith.write(":SENS:VOLT:PROT 1.5\n")
> > keithdata = open(filename,'w')
> > #start first measurement
> > usbkeith.write(":SOUR:CURR 0.025\n")
> > usbkeith.write(":OUTP:STAT ON\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write(":MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 1: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 1: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
> > #start second measurement
> > usbkeith.write("SOUR:CURR 0.050\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write("MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 2: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 2: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
> > #start 3rd measurement
> > time.sleep(timesleepdefault)
> > usbkeith.write("SOUR:CURR 0.075\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write("MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 3: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 3: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
> > #start 4th measurement
> > time.sleep(timesleepdefault)
> > usbkeith.write("SOUR:CURR 0.1\n")
> > time.sleep(timesleepdefault)
> > usbkeith.write("MEAS:CURR?\n")
> > time.sleep(timesleepdefault)
> > measurementcurr=usbkeith.readline()
> > print 'Measured current 4: ',measurementcurr
> > usbkeith.write("MEAS:VOLT?\n")
> > time.sleep(timesleepdefault)
> > measurementvolt=usbkeith.readline()
> > print 'Measured voltage 4: ',measurementvolt
> > keithdata.write(measurementcurr.strip()+' '+measurementvolt)
> > usbkeith.write(":OUTP:STAT OFF\n")
> > print "Goodbye, data logged in file:"
> > print filename
> > usbkeith.close()
> > keithdata.close()
>
> > can anyone here what is going wrong and how to get it right?
>
> > thanks
> > jean
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> Just guessing here - it looks as though you are setting the current and THEN turning the output on.  That might be the correct sequence or it might not.
>  If not, it would explain the offset between that and the subsequent readings.
I changed the order of the two commands (first turn the source on then
set the current) and this is the result
0.0994434 0.253431; first current value wrong but the value seems what
the last one should be!, voltage correct
0.0251083 0.508319; second current value wrong but this is what the
first should be, voltage correct
0.0501242 0.762834; 3rd current value wrong but this is what the
second current value should ve, voltage correct
0.0749226 1.0167: 4th current value wrong but this is what the 3rd
current value should be, voltage correct

>
> This is really a Keithley problem, not a Python problem.
I thought also it was a Keithley problem but then I programmed exactly
the same in octave and with octave I do get the correct results, so I
think now it is at least a Python-related problem. Here is the octave-
code so you see what I mean and maybe find what should be changed in
the Python-code.

clear
file_id = fopen('mydata.txt', 'w');
readbytes = 10000;
timesleepdefault=1
fd = usbtmc_open("/dev/usbtmc1");
usbtmc_write(fd,"*IDN?");
result = char(usbtmc_read(fd,readbytes))
usbtmc_write(fd,"SYST:REM");
usbtmc_write(fd,"SENS:VOLT:PROT 1.5");
#start 1st measurement
usbtmc_write(fd,"SOUR:CURR 0.025");
usbtmc_write(fd,"OUTP:STAT ON");
pause (timesleepdefault)
usbtmc_write(fd,"MEAS:CURR?");
resultcurr = char(usbtmc_read(fd,readbytes))
#strip line termination
resultcurr = resultcurr(1:end-1);
usbtmc_write(fd,"MEAS:VOLT?");
resultvolt = char(usbtmc_read(fd,readbytes))
fprintf(file_id,'%s',resultcurr,' ',resultvolt)
#start 2nd measurement
usbtmc_write(fd,"SOUR:CURR 0.050");
pause (timesleepdefault)
usbtmc_write(fd,"MEAS:CURR?");
resultcurr = char(usbtmc_read(fd,readbytes))
#strip line termination
resultcurr = resultcurr(1:end-1);
usbtmc_write(fd,"MEAS:VOLT?");
resultvolt = char(usbtmc_read(fd,readbytes))
fprintf(file_id,'%s',resultcurr,' ',resultvolt)
#start 3rd measurement
usbtmc_write(fd,"SOUR:CURR 0.075");
pause (timesleepdefault)
usbtmc_write(fd,"MEAS:CURR?");
resultcurr = char(usbtmc_read(fd,readbytes))
#strip line termination
resultcurr = resultcurr(1:end-1);
usbtmc_write(fd,"MEAS:VOLT?");
resultvolt = char(usbtmc_read(fd,readbytes))
fprintf(file_id, '%s',resultcurr,' ',resultvolt)
#start 4th measurement
usbtmc_write(fd,"SOUR:CURR 0.1");
pause (timesleepdefault)
usbtmc_write(fd,"MEAS:CURR?");
resultcurr = char(usbtmc_read(fd,readbytes))
#strip line termination
resultcurr = resultcurr(1:end-1);
usbtmc_write(fd,"MEAS:VOLT?");
resultvolt = char(usbtmc_read(fd,readbytes))
fprintf(file_id,'%s',resultcurr,' ', resultvolt)
usbtmc_close(fd);
fclose(file_id)




More information about the Python-list mailing list