def write(self, data): """Output the given string over the serial port.""" if self.sock is None: import serial raise serial.portNotOpenError() t = len(data) d = data while t > 0: try: if self._writeTimeout is not None and self._writeTimeout > 0: _, ready, _ = select.select([], [self.sock], [], self._writeTimeout) if not ready: raise serial.writeTimeoutError n = self.sock.send(d) if self._dump: print hexdump(d[:n]) if self._writeTimeout is not None and self._writeTimeout > 0: _, ready, _ = select.select([], [self.sock], [], self._writeTimeout) if not ready: raise serial.writeTimeoutError d = d[n:] t = t - n except OSError, v: if v.errno != errno.EAGAIN: raise
def read(self, size=1): """Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.""" if self.sock is None: import serial raise serial.portNotOpenError() read = '' if size > 0: while len(read) < size: ready, _, _ = select.select([self.sock], [], [], self._timeout) if not ready: break # timeout buf = self.sock.recv(size-len(read)) if not len(buf): # Some character is ready, but none can be read # it seems that this is a marker for a dead peer # Exception does not work, for some reason (missing ioctl?) import serial raise serial.SerialException('Peer disconnected') read = read + buf if self._timeout >= 0 and not buf: break # early abort on timeout return read
def read(self, size=1): """Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.""" if self.sock is None: import serial raise serial.portNotOpenError() read = '' if size > 0: while len(read) < size: ready, _, _ = select.select([self.sock], [], [], self._timeout) if not ready: break # timeout buf = self.sock.recv(size - len(read)) if not len(buf): # Some character is ready, but none can be read # it seems that this is a marker for a dead peer # Exception does not work, for some reason (missing ioctl?) import serial raise serial.SerialException('Peer disconnected') read = read + buf if self._timeout >= 0 and not buf: break # early abort on timeout return read