'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.'

def read(self, size=1):

    if (not self.is_open):
        raise portNotOpenError
    read = bytearray()
    timeout = self._timeout
    while (len(read) < size):
        try:
            start_time = time.time()
            (ready, _, _) = select.select([self._socket], [], [], timeout)
            if (not ready):
                break
            buf = self._socket.recv((size - len(read)))
            if (not buf):
                raise SerialException('socket    disconnected')
            read.extend(buf)
            if (timeout is not None):
                timeout -= (time.time() - start_time)
                if (timeout <= 0):
                    break
        except socket.timeout:
            pass
        except socket.error as e:
            raise SerialException('connection    failed    ({})'.format(e))
        except OSError as e:
            if (e.errno != errno.EAGAIN):
                raise SerialException('read    failed:    {}'.format(e))
        except select.error as e:
            if (e[0] != errno.EAGAIN):
                raise SerialException('read    failed:    {}'.format(e))
    return bytes(read)
