Exemplo n.º 1
0
    def write(self, buf: bytes) -> int:
        if self.closed:
            raise channel.ChannelClosedException()

        channel.channel._debug_log(self, buf, True)
        self.serial.write(buf)
        return len(buf)
Exemplo n.º 2
0
    def read(self, n: int, timeout: typing.Optional[float] = None) -> bytes:
        if self.closed:
            raise channel.ChannelClosedException()

        try:
            # Block for the first byte only
            self.serial.timeout = timeout
            first = self.serial.read(1)

            if first == b"":
                raise TimeoutError()

            assert len(
                first) == 1, f"Result is longer than expected ({first!r})!"
        finally:
            self.serial.timeout = 0

        remaining = b""
        if n > 1:
            # If there is more, read it now (non-blocking)
            remaining = self.serial.read(min(n, READ_CHUNK_SIZE) - 1)

        return channel.channel._debug_log(self, first + remaining, False)
Exemplo n.º 3
0
    def close(self) -> None:
        if self.closed:
            raise channel.ChannelClosedException()

        self.serial.close()