hpib.py 6.12 KB
Newer Older
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
1
2
3
4
import serial
import time
import re
import platform
5
6


7
8
9
10
11
12
13
14
15
16
17
18
class PortDevice(object):
    def command(self, hpib_address, command):
        raise NotImplementedError

    def query(self, hpib_address, queryStr, count=80):
        raise NotImplementedError

    def set_local(self, hpib_address):
        raise NotImplementedError


class PrologixGpibUsb(PortDevice):
19
    def __init__(self, port="", timeout=1, portFormat="%d", maxDeviceNum=9, verbose=False,
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
20
21
                 findDevices=False):
        if port == "":
22
            if platform.system() == "Linux":
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
23
24
25
26
27
28
29
                port = "/dev/ttyUSB0"
                portFormat = "%d"
                maxDeviceNum = 99
            elif platform.system() == "Darwin":
                port = "/dev/ttys000"
                portFormat = "%03d"
                maxDeviceNum = 999
30
31
            else:
                raise NotImplementedError("Windows is not supported")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
32
33
34
35
        if verbose:
            print "Looking for ports such as %s in range %d on %s" % \
                  ( port, maxDeviceNum, platform.system() )
        self._rematch=re.search(r"(/dev/tty.*[^0-9])([0-9]+$)",port)
36
37
38
39

        if not self._rematch:
            raise ValueError(port)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
40
41
42
43
44
45
46
47
48
49
50
        self._serialPort = None
        while int(self._rematch.group(2)) < maxDeviceNum:
            if verbose:
                print "trying ", port
            try:
                self._serialPort = serial.Serial(port,timeout=timeout)
                break
            except serial.SerialException:
                port=self._rematch.group(1) +\
                      (portFormat % (int(self._rematch.group(2))+1))
                self._rematch=re.search("(/dev/tty.*[^0-9])([0-9]+$)",port)
51
        if self._serialPort==None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
            raise serial.SerialException("Cannot find port")
        self._serialPort.write("++savecfg 0"       + chr(10))
        self._serialPort.write("++auto 0"          + chr(10))
        self._serialPort.write("++eoi 1"           + chr(10))
        self._serialPort.write("++eos 2"           + chr(10))
        self._serialPort.write("++eot_enable 0"    + chr(10))
        self._serialPort.write("++eot_char 0"      + chr(10))
        self._serialPort.write("++read_tmo_ms 500" + chr(10))
        if findDevices:
            self._devices = []
            for self._hpib_address in range(0, 31):
                self._commandStr = "++addr " + str(self._hpib_address) + chr(10) + \
                                   "*idn?" + chr(10) + \
                                   "++read" + chr(10)
                self._serialPort.write(self._commandStr)
                self._returnStr = self._serialPort.readline()
                if self._returnStr != "":
                    self._devices += [self._hpib_address, self._returnStr]
        else:
            self._devices = "Not scanned."
        self._hpib_address = -1
73

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
74
75
76
77
78
79
80
81
    def command(self, hpib_address, commandStr):
        if hpib_address != self._hpib_address:
            self._commandStr = "++addr " + str(hpib_address)+chr(10)
            self._hpib_address = hpib_address
        else:
            self._commandStr = ""
        self._commandStr += commandStr+chr(10)
        self._commandCount = self._serialPort.write(self._commandStr)
82

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
83
84
85
86
87
88
89
90
91
92
    def query(self, hpib_address, queryStr, count=80):
        if hpib_address != self._hpib_address:
            self._commandStr = "++addr " + str(hpib_address)+chr(10)
            self._hpib_address = hpib_address
        else:
            self._commandStr = ""
        self._commandStr += queryStr + chr(10) + "++read eoi" + chr(10)
        self._commandCount = self._serialPort.write(self._commandStr)
        self._returnStr = self._serialPort.readline()
        return self._returnStr[:-1]
93

94
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
95
96
97
        #self._commandStr = "++addr "+str(hpib_address)+chr(10)+"loc "+str(hpib_address)+chr(10)
        #self._commandCount = self._serialPort.write(self._commandStr)
        return
98

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
99
100
    def getDevices(self):
        return self._devices
101
102


103
class GPIB_232_485CT_A(PortDevice):
104
105
106
107
    def __init__(self, port="/dev/ttyS0", baudrate=9600, bytesize=serial.EIGHTBITS,
                 parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, interCharTimeout=None,
                 pause=0, timeout=10):
        self._serialPort = serial.Serial(port, baudrate, bytesize, parity, stopbits,
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
108
109
110
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
111

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
112
113
114
    def command(self, hpib_address, commandStr):
        self._commandStr = "wr "+str(hpib_address)+chr(10)+commandStr+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
115

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
116
117
118
119
120
121
122
123
124
125
    def query(self, hpib_address, queryStr, count=80):
        # flushInput should not be necessary
        # self._serialPort.flushInput()
        self._commandStr = "wr "+str(hpib_address)+chr(10)+queryStr+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
        self._queryStr = "rd #"+str(count)+" "+str(hpib_address)+chr(13)
        self._queryCount = self._serialPort.write(self._queryStr)
        self._returnStr = self._serialPort.read(count)
        self._returnCnt = int(self._serialPort.readline())
        return self._returnStr[:self._returnCnt-1]
126

127
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
128
129
        self._commandStr = "loc "+str(hpib_address)+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
130
131


auke.klazema's avatar
auke.klazema committed
132
class HpibDevice():
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
133
134
135
136
137
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
138

auke.klazema's avatar
auke.klazema committed
139
    def keepRemote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
140
        self._keepLocal = not keepRemote
141

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
142
143
    def setLocal(self, set2Local=True):
        if set2Local:
144
            self._port.set_local(self._hpib_address)
145

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
146
147
148
149
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
        self.setLocal(self._keepLocal)
        time.sleep(self._pause)
150

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
151
152
153
154
155
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
        self.setLocal(self._keepLocal)
        return self._answerStr

156
157
    def getDevices(self):
        return ["Not implemented"]