[newbie] problem with usbtmc-communication

Terry Reedy tjreedy at udel.edu
Tue Dec 4 20:55:16 CET 2012


On 12/4/2012 7:14 AM, Jean Dubois 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)
[3 near repetitions snipped]

This sort of repetitious code without even line breaks is painful for me 
to read. Python has looping statements for a reason. Replace all four 
nearly identical blocks with the following. (If you are not familiar 
with built-in enumerate, you should be. Read its entry in the library 
manual.)

for number, current_in in enumerate(
     ('0.025', '0.050'. '0.075', '0.100'), 1)
   usbkeith.write(":SOUR:CURR %s\n" % current_in)
   ...
   print 'Measured current %d: ' % number, measurementcurr
   ...
   print 'Measured voltage %d: ' % number, measurementvolt

Now you can make changes in only one place and easily add more test values.

> 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?

No, but if both the python and octave programs used loops, it would be 
easier to see if both are doing the same things before, within, and 
after the loop.

-- 
Terry Jan Reedy




More information about the Python-list mailing list