Python termios.CSIZE Examples
The following are 7
code examples of termios.CSIZE().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
termios
, or try the search function
.
Example #1
Source File: serial.py From python-periphery with MIT License | 6 votes |
def _set_databits(self, databits): if not isinstance(databits, int): raise TypeError("Invalid data bits type, should be integer.") elif databits not in [5, 6, 7, 8]: raise ValueError("Invalid data bits, can be 5, 6, 7, 8.") # Get tty attributes try: (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd) except termios.error as e: raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror) # Modify tty attributes cflag &= ~termios.CSIZE cflag |= Serial._DATABITS_TO_CFLAG[databits] # Set tty attributes try: termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]) except termios.error as e: raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
Example #2
Source File: input_kbd.py From DeepPicar-v2 with GNU General Public License v2.0 | 6 votes |
def init(): fd = sys.stdin.fileno() # save old state flags_save = fcntl.fcntl(fd, fcntl.F_GETFL) attrs_save = termios.tcgetattr(fd) # make raw - the way to do this comes from the termios(3) man page. attrs = list(attrs_save) # copy the stored version to update # iflag attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP | termios.INLCR | termios. IGNCR | termios.ICRNL | termios.IXON ) # oflag attrs[1] &= ~termios.OPOST # cflag attrs[2] &= ~(termios.CSIZE | termios. PARENB) attrs[2] |= termios.CS8 # lflag attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON | termios.ISIG | termios.IEXTEN) termios.tcsetattr(fd, termios.TCSANOW, attrs) # turn off non-blocking fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK) # read a single keystroke return (flags_save, attrs_save)
Example #3
Source File: serial.py From rpi3-webiopi with Apache License 2.0 | 5 votes |
def __init__(self, device="/dev/ttyAMA0", baudrate=9600): if not device.startswith("/dev/"): device = "/dev/%s" % device if isinstance(baudrate, str): baudrate = int(baudrate) aname = "B%d" % baudrate if not hasattr(termios, aname): raise Exception("Unsupported baudrate") self.baudrate = baudrate Bus.__init__(self, "UART", device, os.O_RDWR | os.O_NOCTTY) fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NDELAY) #backup = termios.tcgetattr(self.fd) options = termios.tcgetattr(self.fd) # iflag options[0] = 0 # oflag options[1] = 0 # cflag options[2] |= (termios.CLOCAL | termios.CREAD) options[2] &= ~termios.PARENB options[2] &= ~termios.CSTOPB options[2] &= ~termios.CSIZE options[2] |= termios.CS8 # lflag options[3] = 0 speed = getattr(termios, aname) # input speed options[4] = speed # output speed options[5] = speed termios.tcsetattr(self.fd, termios.TCSADRAIN, options)
Example #4
Source File: serial.py From python-periphery with MIT License | 5 votes |
def _get_databits(self): # Get tty attributes try: (_, _, cflag, _, _, _, _) = termios.tcgetattr(self._fd) except termios.error as e: raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror) cs = cflag & termios.CSIZE if cs not in Serial._CFLAG_TO_DATABITS: raise SerialError(None, "Unknown data bits setting: csize 0x{:x}".format(cs)) return Serial._CFLAG_TO_DATABITS[cs]
Example #5
Source File: cli.py From simdem with MIT License | 4 votes |
def get_instruction_key(self): """Waits for a single keypress on stdin. This is a silly function to call if you need to do it a lot because it has to store stdin's current setup, setup stdin for reading single keystrokes then read the single keystroke then revert stdin back after reading the keystroke. Returns the character of the key that was pressed (zero on KeyboardInterrupt which can happen when a signal gets handled) This method is licensed under cc by-sa 3.0 Thanks to mheyman http://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-for-a-pressed-key\ """ import termios, fcntl, sys, os fd = sys.stdin.fileno() # save old state flags_save = fcntl.fcntl(fd, fcntl.F_GETFL) attrs_save = termios.tcgetattr(fd) # make raw - the way to do this comes from the termios(3) man page. attrs = list(attrs_save) # copy the stored version to update # iflag attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP | termios.INLCR | termios. IGNCR | termios.ICRNL | termios.IXON ) # oflag attrs[1] &= ~termios.OPOST # cflag attrs[2] &= ~(termios.CSIZE | termios. PARENB) attrs[2] |= termios.CS8 # lflag attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON | termios.ISIG | termios.IEXTEN) termios.tcsetattr(fd, termios.TCSANOW, attrs) # turn off non-blocking fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK) # read a single keystroke try: ret = sys.stdin.read(1) # returns a single character except KeyboardInterrupt: ret = 0 finally: # restore old state termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save) fcntl.fcntl(fd, fcntl.F_SETFL, flags_save) return ret
Example #6
Source File: Pipe.py From redeem with GNU General Public License v3.0 | 4 votes |
def __init__(self, printer, prot, iomanager): self.printer = printer self.prot = prot self.iomanager = iomanager (master_fd, slave_fd) = os.openpty() slave = os.ttyname(slave_fd) master_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL, 0) fcntl.fcntl(master_fd, fcntl.F_SETFL, master_flags | os.O_NONBLOCK) # switch to "raw" mode - these constants come from the manpage for termios under cfmakeraw() master_attr = termios.tcgetattr(master_fd) master_attr[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP | termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IXON) master_attr[1] &= ~termios.OPOST master_attr[2] &= ~(termios.CSIZE | termios.PARENB) master_attr[3] &= ~(termios.ECHO | termios.ECHONL | termios.ICANON | termios.ISIG | termios.IEXTEN) master_attr[3] |= termios.CS8 termios.tcsetattr(master_fd, termios.TCSADRAIN, master_attr) # Fun detail: master will always show as /dev/ptmx, but the kernel knows from # the fd which PTY we're using. This means we have to use master_fd instead # of opening master by name. logging.info("Opened PTY for {} and got {}".format(prot, os.ttyname(slave_fd))) self.pipe_link = "/dev/" + prot + "_1" try: os.unlink(self.pipe_link) except OSError as e: # file not found is fine to ignore - anythine else and we should log it if e.errno != errno.ENOENT: logging.error("Failed to unlink '{}': {}".format(self.pipe_link, e.strerror)) logging.info("linking {}".format(self.pipe_link)) os.symlink(slave, self.pipe_link) os.chmod(self.pipe_link, 0o666) logging.info("{} Pipe open. Use '{}' to communicate with it".format(self.prot, self.pipe_link)) self.rd = os.fdopen(master_fd, "r") self.wr = os.fdopen(master_fd, "w") self.send_response = True self.iomanager.add_file(self.rd, self.get_message)
Example #7
Source File: misc.py From fast-autocomplete with MIT License | 4 votes |
def read_single_keypress(): """Waits for a single keypress on stdin. https://stackoverflow.com/a/6599441/1497443 This is a silly function to call if you need to do it a lot because it has to store stdin's current setup, setup stdin for reading single keystrokes then read the single keystroke then revert stdin back after reading the keystroke. Returns the character of the key that was pressed (zero on KeyboardInterrupt which can happen when a signal gets handled) """ if fcntl is None or termios is None: raise ValueError('termios and/or fcntl packages are not available in your system. This is possible because you are not on a Linux Distro.') fd = sys.stdin.fileno() # save old state flags_save = fcntl.fcntl(fd, fcntl.F_GETFL) attrs_save = termios.tcgetattr(fd) # make raw - the way to do this comes from the termios(3) man page. attrs = list(attrs_save) # copy the stored version to update # iflag attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP | termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IXON) # oflag attrs[1] &= ~termios.OPOST # cflag attrs[2] &= ~(termios.CSIZE | termios. PARENB) attrs[2] |= termios.CS8 # lflag attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON | termios.ISIG | termios.IEXTEN) termios.tcsetattr(fd, termios.TCSANOW, attrs) # turn off non-blocking fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK) # read a single keystroke try: ret = sys.stdin.read(1) # returns a single character except KeyboardInterrupt: ret = 0 finally: # restore old state termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save) fcntl.fcntl(fd, fcntl.F_SETFL, flags_save) return ret