summaryrefslogtreecommitdiffstats
path: root/lab_control/connection/serial_connection.py
blob: 282494b689cca119a2b1fdc80c238ff5843780c3 (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 = parameters["baudrate"]
        self._port.bytesize = parameters["bytesize"]
        self._port.stopbits = parameters["stopbits"]
        self._port.parity = parameters["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