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

        if not self._rematch:
            raise ValueError(port)

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
30
31
32
33
34
35
36
37
38
39
40
        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)
41
        if self._serialPort==None:
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
            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
        
class GPIB_232_485CT_A():
    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, \
                                         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)
        
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
    def keepRemote(this, keepRemote=False):
        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"]