把以前的VB控仪器(安捷伦,DSO-X 3034A)的版本升级了一下,用Pyvisa改进之。
既然用上了python,那肯定要加上波形分析的功能了。
Pyvisa的安装不必多说。主要是读取波形的方法需要摸索。官方实例中的python感觉有些老旧,而Pyvisa的官方文档又不切合示波器的场景。因此需要探索出自己的方法。
闲话不多说,直接上代码:
import visa
import string
import struct
# rm = visa.ResourceManager()
# print(rm.list_resources())
# print rm
rm = visa.ResourceManager()
# print rm.list_resources()
inst = rm.open_resource('USB0::0x0957::0x17A4::MY54021611::0::INSTR')
# print inst
# inst.write(':AUToscale')
inst.write("DIGitize CHANnel1")
inst.write(":WAVeform:POINts 1000")
inst.write(":WAVeform:FORMat ASCii")
wav_form_dict = {
0 : "BYTE",
1 : "WORD",
4 : "ASCii",
}
acq_type_dict = {
0 : "NORMal",
1 : "PEAK",
2 : "AVERage",
3 : "HRESolution",
}
preamble_string = inst.query(":WAVeform:PREamble?")
# print preamble_string
(
wav_form, acq_type, wfmpts, avgcnt, x_increment, x_origin,
x_reference, y_increment, y_origin, y_reference
) = string.split(preamble_string, ",")
print "Waveform format: %s" % wav_form_dict[int(wav_form)]
print "Acquire type: %s" % acq_type_dict[int(acq_type)]
print "Waveform points desired: %s" % wfmpts
print "Waveform average count: %s" % avgcnt
print "Waveform X increment: %s" % x_increment
print "Waveform X origin: %s" % x_origin
print "Waveform X reference: %s" % x_reference # Always 0.
print "Waveform Y increment: %s" % y_increment
print "Waveform Y origin: %s" % y_origin
print "Waveform Y reference: %s" % y_reference
inst.write(":WAVeform:DATA?")
data = inst.read_raw()
print '---------'
# print str(data)
data1 = data.split(",")
print data1
print len(data1)
输出的结果是形如['#800013887-2.00825e+000', '-1.84745e+000', '-2.04845e+000', '-1.88765e+000', '-1.88765e+000', '-2.08865e+000', '-1.80725e+000', '-2.00825e+000', '-1.92785e+000', '-1.88765e+000', '-2.08865e+000', '-1.80725e+000', '-2.04845e+000', '-2.00825e+000', '-1.80725e+000', '-2.08865e+000', '-2.00825e+000', '-1.76705e+000', '-1.84745e+000', '-2.00825e+000', '-1.84745e+000', '-1.68664e+000', '-1.64644e+000', '-1.76705e+000', '-1.84745e+000', '-1.80725e+000', '-1.60624e+000', '-1.48564e+000', '-1.36504e+000', '-1.48564e+000', '-1.56604e+000', '-1.60624e+000', '-1.60624e+000', '-1.52584e+000', '-1.36504e+000', '-1.这种类型的数值,这时使用字符串操作,再进行转化成适合FFT的形式就行了。
至于采样周期,可以从 Waveform X increment: +5.04000000E-005 得出。这里代表5ms。
同时还发现,如果将
inst.write(":WAVeform:POINts 1000")
中的1000改为2000,即取2000个点,那么这个周期也将缩小一半。符合规律。
在取得了数值后,又可以得出周期,那么可以套用网上的Python版本FFT程序进行画图了。