# HG changeset patch # User David Douard # Date 1197317955 -3600 # Node ID cd9efa64f6dac78891bf0154d7210fadd48a51dd # Parent 0670b1f5c155aeb7c1f0caa2e287b8b2eaa17911 added a gpib module (handle gpib connection) diff -r 0670b1f5c155 -r cd9efa64f6da gpib.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpib.py Mon Dec 10 21:19:15 2007 +0100 @@ -0,0 +1,43 @@ +""" +gpib: create serial connection to GPIB-USB device (ProLogix is the +only supported device for now). +""" +import serial +from serial.serialutil import SerialException +import time + +class ConnectionError(Exception): + pass + +class GPIB(object): + _retries = 10 + def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, + address=0, mode=1): + self._cnx = serial.Serial(port=device, baudrate=baudrate, timeout=timeout) + self._timeout = timeout + + self.set_mode(mode) + if mode == 1: + self.set_address(address) + + def _set_cmd(self, cmd, value): + self._cnx.write('++%s %d\r'%(cmd, value)) + rval = self._read() + if not rval.isdigit() or int(rval) != value: + raise ConnectionError("Can't set GPIB %s to %s"%(cmd, value)) + + def set_address(self, address): + self._set_cmd('addr', address) + self._adress = address + + def set_mode(self, mode): + self._set_cmd('mode', mode) + self._mode = mode + + def _read(self): + for i in range(self._retries): + rdata = self._cnx.readline() + if rdata.strip() != "": + break + time.sleep(self._timeout) + return rdata