summaryrefslogtreecommitdiffstats
path: root/lab_control/sds1000xe.py
blob: caa6a9ca14733e1251a2b5c213126667e61e41e2 (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
"""
Implements partial support for Siglent SDS1000X-E series oscilloscopes.
"""
import re

from lab_control.oscilloscope import Oscilloscope
from lab_control.connection.tcp_connection import TCPConnection

def _checkChannel(channel):
    assert channel in SDS1000XE.AVAILABLE_CHANNELS, "SDS1000X-E: Invalid channel {channel}"

class SDS1000XE(Oscilloscope):
    """
    Instances of this class connect to the SDS1000X-E IP
    and port and offer an API to control the device.
    """
    PORT = 5025
    TIMEOUT = 5.0
    AVAILABLE_CHANNELS = range(1, 5)

    def __init__(self, address, overrideConnection=None):
        super().__init__()
        if overrideConnection is not None:
            self._connection = overrideConnection
        else:
            self._connection = TCPConnection(address)

        self._connection.configure({"timeout" : SDS1000XE.TIMEOUT})

    def _measure(self, channel: int, code: str) -> float:
        _checkChannel(channel)
        request = f"C{channel}:PAVA? {code}\r\n"
        response = self._connection.send(request)

        if response is not None:
            pattern = r"C(?P<responseChannel>\d):PAVA .+,(?P<rawMeasurement>[\d.E+-]+)\w+"
            matches = re.search(pattern, response)
            measurement = float(matches.group("rawMeasurement"))
        else:
            measurement = None

        return measurement

    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 setVoltsPerDivision(self, channel: int, volts: float) -> None:
        _checkChannel(channel)
        query = f"C{channel}:VDIV {volts:.2E}V\r\n"
        self._connection.send(query, responseExpected=False)

    def getDivisionsDisplayed(self) -> int:
        return 8