problem with usbtmc-communication

Jean Dubois jeandubois314 at gmail.com
Wed Dec 12 11:16:21 EST 2012


On 12 dec, 01:49, Jerry Hill <malaclyp... at gmail.com> wrote:
> On Tue, Dec 11, 2012 at 1:58 AM, Jean Dubois <jeandubois... at gmail.com> wrote:
>
> > I found examples in theusbtmckernel driver documentation (the
> > examples there are given in C):
> >http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&l...
>
> Thanks for that link.  I think it explains how the driver works pretty
> well.  I haven't done any work with devices like this, but I see a few
> things in those docs that might help.
>
> In their example code, they open the device with: open(“/dev/usbtmc1”,O_RDWR);
>
> That's not exactly the same as what you've been doing.  I would try
> opening the file this way in python:
> usb_device = open('/dev/usbtmc1', 'w+', buffering=0)
>
> That truncates the file after it opening it, and disables any
> buffering that might be going on.
>
> Then, I would try writing to the device with usb_device.write() and
> usb_device.read().  read() attempts to read to end-of-file, and based
> on the docs, the driver will work okay that way.  Doing that, along
> with turning off buffering when you open the file, should eliminate
> any issues with the driver failing to emit newlines someplace.
>
> Personally, I would probably try playing with the device from python's
> interactive interpreter.  I think that could shed a lot of light on
> the behavior you're seeing.
>
> --
> Jerry

Thanks a thousand times Jerry!!!, the buffering issue has disappeared
after following your recommendations. The test program now looks like
below and performs as expected.

#!/usr/bin/python
import time
import os
import sys
timesleepdefault=5
print "Enter name of data file",
filename = raw_input()
#the following line is very important, especially the buffering=0
usbkeith = open('/dev/usbtmc1','w+', buffering=0)
usbkeith.write("*IDN?\n")
identification=usbkeith.read().strip()
print 'Found device: ',identification
usbkeith.write("SYST:REM" + "\n")
usbkeith.write(":SENS:VOLT:PROT 1.5\n")
keithdata = open(filename,'w')
usbkeith.write(":OUTP:STAT ON\n")
for number, current_in in enumerate(('0.025', '0.050', '0.075',
'0.100'), 1):
   usbkeith.write(":SOUR:CURR %s\n" % current_in)
   time.sleep(timesleepdefault)
   usbkeith.write(":MEAS:CURR?\n")
   measurementcurr=usbkeith.read()
   print 'Measured current %d: ' % number, measurementcurr
   usbkeith.write(":MEAS:VOLT?\n")
   measurementvolt=usbkeith.read()
   print 'Measured voltage %d: ' % number, measurementvolt
   keithdata.write(measurementcurr.strip()+' '+measurementvolt)
usbkeith.write(":OUTP:STAT OFF\n")
print "Goodbye, data logged in file:"
print filename
usbkeith.close()
keithdata.close()

regards,
Jean



More information about the Python-list mailing list