Python and National Instruments
Sam Schulenburg
samschul at pacbell.net
Wed Mar 15 13:19:42 EST 2000
In article <8aoann$ooe$1 at nnrp1.deja.com>,
wdbeal at my-deja.com wrote:
> Has anyone tried to interface with National Instruments driver
libraries
> under Windows. I'm interested in using Python for test and measurement
> programs.
> Bill Beal
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
I have use Python to access National Inst. dll's (GPIB, NADAQ) via the
calldll, and windll routines that can be found incorporasted in dynwin
at http://www.nightmare.com/~rushing/dynwin/
The following code shows how I used these routines:
// ================ Start snipit ==================
from aspi import * # My Routines and timers
import windll # Get the interface to access dll's
import structob # Get the 'c' language structure interface
gpib = windll.module('gpib-32') # load and get handle to gpib dll
cstring = windll.cstring # Object to map python strings to c
strings
GpibHandle = 0
def GetDevice(BoardAddr=0,PriAddr=0,SecAddr = 0,Timeout = 13):
global GpibHandle
GpibHandle = gpib.ibdev( BoardAddr, # Board address
PriAddr, # Primary device address
SecAddr, # Secondary device address
Timeout, # Timeout
1, # EOI flag
0) # EOS byte
if GpibHandle > 0:
return GpibHandle
else:
print 'Error in obtaining device handle'
def TestGPIB():
global GpibHandle
wrbuffer = cstring("*RST",4) # Identify command
rc = gpib.ibwrt(GpibHandle,wrbuffer.address(),len(wrbuffer))
print 'rc = 0X%X'%rc
if rc != 0x100:
print "Error in ibwrt"
return
wait(1000.0)
wrbuffer = cstring("*IDN?",5) # Identify command
rdbuffer = cstring("",1000) # 1000 character buffer
rc = gpib.ibwrt(GpibHandle,wrbuffer.address(),len(wrbuffer))
print 'rc = 0X%X'%rc
if rc != 0x100:
print "Error in ibwrt"
return
rc = gpib.ibrd(GpibHandle,rdbuffer.address(),200L)
print 'rc = 0X%X'%rc
if rc != 0x2100:
print "Error in ibrd"
return
return rdbuffer
def GPIBWrt(string):
global GpibHandle
wrbuffer = cstring(string,len(string))
rc = gpib.ibwrt(GpibHandle,wrbuffer.address(),len(wrbuffer))
if rc != 0x100:
print "Error in GPIBWwrt"
return
def GPIBRd():
global GpibHandle
rdbuffer = cstring("",1000) # 1000 character buffer
rc = gpib.ibrd(GpibHandle,rdbuffer.address(),200L)
if rc != 0x2100:
print "Error in GPIBRd"
return
return rdbuffer
def GPIBClr():
global GpibHandle
rc = gpib.ibclr(GpibHandle)
if rc != 0x100:
print "Error in GPIBClr"
return
def RampUpFreq(StartFreq=2100,EndFreq=38400,Trigger=0):
""" Will ramp HP8110A from StartFreq to EndFreq
and set Trigger to external or internal mode"""
EndFreq= EndFreq+1
GPIBClr()
wait(100)
GPIBWrt("*RST")
wait(100) # My delay routine in aspi.py
GPIBWrt("VOLT1 5.0V")
GPIBWrt("OUTPUT1 ON")
#Setup initial frequency
wrbuffer = "FREQ %d"%(StartFreq)
GPIBWrt(wrbuffer)
raw_input("Turn on MCS and then hit enter")
for i in range(StartFreq,EndFreq,1000):
wrbuffer = "FREQ %d"%(i)
GPIBWrt(wrbuffer)
wait(20) # My delay routine in aspi.py
if Trigger == 0:
GPIBWrt("ARM:EWID:STATE ON")
print "HP8110A is in external triger mode"
else:
print "HP8110A is in Continuous triger mode"
def RampDownFreq():
""" Will ramp HP8110A from it's present to 2.1khz"""
GPIBWrt("ARM:EWID:STATE OFF")
wait(20)
GPIBWrt("FREQ?")
StartFreq = GPIBRd()
StartFreq = int(atof(StartFreq.trunc()))
EndFreq = 2000
print "StartFreq = ",StartFreq," EndFreq = ",EndFreq
for i in range(StartFreq,EndFreq,-1000):
wrbuffer = "FREQ %d"%(i)
GPIBWrt(wrbuffer)
wait(20) # My delay routine in aspi .py
//===================== end snipit ===============
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list