hpib.py 6.27 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
class PortDevice(object):
    def command(self, hpib_address, command):
        raise NotImplementedError

auke.klazema's avatar
auke.klazema committed
11
    def query(self, hpib_address, query, count=80):
12
13
14
15
16
17
18
        raise NotImplementedError

    def set_local(self, hpib_address):
        raise NotImplementedError


class PrologixGpibUsb(PortDevice):
19
20
21
    _serialPort = None
    _hpib_address = -1

22
    def __init__(self, port="", timeout=1, portFormat="%d", maxDeviceNum=9, verbose=False,
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
23
24
                 findDevices=False):
        if port == "":
25
26
            maxDeviceNum, port, portFormat = self._set_default_platform_values()

27
28
        self._validate_port(port)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
29
30
31
        if verbose:
            print "Looking for ports such as %s in range %d on %s" % \
                  ( port, maxDeviceNum, platform.system() )
32

33
34
        self._find_available_port(maxDeviceNum, port, portFormat, timeout, verbose)

35
36
        self._initialize_prologix_gpi()

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
37
38
39
        if findDevices:
            self._devices = []
            for self._hpib_address in range(0, 31):
40
                commandStr = "++addr " + str(self._hpib_address) + chr(10) + \
41
42
                             "*idn?" + chr(10) + \
                             "++read" + chr(10)
43
44
45
46
                self._serialPort.write(commandStr)
                returnStr = self._serialPort.readline()
                if returnStr != "":
                    self._devices += [self._hpib_address, returnStr]
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
47
48
49
        else:
            self._devices = "Not scanned."
        self._hpib_address = -1
50

51
52
53
54
55
56
57
58
59
    def _initialize_prologix_gpi(self):
        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))

60
61
62
63
64
65
66
67
68
69
70
71
72
    def _find_available_port(self, maxDeviceNum, port, portFormat, timeout, verbose):
        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)
        if self._serialPort is None:
            raise serial.SerialException("Cannot find port")

73
74
75
76
77
    def _validate_port(self, port):
        self._rematch = re.search(r"(/dev/tty.*[^0-9])([0-9]+$)", port)
        if not self._rematch:
            raise ValueError(port)

78
79
80
81
82
83
84
85
86
87
88
89
90
    def _set_default_platform_values(self):
        if platform.system() == "Linux":
            port = "/dev/ttyUSB0"
            port_format = "%d"
            max_device_num = 99
        elif platform.system() == "Darwin":
            port = "/dev/ttys000"
            port_format = "%03d"
            max_device_num = 999
        else:
            raise NotImplementedError("Windows is not supported")
        return max_device_num, port, port_format

auke.klazema's avatar
auke.klazema committed
91
    def command(self, hpib_address, command):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
92
        if hpib_address != self._hpib_address:
93
            command = "++addr " + str(hpib_address) + chr(10) + command
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
94
            self._hpib_address = hpib_address
95
96

        self._serialPort.write(command + chr(10))
97

auke.klazema's avatar
auke.klazema committed
98
    def query(self, hpib_address, query, count=80):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
99
        if hpib_address != self._hpib_address:
100
            query = "++addr " + str(hpib_address) + chr(10) + query
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
101
            self._hpib_address = hpib_address
102
103
104
105
106

        query += chr(10) + "++read eoi" + chr(10)
        self._serialPort.write(query)
        line = self._serialPort.readline()
        return line[:-1]
107

108
    def set_local(self, hpib_address):
auke.klazema's avatar
auke.klazema committed
109
110
        # self._commandStr = "++addr "+str(hpib_address)+chr(10)+"loc "+str(hpib_address)+chr(10)
        # self._commandCount = self._serialPort.write(self._commandStr)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
111
        return
112

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
113
114
    def getDevices(self):
        return self._devices
115
116


117
class GPIB_232_485CT_A(PortDevice):
118
119
120
121
    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
122
123
124
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
125

auke.klazema's avatar
auke.klazema committed
126
127
    def command(self, hpib_address, command):
        self._commandStr = "wr " + str(hpib_address) + chr(10) + command + chr(13)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
128
        self._commandCount = self._serialPort.write(self._commandStr)
129

auke.klazema's avatar
auke.klazema committed
130
    def query(self, hpib_address, query, count=80):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
131
132
        # flushInput should not be necessary
        # self._serialPort.flushInput()
auke.klazema's avatar
auke.klazema committed
133
        self._commandStr = "wr " + str(hpib_address) + chr(10) + query + chr(13)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
134
135
136
137
138
139
        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]
140

141
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
142
143
        self._commandStr = "loc "+str(hpib_address)+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
144
145


auke.klazema's avatar
auke.klazema committed
146
class HpibDevice():
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
147
148
149
150
151
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
152

auke.klazema's avatar
auke.klazema committed
153
    def keepRemote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
154
        self._keepLocal = not keepRemote
155

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
156
157
    def setLocal(self, set2Local=True):
        if set2Local:
158
            self._port.set_local(self._hpib_address)
159

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
160
161
162
163
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
        self.setLocal(self._keepLocal)
        time.sleep(self._pause)
164

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
165
166
167
168
169
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
        self.setLocal(self._keepLocal)
        return self._answerStr

170
171
    def getDevices(self):
        return ["Not implemented"]