|
| 1 | +"""Serial interface for communicating with PSLab devices.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import platform |
| 5 | + |
| 6 | +import serial |
| 7 | + |
| 8 | +import pslab |
| 9 | +from pslab.connection.connection import ConnectionHandler |
| 10 | + |
| 11 | + |
| 12 | +def _check_serial_access_permission(): |
| 13 | + """Check that we have permission to use the tty on Linux.""" |
| 14 | + if platform.system() == "Linux": |
| 15 | + import grp |
| 16 | + |
| 17 | + if os.geteuid() == 0: # Running as root? |
| 18 | + return |
| 19 | + |
| 20 | + for group in os.getgroups(): |
| 21 | + if grp.getgrgid(group).gr_name in ( |
| 22 | + "dialout", |
| 23 | + "uucp", |
| 24 | + ): |
| 25 | + return |
| 26 | + |
| 27 | + udev_paths = [ |
| 28 | + "/run/udev/rules.d/", |
| 29 | + "/etc/udev/rules.d/", |
| 30 | + "/lib/udev/rules.d/", |
| 31 | + ] |
| 32 | + for p in udev_paths: |
| 33 | + udev_rules = os.path.join(p, "99-pslab.rules") |
| 34 | + if os.path.isfile(udev_rules): |
| 35 | + return |
| 36 | + else: |
| 37 | + raise PermissionError( |
| 38 | + "The current user does not have permission to access " |
| 39 | + "the PSLab device. To solve this, either:" |
| 40 | + "\n\n" |
| 41 | + "1. Add the user to the 'dialout' (on Debian-based " |
| 42 | + "systems) or 'uucp' (on Arch-based systems) group." |
| 43 | + "\n" |
| 44 | + "2. Install a udev rule to allow any user access to the " |
| 45 | + "device by running 'pslab install' as root, or by " |
| 46 | + "manually copying " |
| 47 | + f"{pslab.__path__[0]}/99-pslab.rules into {udev_paths[1]}." |
| 48 | + "\n\n" |
| 49 | + "You may also need to reboot the system for the " |
| 50 | + "permission changes to take effect." |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class SerialHandler(ConnectionHandler): |
| 55 | + """Interface for controlling a PSLab over a serial port. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + port : str |
| 60 | + baudrate : int, default 1 MBd |
| 61 | + timeout : float, default 1 s |
| 62 | + """ |
| 63 | + |
| 64 | + # V5 V6 |
| 65 | + _USB_VID = [0x04D8, 0x10C4] |
| 66 | + _USB_PID = [0x00DF, 0xEA60] |
| 67 | + |
| 68 | + def __init__( |
| 69 | + self, |
| 70 | + port: str, |
| 71 | + baudrate: int = 1000000, |
| 72 | + timeout: float = 1.0, |
| 73 | + ): |
| 74 | + self._port = port |
| 75 | + self._ser = serial.Serial( |
| 76 | + baudrate=baudrate, |
| 77 | + timeout=timeout, |
| 78 | + write_timeout=timeout, |
| 79 | + ) |
| 80 | + _check_serial_access_permission() |
| 81 | + |
| 82 | + @property |
| 83 | + def port(self) -> str: |
| 84 | + """Serial port.""" |
| 85 | + return self._port |
| 86 | + |
| 87 | + @property |
| 88 | + def baudrate(self) -> int: |
| 89 | + """Symbol rate.""" |
| 90 | + return self._ser.baudrate |
| 91 | + |
| 92 | + @baudrate.setter |
| 93 | + def baudrate(self, value: int) -> None: |
| 94 | + self._ser.baudrate = value |
| 95 | + |
| 96 | + @property |
| 97 | + def timeout(self) -> float: |
| 98 | + """Timeout in seconds.""" |
| 99 | + return self._ser.timeout |
| 100 | + |
| 101 | + @timeout.setter |
| 102 | + def timeout(self, value: float) -> None: |
| 103 | + self._ser.timeout = value |
| 104 | + self._ser.write_timeout = value |
| 105 | + |
| 106 | + def connect(self) -> None: |
| 107 | + """Connect to PSLab.""" |
| 108 | + self._ser.port = self.port |
| 109 | + self._ser.open() |
| 110 | + |
| 111 | + try: |
| 112 | + self.get_version() |
| 113 | + except Exception: |
| 114 | + self._ser.close() |
| 115 | + raise |
| 116 | + |
| 117 | + def disconnect(self): |
| 118 | + """Disconnect from PSLab.""" |
| 119 | + self._ser.close() |
| 120 | + |
| 121 | + def read(self, number_of_bytes: int) -> bytes: |
| 122 | + """Read bytes from serial port. |
| 123 | +
|
| 124 | + Parameters |
| 125 | + ---------- |
| 126 | + number_of_bytes : int |
| 127 | + Number of bytes to read from the serial port. |
| 128 | +
|
| 129 | + Returns |
| 130 | + ------- |
| 131 | + bytes |
| 132 | + Bytes read from the serial port. |
| 133 | + """ |
| 134 | + return self._ser.read(number_of_bytes) |
| 135 | + |
| 136 | + def write(self, data: bytes) -> int: |
| 137 | + """Write bytes to serial port. |
| 138 | +
|
| 139 | + Parameters |
| 140 | + ---------- |
| 141 | + data : int |
| 142 | + Bytes to write to the serial port. |
| 143 | +
|
| 144 | + Returns |
| 145 | + ------- |
| 146 | + int |
| 147 | + Number of bytes written. |
| 148 | + """ |
| 149 | + return self._ser.write(data) |
| 150 | + |
| 151 | + def __repr__(self) -> str: # noqa |
| 152 | + return ( |
| 153 | + f"{self.__class__.__name__}" |
| 154 | + "[" |
| 155 | + f"{self.port}, " |
| 156 | + f"{self.baudrate} baud, " |
| 157 | + f"timeout {self.timeout} s" |
| 158 | + "]" |
| 159 | + ) |
0 commit comments