summaryrefslogtreecommitdiffstats
path: root/lab_control/sds1000xe.py
blob: a3ffe9c72fa74d41102e49e2a8edcb673929f240 (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
import socket
import re
from lab_control.oscilloscope import Oscilloscope

class SDS1000XE(Oscilloscope):
    PORT = 5025
    TIMEOUT = 0.2
    AVAILABLE_CHANNELS = range(1, 5)

    def __init__(self, address):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.connect((address, SDS1000XE.PORT))
        self._socket.settimeout(SDS1000XE.TIMEOUT)

    def measureAmplitude(self, channel: int) -> float:
        return self._measure(channel, "AMPL")

    def measurePeakToPeak(self, channel: int) -> float:
        return self._measure(channel, "PKPK")

    def measureRMS(self, channel: int) -> float:
        return self._measure(channel, "RMS")

    def measureFrequency(self, channel: int) -> float:
        return self._measure(channel, "FREQ")

    def _measure(self, channel: int, code: str) -> float:
        assert channel in SDS1000XE.AVAILABLE_CHANNELS

        query = f"C{channel}:PAVA? {code}\r\n"
        self._socket.sendall(query.encode())

        try:
            # TODO add code to regex
            response = self._socket.recv(4096).decode()
            m = re.search(r"C(?P<responseChannel>\d):PAVA .+,(?P<rawMeasurement>.+)V", response)
            measurement = float(m.group("rawMeasurement"))
        except TimeoutError as e:
            measurement = None
        
        return measurement