summaryrefslogtreecommitdiffstats
path: root/lab_control/connection/serial_connection.py
blob: 2e48d5669a124fdd95ef8e7aaf2560e4aeb48add (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
import termios
import serial

class SerialConnection:
    def __init__(self, portName):
        self._port = serial.Serial(portName)

    def configure(self, config: dict) -> None:
        self._port.baudrate = config["baudrate"]
        self._port.bytesize = config["bytesize"]
        self._port.stopbits = config["stopbits"]
        self._port.parity = config["parity"]

    def send(self, request):
        self._port.write(request.encode())
        return self._port.readline().decode()
        
    def checkConfiguration(self) -> None:
        iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(self._port)

        # JDS6600 configuration taken from manual
        assert ispeed == termios.B115200
        assert ospeed == termios.B115200
        assert (cflag & termios.CSIZE) == termios.CS8
        assert (cflag & termios.CSTOPB) == 0
        assert (cflag & (termios.PARENB | termios.PARODD)) == 0