aboutsummaryrefslogtreecommitdiff
path: root/scope.py
blob: 5fc2a03b381e2102c1e2f25244f6c881b5d50792 (plain)
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
"""
Provide basic class to operate scope
Created by Eugeniy E. Mikhailov 2021/11/29
"""

import pyvisa
import scpi
import re
import numpy as np

class Scope(scpi.SCPIinstr):
    """     
    Do not instantiate directly, use
    rm = pyvisa.ResourceManager()
    Scope(rm.open_resource('TCPIP::192.168.0.2::INSTR'))
    """
    def __init__(self, resource):
        super().__init__(resource)
        
class SDS1104x(Scope):
    """ Siglent SDS1104x scope """
    def __init__(self, resource):
        super().__init__(resource)
        self.resource.read_termination='\n'
        self.maxRequiredPoints = 1000; # desired number of points per channel, can return twice more

    def response2numStr(self, strIn, firstSeparator=None, unit=None):
        # A typical reply of Siglent is in the form 'TDIV 2.00E-08S'
        # i.e. "<prefix><firstSeparator><numberString><unit>
        # prefix='TDIV', firstSeparator=' ', numberString='2.00E-08', unit='S'
        # this function parses the reply
        spltStr = re.split(firstSeparator, strIn)
        prefix = spltStr[0]
        rstr = spltStr[1]
        spltStr = re.split(unit, rstr)
        numberString = spltStr[0]
        unit = spltStr[1]
        return (prefix, numberString, unit)

    def mean(self, chNum):
        # get mean on a specific channel calculated by scope
        # PAVA stands for PArameter VAlue
        qstr = f'C{chNum}:PAVA? MEAN'
        rstr = self.query(qstr); 
        # reply is in the form 'C1:PAVA MEAN,3.00E-02V'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=',', unit='V')
        return(float(numberString))

    def getAvailableNumberOfPoints(self, chNum):
        if chNum != 1 and chNum != 3:
            # for whatever reason 'SAMPLE_NUM' fails for channel 2 and 4
            chNum = 1
        qstr = f'SAMPLE_NUM?  C{chNum}'
        rstr = self.query(qstr)
        # reply is in the form 'SANU 7.00E+01pts'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='pts')
        return(int(float(numberString)))
            
    def getSampleRate(self):
        rstr = self.query('SAMPLE_RATE?');
        # expected reply is like 'SARA 1.00E+09Sa/s'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='Sa/s')
        return(int(float(numberString)))

    def getRawWaveform(self, chNum, availableNpnts=None, maxRequiredPoints=None):
        if availableNpnts == None:
            availableNpnts = self.getAvailableNumberOfPoints(chNum)
        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))
            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!
            # From the manual
            # WAVEFORM_SETUP SP,<sparsing>,NP,<number>,FP,<point> 
            # SP Sparse point. It defines the interval between data points.
            # For example:
            #   SP = 0 sends all data points.
            #   SP = 1 sends all data points.
            #   SP = 4 sends every 4th data point
            # NP — Number of points. It indicates how many points should be transmitted.
            # For example:
            #   NP = 0 sends all data points.
            #   NP = 50 sends a maximum of 50 data points.
            # FP — First point. It specifies the address of the first data point to be sent.
            # For example:
            #   FP = 0 corresponds to the first data point.
            #   FP = 1 corresponds to the second data point

        qstr = f'C{chNum}:WAVEFORM? DAT2'
        wfRaw=self.query_binary_values(qstr, datatype='b', header_fmt='ieee', container=np.array) 
        # expected full reply: 'C1:WF DAT2,#9000000140.........'
        return(wfRaw, availableNpnts, sparsing)

    def getChanVoltsPerDiv(self, chNum):
        qstr = f'C{chNum}:VDIV?'
        rstr = self.query(qstr)
        # expected reply to query: 'C1:VDIV 1.04E+00V'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='V')
        return(float(numberString))

    def getChanOffset(self, chNum):
        qstr = f'C{chNum}:OFST?'
        rstr = self.query(qstr)
        # expected reply to query: 'C1:OFST -1.27E+00V'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='V')
        return(float(numberString))

    def getTimePerDiv(self):
        qstr = f'TDIV?'
        rstr = self.query(qstr)
        # expected reply to query: 'TDIV 2.00E-08S'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='S')
        return(float(numberString))

    def getTrigDelay(self):
        qstr = f'TRIG_DELAY?'
        rstr = self.query(qstr)
        # expected reply to query: 'TRDL -0.00E+00S'
        prefix, numberString, unit = self.response2numStr(rstr, firstSeparator=' ', unit='S')
        return(float(numberString))


    def getWaveform(self, chNum, availableNpnts=None, maxRequiredPoints=None):
        wfRaw, availableNpnts, sparsing = self.getRawWaveform(chNum, availableNpnts=availableNpnts, maxRequiredPoints=maxRequiredPoints)
        vertDivOnScreen = 10
        VoltageOffset = self.getChanOffset(chNum)
        VoltsPerDiv = self.getChanVoltsPerDiv(chNum)
        return( wfRaw * VoltsPerDiv * vertDivOnScreen/250 -VoltageOffset, availableNpnts, sparsing)

    def getTimeTrace(self, sparsing=1, Npnts=None):
        sampleRate = self.getSampleRate()
        timePerDiv = self.getTimePerDiv()
        trigDelay  = self.getTrigDelay()
        if Npnts == 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)
        

if __name__ == '__main__': 
    print("testing")
    rm = pyvisa.ResourceManager()
    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()