summaryrefslogtreecommitdiffstats
path: root/lab_control/jds6600.py
blob: 2321a4b880e3aa6e4636d394c473e88afdb77403 (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
import serial
import re

from lab_control.function_generator import FunctionGenerator

class JDS6600(FunctionGenerator):
    AVAILABLE_CHANNELS = [1, 2]

    def __init__(self, portName):
        self._port = serial.Serial(portName)
        self._port.baudrate = 115200
        self._port.bytesize = serial.EIGHTBITS
        self._port.stopbits = serial.STOPBITS_ONE
        self._port.parity = serial.PARITY_NONE

    def _sendRequest(self, opcode: str, args: str) -> str:
        request = f":{opcode}={args}.\r\n"
        self._port.write(request.encode())
        responseRaw = self._port.readline()
        return responseRaw.decode().strip()

    def closePort(self) -> None:
        self._port.close()

    def _queryOnOff(self) -> list[str, str]:
        # TODO double check query on the device
        response = self._sendRequest("r20", "")
        return [response[5], response[7]]

    def setOn(self, channel: int) -> None:
        assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}"

        state = self._queryOnOff()
        state[channel - 1] = "1"
        response = self._sendRequest("w20", f"{state[0]},{state[1]}")

    def setOff(self, channel: int) -> None:
        assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}"

        state = self._queryOnOff()
        state[channel - 1] = "0"
        response = self._sendRequest("w20", f"{state[0]},{state[1]}")

    def setFrequency(self, channel: int, frequency: float) -> None:
        assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}"
        assert frequency is not None and frequency >= 0.0 and frequency <= 60e6, f"JDS6600: Invalid frequency {frequency}"

        # TODO implement after testing on actual device
        opcode = f"w{23 + channel - 1}"

    def setAmplitude(self, channel: int, amplitude: float) -> None:
        assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}"
        assert amplitude is not None and amplitude >= 0.0 and amplitude <= 20.0, f"JDS6600: Invalid amplitude {amplitude}"

        opcode = f"w{25 + channel - 1}"
        arg = int(amplitude * 1000.0)
        response = self._sendRequest(opcode, str(arg))

    def setFunction(self, channel: int, function: int) -> None:
        assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}"
        assert function is not None and function >= 0 and function <= 16, f"JDS6600: Invalid function code {function}"

        opcode = f"w{21 + channel - 1}"
        response = self._sendRequest(opcode, str(function))