Personal tools
You are here: Home Comunità zopemaster's Home Programmazione GPyB GPIB Python Interface

Daniele Paganelli


alias mythsmith

  • Blog
  • Feed RSS
    Iscriviti al diario, con il feed RSS (?)
  • Emopiano
    Imporvvisazioni al pianoforte ed alla tastiera (del computer :)
  • Università
    Documenti redatti per/a/con l'Università
  • Programmazione
    La mia limitata biblioteca personale di script, principalmente in Python.
  • Macropost
    Testi molto lunghi, inadatti per il blog.
  • Segnalibri
    I miei segnalibri direttamente da Firefox... quando mi ricordo di aggiornarli!

Sinapsi

  • AGESCI zona di Modena
    (curato da me, si nota? ;-)
  • Magnatune
    Etichetta discografica online. Musica di alta qualità con licenza CreativeCommons. Anteprime gratuite e complete di qualsiasi album... Attenzione: rischi di non uscirne più fuori!!!
  • Conoscere Linux
    Il Linux User Group di Modena, cui sono associato dalla fondazione.
  • Slack.it
    Wiki di un amico...
  • Punto Informatico
    I miei inutili interventi sulla rivista online più antica d'italia...
  • Oblivion999
    Un gruppo metal
  • Fax via eMail
    Il servizio che utilizzo per inviare e ricevere fax via eMail per lavoro.



Contatti Personali:
  • Jabber: dapa@jabber.org
    Per accedere alla rete Jabber usa Gaim oppure Google Talk
  • Posta: daniele.chioccia.modena1.it
  • OpenWengo: paganoide
  • Perché evito MSN?
 
Document Actions

GPIB Python Interface

by mythsmith last modified 2007-05-18 06:51 PM

Here you find classes to expose the libGPIB C library's functions to Python, through ctypes. This is the only useful document here!

"""This module contains classes to control general GPIB objects."""
from ctypes import cdll, c_buffer, byref
import string
import pickle
import sys


# Loading libgpib library in ctypes: lib=cdll.LoadLibrary('/usr/lib/libgpib.so')

status_tags={'0':('ERR','An error occurred during the call.'),
'1':('TIMO','A timeout has occurred.'),
'2':('END','EOI or EOS detected'),
'3':('SRQI','SRQ Interrupt Received. A device or controller is requesting service.'),
'4':('RQS','A device is requesting service'),
'5':('EEVENT','An event has occurred'),
'6':('SPOLL','The board has been serially polled'),
'7':('CMPL','All I/O operations completed'),
'8':('LOK','Board is in lockout state'),
'9':('RREM','The board is in remote state'),
'10':('CIC','Board is controller-in-charge'),
'11':('AATN','ATN line is asserted'),
'12':('TACS','The board has been addressed as a talker'),
'13':('LACS','The board has been addressed as a listener'),
'14':('DTAS','The board has received a device trigger command'),
'15':('DCAS','The board has received a device clear command')}
def report_status(ibsta):
"""This function take an error number (decimal), convert it to its binary representation (that returns as a string), and prints to stout the status bits codes and descriptions""" s='' while ibsta:
s = str(ibsta % 2) + s
ibsta/=2
l=len(s)
if l<16:
n=16-l
s='0'*n+s
print 'Status code %s' % s

index=0
for tag in s:
if tag=='1':
print status_tags[str(index)][0]+'\t'+status_tags[str(index)][1]
index+=1
return s

class CommonGPIB:
'''This class has attributes common to all GPIB objects (devices and boards).''' def __init__(self,name):
'''When a CommonGPIB object is created, __init__ try to initialize it, provides it with a descriptor and prints out information about the state of the new object''' self.name=name
self.ud=lib.ibfind(name)
if self.ud!=-1:
print 'Assigned descriptor '+str(self.ud)+' to '+name
self.ibsta=lib.ibclr(self.ud)
self.report_ibsta()
else:
print 'Unable to initialize '+name

def report_ibsta(self):
'''Verbosely reports the current value of ibsta''' report_status(self.ibsta)

def call(self,libfun,args):
'''This method calls name "libfun" from libgpib.so with arguments list/tuple "args". Eg: obj.call('wrt',('*idn?',5)) request identification from device 17.''' strngs=[] for arg in args:
arg=str(arg)
try:
int(arg)
strngs+=['int('+arg+')']
except:
strngs+=['"'+arg+'"']
cmd='self.ibsta=lib.'+libfun+'('+str(self.ud)+','+string.join(strngs,',')+')'
exec(cmd)
return self.ibsta

