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
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
        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))
88

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

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

103
104
        with self._lock:
            self._write_serial(command + chr(10))
105

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

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

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

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


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

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

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

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


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

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

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

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

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

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