'Output the given byte string over the serial port. Can block if the
connection is blocked. May raise SerialException if the connection is
closed.'

def write(self, data):

    self._cancel_write = False
    if (not self.is_open):
        raise portNotOpenError
    data = to_bytes(data)
    time_used_to_send = ((10.0 * len(data)) / self._baudrate)
    if ((self._write_timeout is not None) and (time_used_to_send > self._write_timeout)):
        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
        raise writeTimeoutError
    for byte in iterbytes(data):
        self.queue.put(byte, timeout=self._write_timeout)
    return len(data)
