def read(self, size=1, timeout=None): """ 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.fd is None: raise serial.portNotOpenError read = [] nread = 0 fd = self.fd fds = [fd] # timeout in seconds if timeout is None and self._timeout: timeout = self._timeout if size > 0: while nread < size: # print "\tread(): size",size, "have", len(read) #debug ready, _, _ = select(fds, [], [], timeout) if not ready: break # timeout buf = os_read(fd, size - nread) if not buf: break # early abort on timeout or error read.append(buf) nread += len(buf) return ''.join(read)
def read(self): """Read an IP packet been sent to this TUN device.""" try: return os_read(self._fileno_iface_fd, 1024 * 4) except TypeError: # read after closing return None
def run_pipe_session(ifd, ofd, log_file=None, log_level=logging.CRITICAL): try: init_logging(log_file, log_level) log.info('Single-session mode on fds (%d, %d)', ifd, ofd) run_session(lambda n: os_read(ifd, n), lambda data: os_write_all(ofd, data)) log.info('Debug session ended. Exiting.') except Exception as e: log.critical('%s', traceback.format_exc())
def run_pipe_session(ifd, ofd, params=None): try: params = decode_params(params) init_logging(params) log.info('Single-session mode on fds (%d, %d)', ifd, ofd) run_session(lambda n: os_read(ifd, n), lambda data: os_write_all(ofd, data), params) log.info('Debug session has ended. Exiting.') except Exception as e: log.critical('%s', traceback.format_exc())
def run_stdio_session(ifd=0, ofd=1, ext_channel_port=None, log_file=None, log_level=logging.CRITICAL): if log_file is not None: import base64 log_file = base64.b64decode(log_file) init_logging(log_file, log_level) log.info('Single-session mode on fds (%d,%d)', ifd, ofd) run_session(lambda n: os_read(ifd, n), lambda data: os_write_all(ofd, data), ext_channel_port) log.info('Debug session ended. Exiting.')
def run_stdio_session(log_file=None, log_level=logging.CRITICAL): try: init_logging(log_file, log_level) # Keeping all imported components from spamming stdout is pretty futile; # just relocate stdio to different fds. ifd = os.dup(0) ofd = os.dup(1) os.dup2(os.open(os.devnull, os.O_RDONLY), 0) os.dup2(os.open(os.devnull, os.O_WRONLY), 1) log.info('Single-session mode on stdio') run_session(lambda n: os_read(ifd, n), lambda data: os_write_all(ofd, data)) log.info('Debug session ended. Exiting.') except Exception as e: log.critical('%s', traceback.format_exc())
def run_stdio_session(params=None): try: params = decode_params(params) init_logging(params) # Keeping all imported components from spamming stdout is pretty futile; # just relocate stdio to different fds. ifd = os.dup(0) ofd = os.dup(1) os.dup2(os.open(os.devnull, os.O_RDONLY), 0) os.dup2(os.open(os.devnull, os.O_WRONLY), 1) log.info('Single-session mode on stdio') run_session(lambda n: os_read(ifd, n), lambda data: os_write_all(ofd, data), params) log.info('Debug session has ended. Exiting.') except Exception as e: log.critical('%s', traceback.format_exc())
def getkey(cls) -> bytes: """Return a key from the current console, in a platform independent way. """ # there's probably a better way to initialize the module without # relying onto a singleton pattern. To be fixed if cls.IS_MSWIN: # w/ py2exe, it seems the importation fails to define the global # symbol 'msvcrt', to be fixed while True: char = msvcrt.getch() if char == b'\r': return b'\n' return char else: char = os_read(stdin.fileno(), 1) return char
def gpiod_line_event_read_fd(fd: int, event: gpiod_line_event) -> int: """ @brief Read the last GPIO event directly from a file descriptor. @param fd: File descriptor. @param event: Buffer in which the event data will be stored. @return 0 if the event was read correctly, -1 on error. Users who directly poll the file descriptor for incoming events can also directly read the event data from it using this routine. This function translates the kernel representation of the event to the libgpiod format. """ evdata = gpioevent_data() try: rd = os_read(fd, sizeof(evdata)) except OSError: return -1 if len(rd) != sizeof(evdata): set_errno(EIO) return -1 memmove(pointer(evdata), rd, sizeof(evdata)) event.event_type = ( GPIOD_LINE_EVENT_RISING_EDGE if evdata.id == GPIOEVENT_EVENT_RISING_EDGE else GPIOD_LINE_EVENT_FALLING_EDGE ) sec = evdata.timestamp // 1_000_000_000 event.ts = datetime(year=1970, month=1, day=1) + timedelta( days=sec // 86400, seconds=sec % 86400, microseconds=(evdata.timestamp % 1_000_000_000) // 1000, ) return 0
def getkey() -> str: """Return a key from the current console, in a platform independent way""" # there's probably a better way to initialize the module without # relying onto a singleton pattern. To be fixed if os_name == 'nt': # w/ py2exe, it seems the importation fails to define the global # symbol 'msvcrt', to be fixed while 1: char = msvcrt.getch() if char == '\3': raise KeyboardInterrupt('Ctrl-C break') if char == '\0': msvcrt.getch() else: if char == '\r': return '\n' return char elif os_name == 'posix': char = os_read(stdin.fileno(), 1) return char else: import time time.sleep(1) return None
def li_read(args): return os_read(args[0].val, args[1].val)