SACADA.py 1.54 KB
Newer Older
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from serial import Serial

class InvalidChannel(Exception):
    pass

class InvalidOption(Exception):
    pass

class SACADA(object):

    """SACADA boards Python interface
       Right now it just implements support for SACADA Mini using SCPI
       over USB-CDC. The plan is to provide a generic base class for use
       with all SACADA boards.
    """

17
    IN_CHANNELS = ["A0", "A1", "A2", "A3", "A4", "INSTR", "TC"]
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
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
    OUT_CHANNELS = ["CH1", "CH2"]

    def __init__(self, location):
        self.open(location)

    def open(self, location):
        self._serial = Serial(location, timeout=1) # We use USB-CDC so baud rate doesn't matter

    def close(self):
        self._serial.close()

    def identify(self):
        return self.sendSCPICommand("*IDN?")

    def sendSCPICommand(self, command):
        self._write(command + "\r\n") # SACADA Mini requires \r\n at the end of commands
        return self._read()

    def readVoltage(self, channel):
        if channel not in self.IN_CHANNELS:
            raise InvalidChannel("{} is not a valid input channel".format(channel))

        return float(self.sendSCPICommand("MEAS:VOLT:DC? {}".format(channel)))

    def setVoltage(self, channel, voltage):
        if channel not in self.OUT_CHANNELS:
            raise InvalidChannel("{} is not a valid output channel".format(channel))

        return float(self.sendSCPICommand("SET:VOLT:DC {} {}".format(channel, voltage)))

    def _write(self, m):
        self._serial.write(bytes(m, "utf-8"))

    def _read(self):
        return self._serial.readline().strip()