hpib.py 5.97 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()

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
27
28
29
30
        if verbose:
            print "Looking for ports such as %s in range %d on %s" % \
                  ( port, maxDeviceNum, platform.system() )
        self._rematch=re.search(r"(/dev/tty.*[^0-9])([0-9]+$)",port)
31
32
33
34

        if not self._rematch:
            raise ValueError(port)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
35
36
37
38
39
40
41
42
43
44
        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)
45
        if self._serialPort==None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
46
            raise serial.SerialException("Cannot find port")
auke.klazema's avatar
auke.klazema committed
47
48
49
50
51
52
        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))
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
53
54
55
56
        self._serialPort.write("++read_tmo_ms 500" + chr(10))
        if findDevices:
            self._devices = []
            for self._hpib_address in range(0, 31):
57
                commandStr = "++addr " + str(self._hpib_address) + chr(10) + \
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
58
59
                                   "*idn?" + chr(10) + \
                                   "++read" + chr(10)
60
61
62
63
                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
64
65
66
        else:
            self._devices = "Not scanned."
        self._hpib_address = -1
67

68
69
70
71
72
73
74
75
76
77
78
79
80
    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
81
    def command(self, hpib_address, command):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
82
        if hpib_address != self._hpib_address:
83
            command = "++addr " + str(hpib_address) + chr(10) + command
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
84
            self._hpib_address = hpib_address
85
86

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

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

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

98
    def set_local(self, hpib_address):
auke.klazema's avatar
auke.klazema committed
99
100
        # 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
101
        return
102

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
103
104
    def getDevices(self):
        return self._devices
105
106


107
class GPIB_232_485CT_A(PortDevice):
108
109
110
111
    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
112
113
114
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
115

auke.klazema's avatar
auke.klazema committed
116
117
    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
118
        self._commandCount = self._serialPort.write(self._commandStr)
119

auke.klazema's avatar
auke.klazema committed
120
    def query(self, hpib_address, query, count=80):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
121
122
        # flushInput should not be necessary
        # self._serialPort.flushInput()
auke.klazema's avatar
auke.klazema committed
123
        self._commandStr = "wr " + str(hpib_address) + chr(10) + query + chr(13)
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
124
125
126
127
128
129
        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]
130

131
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
132
133
        self._commandStr = "loc "+str(hpib_address)+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
134
135


auke.klazema's avatar
auke.klazema committed
136
class HpibDevice():
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
137
138
139
140
141
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
142

auke.klazema's avatar
auke.klazema committed
143
    def keepRemote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
144
        self._keepLocal = not keepRemote
145

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
146
147
    def setLocal(self, set2Local=True):
        if set2Local:
148
            self._port.set_local(self._hpib_address)
149

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
150
151
152
153
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
        self.setLocal(self._keepLocal)
        time.sleep(self._pause)
154

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
155
156
157
158
159
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
        self.setLocal(self._keepLocal)
        return self._answerStr

160
161
    def getDevices(self):
        return ["Not implemented"]