hpib.py 6.23 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
    _serialPort = None
    _hpib_address = -1
21
    _devices = "Not scanned."
22

auke.klazema's avatar
auke.klazema committed
23
24
    def __init__(self, port="", timeout=1, port_format="%d", max_device_num=9, verbose=False,
                 find_devices=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
25
        if port == "":
auke.klazema's avatar
auke.klazema committed
26
            max_device_num, port, port_format = self._set_default_platform_values()
27

28
29
        self._validate_port(port)

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

auke.klazema's avatar
auke.klazema committed
34
        self._find_available_port(max_device_num, port, port_format, timeout, verbose)
35

36
37
        self._initialize_prologix_gpi()

auke.klazema's avatar
auke.klazema committed
38
        if find_devices:
39
            self._find_devices()
40

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

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

auke.klazema's avatar
auke.klazema committed
59
60
    def _find_available_port(self, max_device_num, port, port_format, timeout, verbose):
        while int(self._rematch.group(2)) < max_device_num:
61
            if verbose:
62
                print("trying ", port)
63
64
65
66
            try:
                self._serialPort = serial.Serial(port, timeout=timeout)
                break
            except serial.SerialException:
auke.klazema's avatar
auke.klazema committed
67
                port = self._rematch.group(1) + (port_format % (int(self._rematch.group(2)) + 1))
68
69
70
71
                self._rematch = re.search("(/dev/tty.*[^0-9])([0-9]+$)", port)
        if self._serialPort is None:
            raise serial.SerialException("Cannot find port")

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

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

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

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

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

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

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

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


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

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

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

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


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

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

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

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

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

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