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


Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
7
class PROLOGIX_GPIB_USB():
8
    def __init__(self, port="", timeout=1, portFormat="%d", maxDeviceNum=9, verbose=False,
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
9
10
                 findDevices=False):
        if port == "":
11
            if platform.system() == "Linux":
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
12
13
14
15
16
17
18
                port = "/dev/ttyUSB0"
                portFormat = "%d"
                maxDeviceNum = 99
            elif platform.system() == "Darwin":
                port = "/dev/ttys000"
                portFormat = "%03d"
                maxDeviceNum = 999
19
20
            else:
                raise NotImplementedError("Windows is not supported")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
21
22
23
24
        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)
25
26
27
28

        if not self._rematch:
            raise ValueError(port)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
29
30
31
32
33
34
35
36
37
38
39
        self._serialPort = None
        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)
40
        if self._serialPort==None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
            raise serial.SerialException("Cannot find port")
        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))
        if findDevices:
            self._devices = []
            for self._hpib_address in range(0, 31):
                self._commandStr = "++addr " + str(self._hpib_address) + chr(10) + \
                                   "*idn?" + chr(10) + \
                                   "++read" + chr(10)
                self._serialPort.write(self._commandStr)
                self._returnStr = self._serialPort.readline()
                if self._returnStr != "":
                    self._devices += [self._hpib_address, self._returnStr]
        else:
            self._devices = "Not scanned."
        self._hpib_address = -1
    def command(self, hpib_address, commandStr):
        if hpib_address != self._hpib_address:
            self._commandStr = "++addr " + str(hpib_address)+chr(10)
            self._hpib_address = hpib_address
        else:
            self._commandStr = ""
        self._commandStr += commandStr+chr(10)
        self._commandCount = self._serialPort.write(self._commandStr)
    def query(self, hpib_address, queryStr, count=80):
        if hpib_address != self._hpib_address:
            self._commandStr = "++addr " + str(hpib_address)+chr(10)
            self._hpib_address = hpib_address
        else:
            self._commandStr = ""
        self._commandStr += queryStr + chr(10) + "++read eoi" + chr(10)
        self._commandCount = self._serialPort.write(self._commandStr)
        self._returnStr = self._serialPort.readline()
        return self._returnStr[:-1]
    def setLocal(self, hpib_address):
        #self._commandStr = "++addr "+str(hpib_address)+chr(10)+"loc "+str(hpib_address)+chr(10)
        #self._commandCount = self._serialPort.write(self._commandStr)
        return
    def getDevices(self):
        return self._devices
86
87


Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
88
class GPIB_232_485CT_A():
89
90
91
92
    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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                                         interCharTimeout, timeout)
        self._serialPort.flushInput()
        self._pause = pause
    def command(self, hpib_address, commandStr):
        self._commandStr = "wr "+str(hpib_address)+chr(10)+commandStr+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
    def query(self, hpib_address, queryStr, count=80):
        # flushInput should not be necessary
        # self._serialPort.flushInput()
        self._commandStr = "wr "+str(hpib_address)+chr(10)+queryStr+chr(13)
        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]
    def setLocal(self, hpib_address):
        self._commandStr = "loc "+str(hpib_address)+chr(13)
        self._commandCount = self._serialPort.write(self._commandStr)
112
113


Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
114
115
116
117
118
119
class hpib_device():
    def __init__(self, hpib_address, port, keepLocal=False,pause=0):
        self._hpib_address = hpib_address
        self._port = port
        self._keepLocal = keepLocal
        self._pause = pause
auke.klazema's avatar
auke.klazema committed
120
    def keepRemote(self, keepRemote=False):
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        self._keepLocal = not keepRemote
    def setLocal(self, set2Local=True):
        if set2Local:
            self._port.setLocal(self._hpib_address)
    def command(self, commandStr):
        self._port.command(self._hpib_address, commandStr)
        self.setLocal(self._keepLocal)
        time.sleep(self._pause)
    def query(self, queryStr):
        self._answerStr = self._port.query(self._hpib_address, queryStr)
        self.setLocal(self._keepLocal)
        return self._answerStr
    def getDevices(self):
        return ["Not implemented"]