Вот пример для pyvisa, писалось для сбора данных по 2-м каналам с последущей обработкой во всяких матлабах и октавах.
з.ы. Скорее всего, без модификации, это на другом осциллографе работать не будет.
CODE
# -*- coding: utf-8 -*-
"""
Digitize data with scope 54846A/45A/35A (Agilent)
it use 2 channel mode.
requre:
- python xy
module visa
- qt(for example window)
- visa lib National Instruments
"""
# ---------- Config ----------------------
samples = 20 # number sample data
sample_rate = 1e5
points = 1000 # number points in sample -min 16 max 65536
IP_scope = '192.168.180.250'
tout = 40000 #ms - max time one sample
#-----------------------------------------
import time
import visa
import matplotlib.pyplot as plt
import numpy as np
def GetVoltageConversionFactors( scope ):
yInc = float(scope.query(":WAVeform:YINCrement?"))
yOrg = float(scope.query(":WAVeform:YORigin?"))
yRef = float(scope.query(":WAVeform:YREFerence?"))
return yInc, yOrg, yRef
def ConvertWordDataToVolts( yInc, yOrg, yRef, data ):
#wordVolts[i] = ((wordData[i] - yRef) * yInc) + yOrg
rdata = ((np.array(data) - yRef) * yInc) + yOrg
rdata = rdata.tolist()
return rdata
def GetTimeConversionFactors ( scope ):
xInc = float(scope.query(":WAVeform:XINCrement?"))
xOrg = float(scope.query(":WAVeform:XORigin?"))
xRef = float(scope.query(":WAVeform:XREFerence?"))
return xInc, xOrg, xRef
def CreateTimeData( xInc, xOrg, xRef, AcquiredLength):
# for (i = 0; i < AcquiredLength; i++) {
# TimeValues[i] =((i - xRef) * xInc) + xOrg;
td = range(AcquiredLength)
td = ((np.array(td) - xRef) * xInc) + xOrg
td = td.tolist()
return td
rm = visa.ResourceManager()
#hp = rm.open_resource("TCPIP::192.168.180.250::INSTR")
hp = rm.open_resource("TCPIP::{0}::INSTR".format(IP_scope))
print hp.query("*IDN?")
#hp.write("*RST") # setup to default
#hp.write("*RCL 1") # setup1
#hp.write(":ACQ:AMSR CHAN1") # setup 8GS ch1 ch3
# SET CHANNELS
#hp.write(":VIEW CHAN1")
#hp.timeout = 25000
#hp.query(":AUTOSCALE;*OPC?")
#hp.write("*RST") # setup to default
hp.write(":ACQuire:CONFig TwoCHannel") #TwoCHannel FourCHannel
#hp.write(":ACQuire:SRATe 1e5")
hp.write(":ACQuire:SRATe {0}".format(int(sample_rate)))
hp.write(":ACQuire:Complete 100")
#hp.write(":ACQuire:POINts 1000") # 65536
hp.write(":ACQuire:POINts {0}".format(int(points))) # 65536
hp.write(":ACQuire:INTerpolate 0")
# Setup format data
hp.write( ":WAVeform:FORMat WORD" ); #/* Setup transfer format */
hp.write( ":WAVeform:BYTeorder LSBFirst" );#/* Setup transfer of LSB first */
# Test plot data
hp.timeout = tout
hp.write(":DIGitize CHAN1")
hp.query(":ACQuire:COMPlete?")
hp.write(":CHANNEL1:DISPLAY ON")
hp.write( ":WAVeform:SOURce CHANnel1" ); #/* Waveform data source channel 1 */
preamble = hp.query(":WAVeform:PREamble?")
data = hp.query_binary_values(":WAVeform:DATA?", 'h' ) # h = int16
# Convert data to voltage and time
yInc, yOrg, yRef = GetVoltageConversionFactors(hp)
vdata = ConvertWordDataToVolts( yInc, yOrg, yRef, data )
xInc, xOrg, xRef = GetTimeConversionFactors(hp)
acq_len = int(hp.query("ACQuire:POINts?"))
tdata = CreateTimeData(xInc, xOrg, xRef, acq_len)
# Plot data
plt.plot(tdata, vdata, 'r--')
#plt.plot(tdata, data, 'g--')
plt.show()
# Digitize data
data_frame1 =[]; data_frame2 =[]; x = 0;
dt = [0]; # time between samples
print ("Start acquire ")
print(time.ctime())
tstart = time.ctime()
for x in range(samples) :
hp.write(":DIGitize CHAN1,CHAN3")
print(x)
hp.query(":ACQuire:COMPlete?")
ts = time.clock();
hp.write( ":WAVeform:SOURce CHANnel1" )
data1 = hp.query_binary_values(":WAVeform:DATA?", 'h' )
hp.write( ":WAVeform:SOURce CHANnel3" )
data2 = hp.query_binary_values(":WAVeform:DATA?", 'h' )
data_frame1.append(data1)
data_frame2.append(data2)
tp = time.clock()
dt = dt + [(tp - ts)]
#
tstop = time.ctime()
del x
print ("Complete acquire ")
print(tstop)
# Convert data to voltage and time
# Convert data to voltage and time
yInc, yOrg, yRef = GetVoltageConversionFactors(hp)
vdata_frame1 = []; vdata_frame2 = []; x = 0;
for x in range(samples):
vdata_frame1.append(ConvertWordDataToVolts( yInc, yOrg, yRef, data_frame1[x] ))
vdata_frame2.append(ConvertWordDataToVolts( yInc, yOrg, yRef, data_frame2[x] ))
del x
xInc, xOrg, xRef = GetTimeConversionFactors(hp)
acq_len = int(hp.query("ACQuire:POINts?"))
tdata = CreateTimeData(xInc, xOrg, xRef, acq_len)
#---------------------------------
# Export data to csv files
import csv
myfile = open('dg_ch1.csv', 'wb')
wr = csv.writer(myfile)
wr.writerows(vdata_frame1)
myfile.close()
myfile = open('dg_ch3.csv', 'wb')
wr = csv.writer(myfile)
wr.writerows(vdata_frame2)
myfile.close()
myfile = open('dg_time.csv', 'wb')
wr = csv.writer(myfile)
wr.writerow(tdata)
myfile.close()
myfile = open('dg_dt.csv', 'wb')
wr = csv.writer(myfile)
wr.writerow(dt)
myfile.close()
myfile = open('dg_log.txt', 'wb')
myfile.write(preamble)
myfile.write("\nStart time: ")
myfile.write(tstart + "\n")
myfile.write("Stop time: ")
myfile.write(tstop + "\n")
myfile.write("Acquire: ")
myfile.write(str(samples))
myfile.close()
print ("Export files complete.")