hpib.py 6.3 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
        if findDevices:
38
            self._find_devices()
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
39
40
41
        else:
            self._devices = "Not scanned."
        self._hpib_address = -1
42

43
44
45
46
47
48
49
50
51
52
53
54
    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
55

56
57
58
59
    def _validate_port(self, port):
        self._rematch = re.search(r"(/dev/tty.*[^0-9])([0-9]+$)", port)
        if not self._rematch:
            raise ValueError(port)
60

61
62
63
64
65
66
67
68
69
70
71
72
73
    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")

74
75
76
77
78
79
80
81
    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))
82

83
84
85
86
87
88
89
90
91
92
    def _find_devices(self):
        self._devices = []
        for self._hpib_address in range(0, 31):
            commandStr = "++addr " + str(self._hpib_address) + chr(10) + \
                         "*idn?" + chr(10) + \
                         "++read" + chr(10)
            self._serialPort.write(commandStr)
            returnStr = self._serialPort.readline()
            if returnStr != "":
                self._devices += [self._hpib_address, returnStr]
93

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

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

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

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

111
    def set_local(self, hpib_address):
auke.klazema's avatar
auke.klazema committed
112
113
        # 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
114
        return
115

auke.klazema's avatar
auke.klazema committed
116
    def get_devices(self):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
117
        return self._devices
118
119


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

auke.klazema's avatar
auke.klazema committed
129
130
    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
131
        self._commandCount = self._serialPort.write(self._commandStr)
132

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

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


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

auke.klazema's avatar
auke.klazema committed
156
    def keep_remote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
157
        self._keepLocal = not keepRemote
158

auke.klazema's avatar
auke.klazema committed
159
    def set_local(self, set2Local=True):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
160
        if set2Local:
161
            self._port.set_local(self._hpib_address)
162

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
163
164
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
auke.klazema's avatar
auke.klazema committed
165
        self.set_local(self._keepLocal)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
166
        time.sleep(self._pause)
167

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
168
169
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
auke.klazema's avatar
auke.klazema committed
170
        self.set_local(self._keepLocal)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
171
172
        return self._answerStr

auke.klazema's avatar
auke.klazema committed
173
    def get_devices(self):
174
        return ["Not implemented"]