-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrigolusb.py
More file actions
178 lines (132 loc) · 5.23 KB
/
rigolusb.py
File metadata and controls
178 lines (132 loc) · 5.23 KB
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
# Copyright (c) 2015, Vinnie M.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import numpy as np
from time import sleep
import threading
# Make sure usbtmc service running. On linux: "sudo modprobe usbtmc".
# On Windows use VISA drivers? Windows untested.
# For linux, may also need /etc/udev/rules.d/usbtmc.rules file containing the following:
# USBTMC instruments
# Rigol DS1052E
# SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="1ab1", ATTRS{idProduct}=="0588", GROUP="usbtmc", MODE="0660"
lock = threading.Lock()
def _send_command(device_file, command, read_bytes=0):
lock.acquire()
try:
sleep(0.020)
os.write(device_file, bytearray(command, 'ascii'))
if read_bytes > 0:
response = os.read(device_file, read_bytes)
return response
except:
raise
finally:
lock.release()
# Open the device file
def open_device_file(device_path):
os_file = os.open(device_path, os.O_RDWR)
return os_file
# Close the device file
def close_device_file(os_file):
os.close(os_file)
# Stop all acquisition
def set_stop(os_file):
_send_command(os_file, ":STOP")
# Switch from remote (rmt) control back to local control
def set_local(os_file):
_send_command(os_file, ":KEY:FORC")
# Get the time-per-division
def get_id(os_file):
return str(_send_command(os_file, "*IDN?", 2000))
# Get the time-per-division
def get_time_per_division(os_file):
return float(_send_command(os_file, ":TIM:SCAL?", 20))
# Get the time-offset
def get_time_offset(os_file):
return float(_send_command(os_file, ":TIM:OFFS?", 20))
# Get state of channel
def get_channel_state(os_file, ch_num):
return int(_send_command(os_file, ":CHAN" + str(ch_num) + ":DISP?", 20))
# Get the channel data
def get_points(os_file, waveform_pnts_mode, ch_num):
_send_command(os_file, ":WAV:POIN:MODE " + waveform_pnts_mode)
raw_data = _send_command(os_file, ":WAV:DATA? CHAN"
+ str(ch_num), 1048586) # 1048576 + 10 header
points = np.frombuffer(raw_data, 'B')
return points[10:]
# Get the channel volts-per-division
def get_volts_div(os_file, ch_num):
return float(_send_command(os_file, ":CHAN" + str(ch_num) + ":SCAL?", 20))
# Get the channel vertical-offset
def get_vertical_offset(os_file, ch_num):
return float(_send_command(os_file, ":CHAN" + str(ch_num) + ":OFFS?", 20))
# Get the channel sample-rate
def get_sample_rate(os_file, ch_num):
return float(_send_command(os_file, ":ACQ:SAMP? CHAN" + str(ch_num), 20))
# Get the channel Vmax
def get_vmax(os_file, ch_num):
return float(_send_command(os_file, ":MEAS:VMAX? CHAN" + str(ch_num), 20))
# Get the channel Vmin
def get_vmin(os_file, ch_num):
return float(_send_command(os_file, ":MEAS:VMIN? CHAN" + str(ch_num), 20))
# Get the channel Vpp
def get_vpp(os_file, ch_num):
return float(_send_command(os_file, ":MEAS:VPP? CHAN" + str(ch_num), 20))
# Get the channel Vamp
def get_vamp(os_file, ch_num):
vamp = float(_send_command(os_file, ":MEAS:VAMP? CHAN" + str(ch_num), 20))
try:
vamp = float(vamp)
if vamp > 1E9: # assumed to be an error
raise ValueError
except ValueError:
vamp = '***'
return vamp
# Get the channel Vrms
def get_vrms(os_file, ch_num):
return float(_send_command(os_file, ":MEAS:VRMS? CHAN" + str(ch_num), 20))
# Get the channel Freq
def get_freq(os_file, ch_num):
freq = _send_command(os_file, ":MEAS:FREQ? CHAN" + str(ch_num), 20)
try:
freq = float(freq)
if freq > 1E9: # assumed to be an error
raise ValueError
except ValueError:
freq = '********'
return freq
# Get the channel Duty Cycle
def get_duty_cycle(os_file, ch_num):
pdut = _send_command(os_file, ":MEAS:PDUT? CHAN" + str(ch_num), 20)
ndut = _send_command(os_file, ":MEAS:NDUT? CHAN" + str(ch_num), 20)
try:
pdut = float(pdut)
ndut = float(ndut)
pdut *= 100
ndut *= 100
pdut = round(pdut, 3)
ndut = round(ndut, 3)
if pdut > 100 or ndut > 100: # assumed to be an error
raise ValueError
except ValueError:
pdut = '***'
ndut = '***'
return str(pdut) + '/' + str(ndut)