-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstrument.py
119 lines (87 loc) · 3.41 KB
/
instrument.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
import os
import time
import numpy
import usbtmc
#TODO: switch to usbtmc module functions instead of file-based reads
import pytest
debug = False
class TekScope1000:
"""Class to control a Tektronix TDS1000 series oscilloscope"""
def __init__(self, serialno):
#change to self.inst?
self.meas = usbtmc.Instrument(0x0699,0x03ab,serialno)
self.name = self.meas.ask("*IDN?")
print(self.name)
def write(self, command):
"""Send an arbitrary command directly to the scope"""
self.meas.write(command)
def read(self, length = 4000):
"""Read an arbitrary amount of data directly from the scope"""
return self.meas.read(length)
def reset(self):
"""Reset the instrument"""
self.meas.sendReset()
def read_data(self):
""" Function for reading data and parsing binary into numpy array """
self.write("CURV?")
rawdata = self.meas.read_raw(9000)
if(debug): print(rawdata)
# First few bytes are characters to specify
# the length of the transmission. Need to strip these off:
# we'll assume a 5000-byte transmission
# so the string would be "#45000" and we therefor strip 6 bytes.
return numpy.frombuffer(rawdata[6:-1], 'i2')
def get_data(self,source):
"""
Get scaled data from source where source is one of
CH1,CH2,REFA,REFB
"""
self.write("DATA:SOURCE " + source)
data = self.read_data()
# Get the voltage scale
self.write("WFMP:" + source + ":YMULT?")
ymult = float(self.read(20))
# And the voltage offset
self.write("WFMP:" + source + ":YOFF?")
yoff = float(self.read(20))
# And the voltage zero
self.write("WFMP:" + source + ":YZERO?")
yzero = float(self.read(20))
data = ((data - yoff) * ymult) + yzero
return data
def get_xdata(self):
"""Method to get the horizontal data array from a scope"""
# Get the timescale
self.write("HORIZONTAL:MAIN:SCALE?")
timescale = float(self.read(20))
# Get the timescale offset
self.write("HORIZONTAL:MAIN:POSITION?")
timeoffset = float(self.read(20))
# Get the length of the horizontal record
self.write("HORIZONTAL:RECORDLENGTH?")
time_size = int(self.read(30))
if(debug): print("time_array size = ",time_size)
#time = numpy.arange(0,timescale*10,timescale*10/time_size*2)
# not sure what the above was for, but I'm doing it with linspace now:
time = numpy.linspace(0,timescale*10,time_size)
if(debug): print("length of xdata = ", len(time))
# For some reason the output was too short compared to the data buffer
return time
def test_get_data():
assert len(self.get_data("CH1")) == 5006
class RigolDG:
"""Class to control a Rigol DG4102 arb wave generator."""
def __init__(self, device):
self.meas = usbtmc(device)
self.name = self.meas.getName()
print(self.name)
def write(self, command):
"""Send an arbitrary command directly to the scope"""
self.meas.write(command)
time.sleep(0.1)
def read(self, command):
"""Read an arbitrary amount of data directly from the scope"""
return self.meas.read(command)
def reset(self):
"""Reset the instrument"""
self.meas.sendReset()