hpib.py 5.83 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
            if platform.system() == "Linux":
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
26
27
28
29
30
31
32
                port = "/dev/ttyUSB0"
                portFormat = "%d"
                maxDeviceNum = 99
            elif platform.system() == "Darwin":
                port = "/dev/ttys000"
                portFormat = "%03d"
                maxDeviceNum = 999
33
34
            else:
                raise NotImplementedError("Windows is not supported")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
35
36
37
38
        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)
39
40
41
42

        if not self._rematch:
            raise ValueError(port)

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

auke.klazema's avatar
auke.klazema committed
76
    def command(self, hpib_address, command):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
77
        if hpib_address != self._hpib_address:
78
            command = "++addr " + str(hpib_address) + chr(10) + command
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
79
            self._hpib_address = hpib_address
80
81

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

auke.klazema's avatar
auke.klazema committed
83
    def query(self, hpib_address, query, count=80):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
84
        if hpib_address != self._hpib_address:
85
            query = "++addr " + str(hpib_address) + chr(10) + query
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
86
            self._hpib_address = hpib_address
87
88
89
90
91

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

93
    def set_local(self, hpib_address):
auke.klazema's avatar
auke.klazema committed
94
95
        # 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
96
        return
97

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
98
99
    def getDevices(self):
        return self._devices
100
101


102
class GPIB_232_485CT_A(PortDevice):
103
104
105
106
    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
107
108
109
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
110

auke.klazema's avatar
auke.klazema committed
111
112
    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
113
        self._commandCount = self._serialPort.write(self._commandStr)
114

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

126
    def set_local(self, hpib_address):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
127
128
        self._commandStr = "loc "+str(hpib_address)+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
129
130


auke.klazema's avatar
auke.klazema committed
131
class HpibDevice():
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
132
133
134
135
136
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
137

auke.klazema's avatar
auke.klazema committed
138
    def keepRemote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
139
        self._keepLocal = not keepRemote
140

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
141
142
    def setLocal(self, set2Local=True):
        if set2Local:
143
            self._port.set_local(self._hpib_address)
144

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
145
146
147
148
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
        self.setLocal(self._keepLocal)
        time.sleep(self._pause)
149

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
150
151
152
153
154
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
        self.setLocal(self._keepLocal)
        return self._answerStr

155
156
    def getDevices(self):
        return ["Not implemented"]