diff options
Diffstat (limited to 'scope.py')
-rw-r--r-- | scope.py | 72 |
1 files changed, 47 insertions, 25 deletions
@@ -16,9 +16,15 @@ class Scope(scpi.SCPIinstr): """ def __init__(self, resource): super().__init__(resource) + +class Trace: + x = None + y = None class SDS1104x(Scope): """ Siglent SDS1104x scope """ + vertDivOnScreen = 10 + horizDivOnScreen = 14 def __init__(self, resource): super().__init__(resource) self.resource.read_termination='\n' @@ -62,20 +68,29 @@ class SDS1104x(Scope): prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='Sa/s') return(int(float(numberString))) - def getRawWaveform(self, chNum, availableNpnts=None, maxRequiredPoints=None): + def calcSparcingAndNumPoints(self, availableNpnts=None, maxRequiredPoints=None): if availableNpnts == None: - availableNpnts = self.getAvailableNumberOfPoints(chNum) + # using channel 1 to get availableNpnts + availableNpnts = self.getAvailableNumberOfPoints(1) if maxRequiredPoints == None: maxRequiredPoints = self.maxRequiredPoints - + if availableNpnts <= maxRequiredPoints*2: Npnts = availableNpnts sparsing = 1 - cstr = f'WAVEFORM_SETUP NP,0,FP,0,SP,{sparsing}' - self.write(cstr) else: sparsing = int(np.floor(availableNpnts/maxRequiredPoints)) Npnts = int(np.floor(availableNpnts/sparsing)) + return(sparsing, Npnts, availableNpnts, maxRequiredPoints) + + def getRawWaveform(self, chNum, availableNpnts=None, maxRequiredPoints=None): + (sparsing, Npnts, availableNpnts, maxRequiredPoints) = self.calcSparcingAndNumPoints(availableNpnts, maxRequiredPoints) + if sparsing == 1 and Npnts == availableNpnts: + # we are getting all of it + cstr = f'WAVEFORM_SETUP NP,0,FP,0,SP,{sparsing}' + # technically when we know Npnts and sparsing + # we can use command from the follow up 'else' clause + else: cstr = f'WAVEFORM_SETUP SP,{sparsing},NP,{Npnts},FP,0' # Note: it is not enough to provide sparsing (SP), # number of points (NP) needed to be calculated properly too! @@ -94,11 +109,12 @@ class SDS1104x(Scope): # For example: # FP = 0 corresponds to the first data point. # FP = 1 corresponds to the second data point + self.write(cstr) qstr = f'C{chNum}:WAVEFORM? DAT2' - wfRaw=self.query_binary_values(qstr, datatype='b', header_fmt='ieee', container=np.array) + wfRaw=self.query_binary_values(qstr, datatype='b', header_fmt='ieee', container=np.array, chunk_size=(Npnts+100)) # expected full reply: 'C1:WF DAT2,#9000000140.........' - return(wfRaw, availableNpnts, sparsing) + return(wfRaw, availableNpnts, Npnts, sparsing) def getChanVoltsPerDiv(self, chNum): qstr = f'C{chNum}:VDIV?' @@ -130,24 +146,31 @@ class SDS1104x(Scope): def getWaveform(self, chNum, availableNpnts=None, maxRequiredPoints=None): - wfRaw, availableNpnts, sparsing = self.getRawWaveform(chNum, availableNpnts=availableNpnts, maxRequiredPoints=maxRequiredPoints) - vertDivOnScreen = 10 + wfRaw, availableNpnts, Npnts, sparsing = self.getRawWaveform(chNum, availableNpnts=availableNpnts, maxRequiredPoints=maxRequiredPoints) VoltageOffset = self.getChanOffset(chNum) VoltsPerDiv = self.getChanVoltsPerDiv(chNum) - return( wfRaw * VoltsPerDiv * vertDivOnScreen/250 -VoltageOffset, availableNpnts, sparsing) + return( wfRaw * VoltsPerDiv * self.vertDivOnScreen/250 -VoltageOffset, availableNpnts, sparsing) - def getTimeTrace(self, sparsing=1, Npnts=None): + def getTimeTrace(self, availableNpnts=None, maxRequiredPoints=None): + (sparsing, Npnts, availableNpnts, maxRequiredPoints) = self.calcSparcingAndNumPoints(availableNpnts, maxRequiredPoints) sampleRate = self.getSampleRate() timePerDiv = self.getTimePerDiv() trigDelay = self.getTrigDelay() - if Npnts == None: + if Npnts == None and sparsing == None: # using channel 1 as reference Npnts = self.getAvailableNumberOfPoints(1) - time = np.arange(Npnts) / sampleRate * sparsing; - horizDivOnScreen = 14 - time = time - timePerDiv * horizDivOnScreen/2 - trigDelay - return(time) + t = np.arange(Npnts) / sampleRate * sparsing; + t = t - timePerDiv * self.horizDivOnScreen/2 - trigDelay + return(t) + def getTrace(self, chNum, availableNpnts=None, maxRequiredPoints=None): + wfVoltage, availableNpnts, sparsing = self.getWaveform( chNum, availableNpnts=availableNpnts, maxRequiredPoints=maxRequiredPoints) + t = self.getTimeTrace(availableNpnts=availableNpnts, maxRequiredPoints=maxRequiredPoints) + tr = Trace() + tr.x = t + tr.y = wfVoltage + return( tr ) + if __name__ == '__main__': print("testing") @@ -155,15 +178,14 @@ if __name__ == '__main__': print(rm.list_resources()) instr=rm.open_resource('TCPIP::192.168.0.61::INSTR') scope = SDS1104x(instr) - print(scope.idn) - print(scope.mean(1)) - print(scope.getAvailableNumberOfPoints(1)) - print(scope.getSampleRate()) - print(scope.getTimePerDiv()) - print(scope.getChanVoltsPerDiv(1)) - print(scope.getChanOffset(1)) - wf = scope.getWaveform(4) - time = scope.getTimeTrace() + print(f'ID: {scope.idn}') + print(f'Ch1 mean: {scope.mean(1)}') + print(f'Ch1 available points: {scope.getAvailableNumberOfPoints(1)}') + print(f'Sample Rate: {scope.getSampleRate()}') + print(f'Time per Div: {scope.getTimePerDiv()}') + print(f'Ch1 Volts per Div: {scope.getChanVoltsPerDiv(1)}') + print(f'Ch1 Voltage Offset: {scope.getChanOffset(1)}') + ch1 = scope.getTrace(1) |