Esempio n. 1
0
 def write(self, data):
     """\
     Output the given byte string over the serial port. Can block if the
     connection is blocked. May raise SerialException if the connection is
     closed.
     """
     self._cancel_write = False
     if not self.is_open:
         raise portNotOpenError
     data = to_bytes(data)
     # calculate aprox time that would be used to send the data
     time_used_to_send = 10.0 * len(data) / self._baudrate
     # when a write timeout is configured check if we would be successful
     # (not sending anything, not even the part that would have time)
     if self._write_timeout is not None and time_used_to_send > self._write_timeout:
         # must wait so that unit test succeeds
         time_left = self._write_timeout
         while time_left > 0 and not self._cancel_write:
             time.sleep(min(time_left, 0.5))
             time_left -= 0.5
         if self._cancel_write:
             return 0  # XXX
         raise writeTimeoutError
     for byte in iterbytes(data):
         self.queue.put(byte, timeout=self._write_timeout)
     return len(data)
 def write(self, data):
     """\
     Output the given byte string over the serial port. Can block if the
     connection is blocked. May raise SerialException if the connection is
     closed.
     """
     self._cancel_write = False
     if not self.is_open:
         raise portNotOpenError
     data = to_bytes(data)
     # calculate aprox time that would be used to send the data
     time_used_to_send = 10.0 * len(data) / self._baudrate
     # when a write timeout is configured check if we would be successful
     # (not sending anything, not even the part that would have time)
     if self._write_timeout is not None and time_used_to_send > self._write_timeout:
         # must wait so that unit test succeeds
         time_left = self._write_timeout
         while time_left > 0 and not self._cancel_write:
             time.sleep(min(time_left, 0.5))
             time_left -= 0.5
         if self._cancel_write:
             return 0  # XXX
         raise writeTimeoutError
     for byte in iterbytes(data):
         self.queue.put(byte, timeout=self._write_timeout)
     return len(data)