Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!rt.uk.eu.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'sys': 0.05; 'filename': 0.07; 'line:': 0.07; 'subject:skip:c 10': 0.07; 'python': 0.09; '"\\n")': 0.09; '%s\\n"': 0.09; 'corresponds': 0.09; 'expected.': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:k 40': 0.09; 'terry': 0.09; 'wrong,': 0.09; 'read.': 0.13; 'library': 0.15; '%d:': 0.16; '(these': 0.16; 'account)': 0.16; 'blocks': 0.16; 'file:"': 0.16; 'looping': 0.16; 'measurement': 0.16; 'ohm': 0.16; 'programmable': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repetitions': 0.16; 'wrote:': 0.17; 'skip:i 40': 0.17; 'skip:u 30': 0.17; 'tries': 0.17; 'jan': 0.18; 'subject:] ': 0.19; 'changes': 0.20; 'skip:= 10': 0.20; 'sort': 0.21; 'import': 0.21; 'subject:problem': 0.22; 'nearly': 0.23; 'this:': 0.23; 'second': 0.24; 'connected': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:m 30': 0.26; 'am,': 0.27; 'first,': 0.27; 'skip:# 10': 0.27; 'replace': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'jean': 0.29; 'measure': 0.29; 'second,': 0.29; 'statements': 0.29; 'no,': 0.29; "skip:' 10": 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'right?': 0.33; 'values.': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'produced': 0.33; 'entry': 0.33; 'wrong': 0.34; 'built-in': 0.35; 'doing': 0.35; 'similar': 0.35; 'add': 0.36; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'be.': 0.36; 'subject:with': 0.36; 'test': 0.36; 'should': 0.36; 'does': 0.37; 'communicate': 0.37; 'data': 0.37; 'easier': 0.38; 'skip:o 20': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'apply': 0.39; 'easily': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'more': 0.63; 'here': 0.65; 'taking': 0.65; 'following.': 0.65; 'power': 0.74; '4th': 0.78; 'received:fios.verizon.net': 0.84; 'voltage': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: [newbie] problem with usbtmc-communication Date: Tue, 04 Dec 2012 14:55:16 -0500 References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354650937 news.xs4all.nl 6852 [2001:888:2000:d::a6]:60015 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34249 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