problem with usbtmc-communication

wrw at wrw at
Wed Dec 5 16:26:27 CET 2012

On Dec 4, 2012, at 11:12 AM, Jean Dubois <jeandubois314 at> wrote:

> On 4 dec, 15:33, w... at wrote:
>> On Dec 4, 2012, at 7:14 AM, Jean Dubois <jeandubois... at> wrote:
>>> The following test program which tries to communicate with a Keithley
>>> 2200 programmable power supply using usbtmc in Python does not work as

Is usbtmc a software layer (library of some sort) or some sort of hardware adapter?

>>> 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()

Without knowing anything about the usbtmc hardware/software it is hard to make real recommendations, but it seems pretty clear that you are being stepped on by a buffer problem of some sort.  I'm VERY suspicious of using of readline() as a way of getting the data out of the usbtmc thingy.  That makes python treat the Keithley as a file-like object and there are way too many ways the file pointer may not be where you think it is.  

I note that in your Octave example you are reading characters rather than lines.  It seems to me that you have two choices here, either do the equivalent in python or dig through the Keithley documentation to find the hex codes that usbtmc is presumably translating your commands into.  If you can find those, and if you are interested, I can send you off-line the handler I wrote a couple of years ago that used a dictionary to translate English commands into hex, then assembled those with a checksum and sent the string out to a Keyspan usb to serial converter.


More information about the Python-list mailing list