National Intruments GPIB

Dietmar Schwertberger dietmar at schwertberger.de
Wed Mar 7 16:55:56 EST 2001


Hi Ken!

In article <3AA46593.753BAE1 at newfocus.com>, Ken Koller
<URL:mailto:kkoller at newfocus.com> wrote:
> Has anyone written a python interface to some of the National
> Instruments libraries? I'm in the process of using Python to connect to
> some instrumentation and I'm writing the C code to interface to their
> GPIB API. Was hoping someone else had done it :)

Instead of using C you could just call the DLL using windll/calldll.


Example(s):
(not necessarily functional)

= NI_GPIB =================================================================
[snip]

from GPIB import *

import windll, calldll
gpib = windll.module("gpib-32")

def ibdev(BdIndx=0, address=(0,0), more=None):
    '''ibdev(BdIndx=0, address=(0, 0), more=(11,1,0)):
    Open and initialize a device descriptor (handle) for
    use with subsequent calls for e.g. ibwrt and ibrd.
        BdIndx Index of the access interface for the device
        address  The (primary, secondary) GPIB address of the device
        more     (The I/O timeout value, EOI mode of the device, EOS character and modes)
    Returns the device descriptor or raises GPIBError.
    '''
    if more==None:
        more=(11,1,0)
    handle = gpib.ibdev( BdIndx, address[0], address[1], more[0], more[1], more[2])
    if handle > 0:
        return handle
    _raise( GPIBIOError, "ibdev(BdIndx=%d, address=%s, more=%s): Could not get handle."%(BdIndx, str(address), str(more)) )

get_handle=ibdev

def ibwrt(handle, s):
    '''ibwrt(handle, s):
    Write data to a device from a user buffer.
        handle: Interface or device descriptor
        s:      bytes to write
    Returns None or raises GPIBIOError (or GPIBTimeoutError).
    '''
    wrbuffer = windll.cstring(s,len(s))
    ibsta = gpib.ibwrt(handle,wrbuffer.address(),len(wrbuffer))
    if ibsta <> _iocompletedstatus:
        _raise(GPIBIOError, "ibwrt(handle=%d, s='%s'): Could not send string."%(handle, s), ibsta )

write = ibwrt


[snip]

boardID = 0
deviceaddress = (9,0)

# low level:
handle = ibdev(boardID, l)
ibwrt(handle, "*IDN?")

# higher level
bus = NI_GPIBInterface(boardID)
device = GPIBDevice(bus, address)
device.write("*IDN")

# highest level
multimeter1 = HP34970A(bus, address)
voltage = multimeter1.measure(range="10VAC", channel=5)

=========================================================================


I'm still working on the highest level classes for multimeters, sources,
source-monitor-units, relais scanners, ...


You may also try a search at google/deja for a posting by Sam Schulenburg:
# Forum: comp.lang.python
# Thread: Python and National Instruments
# Subject: Re: Python and National Instruments
# Date: 03/15/2000
# Author: Sam Schulenburg <samschul at pacbell.net>



Regards,

Dietmar




More information about the Python-list mailing list