コード例 #1
0
    def read(self, size=1):
        """Read size bytes.

        If a timeout is set it may return fewer characters than requested.
        With no timeout it will block until the requested number of bytes
        is read.

        Args:
            size: Number of bytes to read.

        Returns:
            Bytes read from the port, of type 'bytes'.
        """
        if not self.is_open:
            raise serialutil.portNotOpenError

        # Not checking if the config is OK, so will try to read from a possibly
        # empty queue if using the wrong baudrate, etc. This is deliberate.

        response = bytearray()
        timeout_obj = serialutil.Timeout(self.timeout)
        while True:
            b = self._read1(timeout_obj)
            if b:
                response.extend(b)
                if size is not None and len(response) >= size:
                    break
            else:  # pragma: no cover
                # The timeout expired while in _read1.
                break
            if timeout_obj.expired():  # pragma: no cover
                break
        response = bytes(response)
        return response
コード例 #2
0
 def test_simple_timeout(self):
     """Test simple timeout"""
     t = serialutil.Timeout(2)
     self.assertFalse(t.expired())
     self.assertTrue(t.time_left() > 0)
     time.sleep(2.1)
     self.assertTrue(t.expired())
     self.assertEqual(t.time_left(), 0)
コード例 #3
0
    def write(self, data):
        """Write the bytes data to the port.

        Args:
            data: The data to write (bytes or bytearray instance).

        Returns:
            Number of bytes written.

        Raises:
            SerialTimeoutException: In case a write timeout is configured for
                the port and the time is exceeded.
        """
        if not isinstance(data, (bytes, bytearray)):
            raise ValueError("write takes bytes")
        data = bytes(data)  # Make sure it can't change.
        self.logger.info(f'AstrohavenSerialSimulator.write({data!r})')
        count = 0
        timeout_obj = serialutil.Timeout(self.write_timeout)
        for b in data:
            self._write1(b, timeout_obj)
            count += 1
        return count
コード例 #4
0
 def test_blocking(self):
     """Test no timeout (None)"""
     t = serialutil.Timeout(None)
     self.assertFalse(t.is_non_blocking)
     self.assertTrue(t.is_infinite)
コード例 #5
0
 def test_non_blocking(self):
     """Test nonblocking case (0)"""
     t = serialutil.Timeout(0)
     self.assertTrue(t.is_non_blocking)
     self.assertFalse(t.is_infinite)
     self.assertTrue(t.expired())