forked from hadmack/pyusbtmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyusbtmc.py
executable file
·273 lines (228 loc) · 9.32 KB
/
pyusbtmc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python
#
# PyUSBtmc
#
# Copyright (c) 2011 Mike Hadmack
# Copyright (c) 2010 Matt Mets
# This code is distributed under the MIT license
import os
import sys
import numpy
import time
class PyUsbTmcError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class usbtmc:
"""Simple implementation of a USBTMC device interface using the
linux kernel usbtmc character device driver"""
def __init__(self, device):
self.device = device
try:
# Get a handle to the IO device
self.FILE = os.open(device, os.O_RDWR)
except OSError as e:
raise PyUsbTmcError("Error opening device: " + str(e))
# print >> sys.stderr, "Error opening device: ", e
# raise e
# TODO: This should throw a more descriptive exception to caller
def write(self, command):
"""Write command directly to the device"""
try:
os.write(self.FILE, command);
except OSError as e:
print >> sys.stderr, "Write Error: ", e
def read(self, length=4000):
"""Read an arbitrary amount of data directly from the device"""
try:
return os.read(self.FILE, length)
except OSError as e:
if e.args[0] == 110:
print >> sys.stderr, "Read Error: Read timeout"
else:
print >> sys.stderr, "Read Error: ", e
return ""
def query(self, command, length=300):
"""Write command then read the response and return"""
self.write(command)
return self.read(length)
def getName(self):
return self.query("*IDN?")
def sendReset(self):
self.write("*RST")
def close(self):
"""Close interface to instrument and release file descriptor"""
os.close(self.FILE)
RIGOL_WAV_PREAMBLE_LENGTH = 10
class ScopeChannel:
'''Data and state container for a scope channel
scope is a RigolScope instance'''
def __init__(self, scope, channel_num):
self.scope = scope
self.chan = channel_num
def grabChannelData(self):
'''Grab new data from the scope'''
self.data = self.scope.readData(self.chan)
self.voltscale = self.scope.getVoltScale(self.chan)
self.voltoffset = self.scope.getVoltOffset(self.chan)
def getScaledWaveform(self):
"""Returns a numpy array with voltage scaled scope trace from most recent grab"""
# First invert the data (ya rly)
idata = 255 - self.data
# Now, we know from experimentation that the scope display range is actually
# 30-229. So shift by 130 - the voltage offset in counts, then scale to
# get the actual voltage.
return (idata - 130.0 - self.voltoffset/self.voltscale*25) / 25 * self.voltscale
class RigolScope(usbtmc):
"""Class to control a Rigol DS1000 series 2 channel oscilloscope"""
def __init__(self, device):
usbtmc.__init__(self, device)
self.name = self.getName()
print "# Connected to: " + self.name
self.chan1 = ScopeChannel(self, 1)
self.chan2 = ScopeChannel(self, 2)
self.grabbed_channels = 0
self.size = 0
##################################
# Direct Harware Control Methods #
##################################
def stop(self):
"""Stop acquisition"""
self.write(":STOP")
def run(self):
"""Start acquisition"""
self.write(":RUN")
def forceTrigger(self):
"""Force the scope to trigger now
Also returns scope to local control"""
self.write(":KEY:FORC")
def unlock(self):
"""Unlock scope panel keys"""
#self.write(":KEY:LOCK DIS") # another way
self.forceTrigger()
def close(self):
"""Overload usbtmc close for Rigol specific commands"""
self.unlock()
usbtmc.close(self)
def readRawData(self,chan=1):
"""Read raw data from scope channel"""
command = ":WAV:DATA? CHAN" + str(chan)
self.write(command)
return self.read(9000)
def getStatus(self):
"""Get the scope trigger status"""
return self.query(':TRIGGER:STATUS?')
def setWavePointsMode(self, mode):
"""Set the waveform point mode
mode='NORM' -- 600 points from screen
mode='RAW' -- Return full memory in STOP state
mode='MAX' -- NORM in RUN, RAW in STOP
TODO: Get this to work"""
self.write('WAVEFORM:POINTS:MODE ' + mode)
def getWavePointsMode(self):
"""Return the current waveform point mode"""
return self.query('WAVEFORM:POINTS:MODE?')
def getVoltScale(self,chan=1):
return float(self.query(":CHAN"+str(chan)+":SCAL?", 20))
def getVoltOffset(self,chan=1):
return float(self.query(":CHAN"+str(chan)+":OFFS?", 20))
def getTimeScale(self):
return float(self.query(":TIM:SCAL?", 20))
def getTimeOffset(self):
return float(self.query(":TIM:OFFS?", 20))
def enableAveraging(self, on=True):
if on:
self.write(":ACQuire:TYPE AVERage")
else:
self.write(":ACQuire:TYPE NORMal")
self.write(command)
def setAverages(self, averages):
if averages in [2,4, 8, 16, 32, 64, 128,256]:
self.write(":ACQuire:AVERages %d"%averages)
#####################
# Private Methods #
#####################
def readData(self,chan=1):
"""Read scope channel and return numpy array"""
rawdata = self.readRawData(chan)
return numpy.frombuffer(rawdata, dtype='B', offset=RIGOL_WAV_PREAMBLE_LENGTH)
def makeTimeAxis(self):
"""Retrieve timescale and offset from the scope and return an array or
time points corresponding to the present scope trace
Units are seconds by default
Returns a numpy array of time points"""
# TODO: How can I store these in sec/div as displayed on scope?
# NOTE: This will not work with more than 600 data points!
self.timescale = self.getTimeScale()
self.timeoffset = self.getTimeOffset()
print "Timescale: " + str(self.timescale)
print "Timeoffset: " + str(self.timeoffset)
# The scope display range is 0-600, with 300 being time zero.
timespan = 6*self.timescale
print "Timespan: " + str(timespan)
self.timeaxis = numpy.linspace(self.timeoffset-timespan,self.timeoffset+timespan, 600)
def checkActiveChannels(self):
'''Probe which scope channels are active'''
# TODO: Implementation
# We probably should just always get both channels?
return 1 | 2
##################
# Public Methods #
##################
def getTimeAxis(self):
return self.timeaxis
def getScaledWaveform(self,chan=1):
"""Returns most recently grabbed data for chan"""
if chan == 1:
return self.chan1.getScaledWaveform()
elif chan == 2:
return self.chan2.getScaledWaveform()
elif chan == 1 & 2:
return self.chan1.getScaledWaveform(), self.chan2.getScaledWaveform()
def writeWaveformToFile(self, filename, header=''):
"""Write the most recently acquired data to file"""
if filename == "": fo = sys.stdout # use stdout if no filename
else: fo = open(filename, 'w')
self.writeWaveform(fo, header)
fo.close()
def writeWaveform(self, fo, header=''):
"""Write the most recently acquired data to a file object"""
data1 = numpy.zeros(self.size)
data2 = numpy.zeros(self.size)
if self.grabbed_channels | 1:
data1 = self.chan1.getScaledWaveform()
if self.grabbed_channels | 2:
data2 = self.chan2.getScaledWaveform()
fo.write(header)
fo.write("# DeviceId=" + self.name.strip() + '\n')
fo.write("# " + time.ctime(self.timestamp) + '\n')
fo.write("# Chan1: %g V/div, Chan2: %g V/div, Horiz: %g sec/div, Trigger: %g sec\n"%(self.chan1.voltscale, self.chan2.voltscale,self.timescale,self.timeoffset))
fo.write("# Time (sec) \tChannel 1 (V)\tChannel 2 (V)\n")
for i in range(self.size):
# time resolution is 1/600 = 0.0017 => 5 sig figs
# voltage resolution 1/255 = 0.004 => 4 sig figs
fo.write("%1.4e\t%1.3e\t%1.3e\n"%(self.timeaxis[i],data1[i],data2[i]))
def grabData(self):
'''Retrieves and stores voltage and time axes data
for now grabs both channels'''
# TODO: Check which channels are active then latch data for them
active_channels = self.checkActiveChannels()
if active_channels & 1:
self.chan1.grabChannelData()
self.grabbed_channels &= 1
if active_channels & 2:
self.chan2.grabChannelData()
self.grabbed_channels &= 2
self.makeTimeAxis()
self.size = self.timeaxis.size
self.timestamp = time.time()
def main():
'''Module test code'''
print "# RigolScope Test #"
scope = RigolScope("/dev/usbtmc-rigol")
scope.grabData()
scope.writeWaveformToFile("out.dat")
scope.close()
if __name__ == "__main__":
main()