diff options
Diffstat (limited to 'lab_control/jds6600.py')
-rw-r--r-- | lab_control/jds6600.py | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/lab_control/jds6600.py b/lab_control/jds6600.py index 006a041..5ada6cf 100644 --- a/lab_control/jds6600.py +++ b/lab_control/jds6600.py @@ -1,25 +1,44 @@ +""" +Implements partial support for Joy-IT JDS6600 function generator. +""" + import serial -import re from lab_control.function_generator import FunctionGenerator +def _checkChannel(channel: int): + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + +def _checkArg(arg: float, lowerBound: float, upperBound: float): + valid = arg is not None + if valid: + valid &= arg >= lowerBound + valid &= arg <= upperBound + assert valid, f"JDS6600: Invalid argument {arg}" + class JDS6600(FunctionGenerator): + """ + Instances of this class bind to the JDS6600 serial port + and offer an API to control the device. + """ AVAILABLE_CHANNELS = [1, 2] + SINE = 0 + SQUARE = 1 + TRIANGULAR = 3 def __init__(self, portName): + super().__init__() 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: + """ + Close the serial port. Instances of this class + are no longer usable after this is called. + """ self._port.close() def _queryOnOff(self) -> list[str, str]: @@ -27,39 +46,45 @@ class JDS6600(FunctionGenerator): response = self._sendRequest("r20") return [response[5], response[7]] + 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 setOn(self, channel: int) -> None: - assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + _checkChannel(channel) state = self._queryOnOff() state[channel - 1] = "1" - response = self._sendRequest("w20", f"{state[0]},{state[1]}") + 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}" + _checkChannel(channel) state = self._queryOnOff() state[channel - 1] = "0" - response = self._sendRequest("w20", f"{state[0]},{state[1]}") + 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}" + _checkChannel(channel) + _checkArg(frequency, 0.0, 60e6) opcode = f"w{23 + channel - 1}" arg = int(frequency * 100.0) - response = self._sendRequest(opcode, str(arg)) + self._sendRequest(opcode, str(arg)) 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}" + _checkChannel(channel) + _checkArg(amplitude, 0.0, 20.0) opcode = f"w{25 + channel - 1}" arg = int(amplitude * 1000.0) - response = self._sendRequest(opcode, str(arg)) + 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}" + _checkChannel(channel) + _checkArg(function, 0, 16) opcode = f"w{21 + channel - 1}" - response = self._sendRequest(opcode, str(function)) + self._sendRequest(opcode, str(function)) |