def wrt(self,strng):
'''This method writes a string to the GPIB object. Returns ibsta.''' try:
int(strng)
strng='int('+str(strng)+')'
except:
pass self.ibsta=lib.ibwrt(self.ud,strng,len(strng))
return self.ibsta

def wrta(self,strng):
'''This method writes asynchronously a string to the GPIB object. Returns ibsta.''' self.ibsta=lib.ibwrta(self.ud,strng,len(strng))
return self.ibsta

def rd(self,buffer_len=512):
'''Reads 512 bytes from device, prints the result and returns it. Returns (result, ibsta)''' rdb=c_buffer('@'*buffer_len)
self.ibsta=lib.ibrd(self.ud,byref(rdb),buffer_len)
self.red=rdb.value.replace('@','')
return self.red, self.ibsta

def rda(self,buffer_len=512):
'''Reads asynchronously 512 bytes from device, prints the result and returns it. Returns (result, ibsta)''' rdb=c_buffer('@'*buffer_len)
self.ibsta=lib.ibrda(self.ud,byref(rdb),buffer_len)
return rdb.value.replace('@',''), self.ibsta

def write_read(self,strng,buffer_len=512):
'''Writes data to device's buffer, then immediately reads it and returns Returns (result, wrinting ibsta, reading ibsta)''' staw=lib.ibwrt(self.ud,strng,len(strng))
rdb=c_buffer('@'*buffer_len)
self.ibstasta=lib.ibrd(self.ud,byref(rdb),buffer_len)
self.red=rdb.value.replace('@','')
sys.stdout.write(self.red)
return self.red, staw, self.ibsta

def write_reada(self,strng,buffer_len=512):
'''Writes data to device's buffer, then immediately reads it asynchronously and returns Returns (result, wrinting ibsta, reading ibsta)''' staw=lib.ibwrt(self.ud,strng,len(strng))
rdb=c_buffer('@'*buffer_len)
self.ibstasta=lib.ibrda(self.ud,byref(rdb),buffer_len)
self.red=rdb.value.replace('@','')
sys.stdout.write(self.red)
return prnt, staw, self.ibsta

def writea_read(self,strng,buffer_len=512):
'''Asynchronously writes data to device's buffer, then immediately reads it and returns Returns (result, wrinting ibsta, reading ibsta)''' staw=lib.ibwrta(self.ud,strng,len(strng))
rdb=c_buffer('@'*buffer_len)
self.ibstasta=lib.ibrd(self.ud,byref(rdb),buffer_len)
self.red=self.rdb.value.replace('@','')
sys.stdout.write(self.red)
return prnt, staw, self.ibsta

def writea_reada(self,strng,buffer_len=512):
'''Asynchronously writes data to device's buffer, then immediately reads it asynchronously and returns Returns (result, wrinting ibsta, reading ibsta)''' staw=lib.ibwrta(self.ud,strng,len(strng))
rdb=c_buffer('@'*buffer_len)
self.ibstasta=lib.ibrda(self.ud,byref(rdb),buffer_len)
prnt=self.rdb.value.replace('@','')
print prnt
return prnt, staw, self.ibsta

def clr(self):
'''Send ibclr do GPIB object.''' self.ibsta=lib.ibclr(self.ud)
return self.ibsta


########################################### class InterfaceGPIB(CommonGPIB):
'''Common board methods.''' def sre(self,bool):
'''If bool=1, sets remote mode for interface. Unsets it if =0''' self.ibsta=lib.ibsre(self.ud,bool)

########################################### class PeripheralGPIB(CommonGPIB):
'''Common device methods''' def rsp(self):
rdb=c_buffer('@'*512)
self.ibsta=lib.ibrsp(self.ud,byref(rdb))
prnt=rdb.value.replace('@','')
sys.stdout.write(prnt)

return prnt, self.ibsta


########################################### class CommonExperiment:
'''This class contains methods useful within experiment classes.''' def write_list(self,dictionary):
'''Writes a list of commands to destination. Syntax:] {'destination':['command1','command2',...], 'destination2:['command1','command2',...], ...}''' for destination in dictionary.keys():
for command in dictionary.get(destination):
if command!='read':
cmd='self.'+destination+'.wrt("'+command+'")'
exec(cmd)
else:
cmd='self.'+destination+'.rd()' # The red data will be stored in self.destination.red (see rd in CommonGPIB) exec(cmd)




hosting
Iniziative che sponsorizziamo:
Scopri altri siti scout!
Sito precedente 5o sito precedente Home ScoutRing Lista dei Siti Sito Casuale Sito successivo 5o sito successivo
Informati e protesta contro il DRM!
Powered by Plone, the Open Source Content Management System