-
Notifications
You must be signed in to change notification settings - Fork 5
/
netgpib.py
executable file
·188 lines (159 loc) · 5.71 KB
/
netgpib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import sys
#import math
import socket
import time
#import pdb
import select
import struct
import termstatus
class netGPIB:
def __init__(self, ip, gpibAddr, eot='\004', debug=0, auto=False, log=False, tSleep=0):
#End of Transmission character
self.eot = eot
# EOT character number in the ASCII table
self.eotNum = struct.unpack('B',eot)[0]
#Debug flag
self.debug = debug
#Auto mode
self.auto = auto
#Waiting time
self.tSleep=tSleep
#Log mode
self.log = log
self.ip = ip
self.gpibAddr = gpibAddr
#Connect to the GPIB-Ethernet converter
netAddr = (ip, 1234)
self.netSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.netSock.connect(netAddr)
#Initialize the GPIB-Ethernet converter
self.netSock.setblocking(0)
self.netSock.send("++addr "+str(self.gpibAddr)+"\n")
time.sleep(0.1)
self.netSock.send("++eos 3\n")
time.sleep(0.1)
self.netSock.send("++mode 1\n")
time.sleep(0.1)
if self.auto:
self.netSock.send("++auto 1\n")
else:
self.netSock.send("++auto 0\n")
time.sleep(0.1)
self.netSock.send("++ifc\n")
time.sleep(0.1)
self.netSock.send("++read_tmo_ms 3000\n")
time.sleep(0.1)
self.netSock.send("++eot_char "+str(self.eotNum)+"\n")
self.netSock.send("++eot_enable 1\n")
self.netSock.send("++addr "+str(self.gpibAddr)+"\n")
def refresh(self):
self.netSock.send("++addr "+str(self.gpibAddr)+"\n")
time.sleep(0.1)
self.netSock.send("++eos 3\n")
time.sleep(0.1)
self.netSock.send("++mode 1\n")
time.sleep(0.1)
if self.auto:
self.netSock.send("++auto 1\n")
else:
self.netSock.send("++auto 0\n")
time.sleep(0.1)
self.netSock.send("++ifc\n")
time.sleep(0.1)
self.netSock.send("++read_tmo_ms 3000\n")
time.sleep(0.1)
self.netSock.send("++eot_char"+str(self.eotNum)+"\n")
self.netSock.send("++eot_enable 1\n")
self.netSock.send("++addr "+str(self.gpibAddr)+"\n")
def getData(self, buf, sleep=None):
if sleep is None:
sleep=self.tSleep+0.1
data=""
dlen=0
if self.debug == True:
progressInfo=termstatus.statusTxt("0 bytes received")
while 1: # Repeat reading data until eot is found
while 1: # Read some data
readSock, writeSock, errSock = select.select([self.netSock],[],[],3)
if len(readSock) == 1:
data1 = readSock[0].recv(buf)
if self.debug == True:
dlen=dlen+len(data1)
progressInfo.update(str(dlen)+' bytes received')
break
if data1[-1] == self.eot: #if eot is found at the end
data = data + data1[:-1] #remove eot
break
else:
data = data + data1
time.sleep(sleep)
if self.debug == True:
progressInfo.end()
return data
def query(self,string,buf=100,sleep=None):
"""Send a query to the device and return the result."""
if sleep is None:
sleep=self.tSleep
if self.log:
# TODO: make this print command python3 friendly
print >>sys.stderr, "?? %s" % string
self.netSock.send(string+"\n")
if not self.auto:
time.sleep(sleep)
self.netSock.send("++read eoi\n") #Change to listening mode
ret = self.getData(buf)
if self.log:
# TODO: make this print command python3 friendly
print >>sys.stderr, "== %s" % ret.strip()
return ret
def srq(self):
"""Poll the device's SRQ"""
self.netSock.send("++srq\n")
while 1: # Read some data
readSock, writeSock, errSock = select.select([self.netSock],[],[],3)
if len(readSock) == 1:
data = readSock[0].recv(100)
break
return data[:-2]
def command(self,string,sleep=None):
"""Send a command to the device."""
if sleep is None:
sleep=self.tSleep
if self.log:
# TODO: make this print command python3 friendly
print >>sys.stderr, ">> %s" % string
self.netSock.send(string+"\n")
time.sleep(sleep)
def spoll(self):
"""Perform a serial polling and return the result."""
self.netSock.send("++spoll\n")
while 1: # Read some data
readSock, writeSock, errSock = select.select([self.netSock],[],[],3)
if len(readSock) == 1:
data = readSock[0].recv(100)
break
return data[:-2]
def close(self):
self.netSock.close()
def setDebugMode(self, debugFlag):
if debugFlag:
self.debug=1
else:
self.debug=0
def gpibGetData(netSock, buf, eot, debug=0):
data=""
while 1: # Repeat reading data until eot is found
while 1: # Read some data
readSock, writeSock, errSock = select.select([netSock],[],[],3)
if len(readSock) == 1:
data1 = readSock[0].recv(buf)
if debug == True:
print(str(len(data1))+' bytes received')
break
if data1[len(data1)-1] == eot: #if eot is found at the end
data = data + data1[0:len(data1)-1] #remove eot
break
else:
data = data + data1
time.sleep(1)
return data