parameters: (self, force_update=False)
docstring: Set communication parameters on opened port.
docstring_summary: Set communication parameters on opened port.
docstring_tokens: Set communication parameters on opened port .
function: def _reconfigure_port(self, force_update=False):
          """Set communication parameters on opened port."""
          if self.fd is None:
              raise SerialException("Can only operate on a valid file descriptor")
  
          # if exclusive lock is requested, create it before we modify anything else
          if self._exclusive is not None:
              if self._exclusive:
                  try:
                      fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
                  except IOError as msg:
                      raise SerialException(msg.errno, "Could not exclusively lock port {}: {}".format(self._port, msg))
              else:
                  fcntl.flock(self.fd, fcntl.LOCK_UN)
  
          custom_baud = None
  
          vmin = vtime = 0                # timeout is done via select
          if self._inter_byte_timeout is not None:
              vmin = 1
              vtime = int(self._inter_byte_timeout * 10)
          try:
              orig_attr = termios.tcgetattr(self.fd)
              iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr
          except termios.error as msg:      # if a port is nonexistent but has a /dev file, it'll fail here
              raise SerialException("Could not configure port: {}".format(msg))
          # set up raw mode / no echo / binary
          cflag |= (termios.CLOCAL | termios.CREAD)
          lflag &= ~(termios.ICANON | termios.ECHO | termios.ECHOE |
                     termios.ECHOK | termios.ECHONL |
                     termios.ISIG | termios.IEXTEN)  # |termios.ECHOPRT
          for flag in ('ECHOCTL', 'ECHOKE'):  # netbsd workaround for Erk
              if hasattr(termios, flag):
                  lflag &= ~getattr(termios, flag)
  
          oflag &= ~(termios.OPOST | termios.ONLCR | termios.OCRNL)
          iflag &= ~(termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IGNBRK)
          if hasattr(termios, 'IUCLC'):
              iflag &= ~termios.IUCLC
          if hasattr(termios, 'PARMRK'):
              iflag &= ~termios.PARMRK
  
          # setup baud rate
          try:
              ispeed = ospeed = getattr(termios, 'B{}'.format(self._baudrate))
          except AttributeError:
              try:
                  ispeed = ospeed = self.BAUDRATE_CONSTANTS[self._baudrate]
              except KeyError:
                  #~ raise ValueError('Invalid baud rate: %r' % self._baudrate)
                  # may need custom baud rate, it isn't in our list.
                  ispeed = ospeed = getattr(termios, 'B38400')
                  try:
                      custom_baud = int(self._baudrate)  # store for later
                  except ValueError:
                      raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate))
                  else:
                      if custom_baud < 0:
                          raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate))
  
          # setup char len
          cflag &= ~termios.CSIZE
          if self._bytesize == 8:
              cflag |= termios.CS8
          elif self._bytesize == 7:
              cflag |= termios.CS7
          elif self._bytesize == 6:
              cflag |= termios.CS6
          elif self._bytesize == 5:
              cflag |= termios.CS5
          else:
              raise ValueError('Invalid char len: {!r}'.format(self._bytesize))
          # setup stop bits
          if self._stopbits == serial.STOPBITS_ONE:
              cflag &= ~(termios.CSTOPB)
          elif self._stopbits == serial.STOPBITS_ONE_POINT_FIVE:
              cflag |= (termios.CSTOPB)  # XXX same as TWO.. there is no POSIX support for 1.5
          elif self._stopbits == serial.STOPBITS_TWO:
              cflag |= (termios.CSTOPB)
          else:
              raise ValueError('Invalid stop bit specification: {!r}'.format(self._stopbits))
          # setup parity
          iflag &= ~(termios.INPCK | termios.ISTRIP)
          if self._parity == serial.PARITY_NONE:
              cflag &= ~(termios.PARENB | termios.PARODD | CMSPAR)
          elif self._parity == serial.PARITY_EVEN:
              cflag &= ~(termios.PARODD | CMSPAR)
              cflag |= (termios.PARENB)
          elif self._parity == serial.PARITY_ODD:
              cflag &= ~CMSPAR
              cflag |= (termios.PARENB | termios.PARODD)
          elif self._parity == serial.PARITY_MARK and CMSPAR:
              cflag |= (termios.PARENB | CMSPAR | termios.PARODD)
          elif self._parity == serial.PARITY_SPACE and CMSPAR:
              cflag |= (termios.PARENB | CMSPAR)
              cflag &= ~(termios.PARODD)
          else:
              raise ValueError('Invalid parity: {!r}'.format(self._parity))
          # setup flow control
          # xonxoff
          if hasattr(termios, 'IXANY'):
              if self._xonxoff:
                  iflag |= (termios.IXON | termios.IXOFF)  # |termios.IXANY)
              else:
                  iflag &= ~(termios.IXON | termios.IXOFF | termios.IXANY)
          else:
              if self._xonxoff:
                  iflag |= (termios.IXON | termios.IXOFF)
              else:
                  iflag &= ~(termios.IXON | termios.IXOFF)
          # rtscts
          if hasattr(termios, 'CRTSCTS'):
              if self._rtscts:
                  cflag |= (termios.CRTSCTS)
              else:
                  cflag &= ~(termios.CRTSCTS)
          elif hasattr(termios, 'CNEW_RTSCTS'):   # try it with alternate constant name
              if self._rtscts:
                  cflag |= (termios.CNEW_RTSCTS)
              else:
                  cflag &= ~(termios.CNEW_RTSCTS)
          # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails??
  
          # buffer
          # vmin "minimal number of characters to be read. 0 for non blocking"
          if vmin < 0 or vmin > 255:
              raise ValueError('Invalid vmin: {!r}'.format(vmin))
          cc[termios.VMIN] = vmin
          # vtime
          if vtime < 0 or vtime > 255:
              raise ValueError('Invalid vtime: {!r}'.format(vtime))
          cc[termios.VTIME] = vtime
          # activate settings
          if force_update or [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] != orig_attr:
              termios.tcsetattr(
                  self.fd,
                  termios.TCSANOW,
                  [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
  
          # apply custom baud rate, if any
          if custom_baud is not None:
              self._set_special_baudrate(custom_baud)
  
          if self._rs485_mode is not None:
              self._set_rs485_mode(self._rs485_mode)
function_tokens: def _reconfigure_port ( self , force_update = False ) : if self . fd is None : raise SerialException ( "Can only operate on a valid file descriptor" ) # if exclusive lock is requested, create it before we modify anything else if self . _exclusive is not None : if self . _exclusive : try : fcntl . flock ( self . fd , fcntl . LOCK_EX | fcntl . LOCK_NB ) except IOError as msg : raise SerialException ( msg . errno , "Could not exclusively lock port {}: {}" . format ( self . _port , msg ) ) else : fcntl . flock ( self . fd , fcntl . LOCK_UN ) custom_baud = None vmin = vtime = 0 # timeout is done via select if self . _inter_byte_timeout is not None : vmin = 1 vtime = int ( self . _inter_byte_timeout * 10 ) try : orig_attr = termios . tcgetattr ( self . fd ) iflag , oflag , cflag , lflag , ispeed , ospeed , cc = orig_attr except termios . error as msg : # if a port is nonexistent but has a /dev file, it'll fail here raise SerialException ( "Could not configure port: {}" . format ( msg ) ) # set up raw mode / no echo / binary cflag |= ( termios . CLOCAL | termios . CREAD ) lflag &= ~ ( termios . ICANON | termios . ECHO | termios . ECHOE | termios . ECHOK | termios . ECHONL | termios . ISIG | termios . IEXTEN ) # |termios.ECHOPRT for flag in ( 'ECHOCTL' , 'ECHOKE' ) : # netbsd workaround for Erk if hasattr ( termios , flag ) : lflag &= ~ getattr ( termios , flag ) oflag &= ~ ( termios . OPOST | termios . ONLCR | termios . OCRNL ) iflag &= ~ ( termios . INLCR | termios . IGNCR | termios . ICRNL | termios . IGNBRK ) if hasattr ( termios , 'IUCLC' ) : iflag &= ~ termios . IUCLC if hasattr ( termios , 'PARMRK' ) : iflag &= ~ termios . PARMRK # setup baud rate try : ispeed = ospeed = getattr ( termios , 'B{}' . format ( self . _baudrate ) ) except AttributeError : try : ispeed = ospeed = self . BAUDRATE_CONSTANTS [ self . _baudrate ] except KeyError : #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) # may need custom baud rate, it isn't in our list. ispeed = ospeed = getattr ( termios , 'B38400' ) try : custom_baud = int ( self . _baudrate ) # store for later except ValueError : raise ValueError ( 'Invalid baud rate: {!r}' . format ( self . _baudrate ) ) else : if custom_baud < 0 : raise ValueError ( 'Invalid baud rate: {!r}' . format ( self . _baudrate ) ) # setup char len cflag &= ~ termios . CSIZE if self . _bytesize == 8 : cflag |= termios . CS8 elif self . _bytesize == 7 : cflag |= termios . CS7 elif self . _bytesize == 6 : cflag |= termios . CS6 elif self . _bytesize == 5 : cflag |= termios . CS5 else : raise ValueError ( 'Invalid char len: {!r}' . format ( self . _bytesize ) ) # setup stop bits if self . _stopbits == serial . STOPBITS_ONE : cflag &= ~ ( termios . CSTOPB ) elif self . _stopbits == serial . STOPBITS_ONE_POINT_FIVE : cflag |= ( termios . CSTOPB ) # XXX same as TWO.. there is no POSIX support for 1.5 elif self . _stopbits == serial . STOPBITS_TWO : cflag |= ( termios . CSTOPB ) else : raise ValueError ( 'Invalid stop bit specification: {!r}' . format ( self . _stopbits ) ) # setup parity iflag &= ~ ( termios . INPCK | termios . ISTRIP ) if self . _parity == serial . PARITY_NONE : cflag &= ~ ( termios . PARENB | termios . PARODD | CMSPAR ) elif self . _parity == serial . PARITY_EVEN : cflag &= ~ ( termios . PARODD | CMSPAR ) cflag |= ( termios . PARENB ) elif self . _parity == serial . PARITY_ODD : cflag &= ~ CMSPAR cflag |= ( termios . PARENB | termios . PARODD ) elif self . _parity == serial . PARITY_MARK and CMSPAR : cflag |= ( termios . PARENB | CMSPAR | termios . PARODD ) elif self . _parity == serial . PARITY_SPACE and CMSPAR : cflag |= ( termios . PARENB | CMSPAR ) cflag &= ~ ( termios . PARODD ) else : raise ValueError ( 'Invalid parity: {!r}' . format ( self . _parity ) ) # setup flow control # xonxoff if hasattr ( termios , 'IXANY' ) : if self . _xonxoff : iflag |= ( termios . IXON | termios . IXOFF ) # |termios.IXANY) else : iflag &= ~ ( termios . IXON | termios . IXOFF | termios . IXANY ) else : if self . _xonxoff : iflag |= ( termios . IXON | termios . IXOFF ) else : iflag &= ~ ( termios . IXON | termios . IXOFF ) # rtscts if hasattr ( termios , 'CRTSCTS' ) : if self . _rtscts : cflag |= ( termios . CRTSCTS ) else : cflag &= ~ ( termios . CRTSCTS ) elif hasattr ( termios , 'CNEW_RTSCTS' ) : # try it with alternate constant name if self . _rtscts : cflag |= ( termios . CNEW_RTSCTS ) else : cflag &= ~ ( termios . CNEW_RTSCTS ) # XXX should there be a warning if setting up rtscts (and xonxoff etc) fails?? # buffer # vmin "minimal number of characters to be read. 0 for non blocking" if vmin < 0 or vmin > 255 : raise ValueError ( 'Invalid vmin: {!r}' . format ( vmin ) ) cc [ termios . VMIN ] = vmin # vtime if vtime < 0 or vtime > 255 : raise ValueError ( 'Invalid vtime: {!r}' . format ( vtime ) ) cc [ termios . VTIME ] = vtime # activate settings if force_update or [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] != orig_attr : termios . tcsetattr ( self . fd , termios . TCSANOW , [ iflag , oflag , cflag , lflag , ispeed , ospeed , cc ] ) # apply custom baud rate, if any if custom_baud is not None : self . _set_special_baudrate ( custom_baud ) if self . _rs485_mode is not None : self . _set_rs485_mode ( self . _rs485_mode )