hpib.py 6.37 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
import sys
6
from threading import Lock
7
8


9
10
11
12
class PortDevice(object):
    def command(self, hpib_address, command):
        raise NotImplementedError

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

    def set_local(self, hpib_address):
        raise NotImplementedError


class PrologixGpibUsb(PortDevice):
21
    _serialPort = None
22
    _devices = "Not scanned."
23

auke.klazema's avatar
auke.klazema committed
24
25
    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
26
        if port == "":
auke.klazema's avatar
auke.klazema committed
27
            max_device_num, port, port_format = self._set_default_platform_values()
28

29
        self._validate_port(port)
30
        self._lock = Lock()
31

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

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

38
39
        self._initialize_prologix_gpi()

auke.klazema's avatar
auke.klazema committed
40
        if find_devices:
41
            self._find_devices()
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

auke.klazema's avatar
auke.klazema committed
61
62
    def _find_available_port(self, max_device_num, port, port_format, timeout, verbose):
        while int(self._rematch.group(2)) < max_device_num:
63
            if verbose:
64
                print("trying ", port)
65
66
67
68
            try:
                self._serialPort = serial.Serial(port, timeout=timeout)
                break
            except serial.SerialException:
auke.klazema's avatar
auke.klazema committed
69
                port = self._rematch.group(1) + (port_format % (int(self._rematch.group(2)) + 1))
70
71
72
73
                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
    def _write_serial(self, commandString):
        if sys.version_info >= (3,0):
            self._serialPort.write(bytes(commandString, 'utf-8'))
        else:
            self._serialPort.write(commandString)

80
    def _initialize_prologix_gpi(self):
81
82
83
84
85
86
87
88
        with self._lock:
            self._write_serial("++savecfg 0" + chr(10))
            self._write_serial("++auto 0" + chr(10))
            self._write_serial("++eoi 1" + chr(10))
            self._write_serial("++eos 2" + chr(10))
            self._write_serial("++eot_enable 0" + chr(10))
            self._write_serial("++eot_char 0" + chr(10))
            self._write_serial("++read_tmo_ms 500" + chr(10))
89

90
91
    def _find_devices(self):
        self._devices = []
92
93
        for address in range(0, 31):
            commandStr = "++addr " + str(address) + chr(10) + \
94
95
                         "*idn?" + chr(10) + \
                         "++read" + chr(10)
96
97
98
            with self._lock:
                self._write_serial(commandStr)
                returnStr = self._serialPort.readline()
99
            if returnStr != "":
100
                self._devices += [address, returnStr]
101

auke.klazema's avatar
auke.klazema committed
102
    def command(self, hpib_address, command):
103
        command = "++addr " + str(hpib_address) + chr(10) + command
104

105
106
        with self._lock:
            self._write_serial(command + chr(10))
107

auke.klazema's avatar
auke.klazema committed
108
    def query(self, hpib_address, query, count=80):
109
        query = "++addr " + str(hpib_address) + chr(10) + query
110
111

        query += chr(10) + "++read eoi" + chr(10)
112
113
114
        with self._lock:
            self._write_serial(query)
            line = self._serialPort.readline()
115
        return line[:-1]
116

117
    def set_local(self, hpib_address):
auke.klazema's avatar
auke.klazema committed
118
        # self._commandStr = "++addr "+str(hpib_address)+chr(10)+"loc "+str(hpib_address)+chr(10)
119
        # self._commandCount = self._write_serial(self._commandStr)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
120
        return
121

auke.klazema's avatar
auke.klazema committed
122
    def get_devices(self):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
123
        return self._devices
124
125


126
class GPIB_232_485CT_A(PortDevice):
127
128
129
130
    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
131
132
133
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
134

auke.klazema's avatar
auke.klazema committed
135
136
    def command(self, hpib_address, command):
        self._commandStr = "wr " + str(hpib_address) + chr(10) + command + chr(13)
137
        self._commandCount = self._write_serial(self._commandStr)
138

auke.klazema's avatar
auke.klazema committed
139
    def query(self, hpib_address, query, count=80):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
140
141
        # flushInput should not be necessary
        # self._serialPort.flushInput()
auke.klazema's avatar
auke.klazema committed
142
        self._commandStr = "wr " + str(hpib_address) + chr(10) + query + chr(13)
143
        self._commandCount = self._write_serial(self._commandStr)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
144
        self._queryStr = "rd #"+str(count)+" "+str(hpib_address)+chr(13)
145
        self._queryCount = self._write_serial(self._queryStr)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
146
147
148
        self._returnStr = self._serialPort.read(count)
        self._returnCnt = int(self._serialPort.readline())
        return self._returnStr[:self._returnCnt-1]
149

150
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
151
        self._commandStr = "loc "+str(hpib_address)+chr(13)
152
        self._commandCount = self._write_serial(self._commandStr)
153
154


155
class HpibDevice(object):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
156
157
158
159
160
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
161

auke.klazema's avatar
auke.klazema committed
162
    def keep_remote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
163
        self._keepLocal = not keepRemote
164

auke.klazema's avatar
auke.klazema committed
165
    def set_local(self, set2Local=True):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
166
        if set2Local:
167
            self._port.set_local(self._hpib_address)
168

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
169
170
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
auke.klazema's avatar
auke.klazema committed
171
        self.set_local(self._keepLocal)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
172
        time.sleep(self._pause)
173

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
174
    def query(self, queryStr):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
175
        answerStr = self._port.query(self._hpib_address, queryStr)
auke.klazema's avatar
auke.klazema committed
176
        self.set_local(self._keepLocal)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
177
        return answerStr
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
178

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