def set_special_baudrate(port, baudrate): import array buf = array.array('i', [0] * 32) # get serial_struct FCNTL.ioctl(port.fd, TERMIOS.TIOCGSERIAL, buf) # set custom divisor buf[6] = buf[7] / baudrate # update flags buf[4] &= ~ASYNC_SPD_MASK buf[4] |= ASYNC_SPD_CUST # set serial_struct try: res = FCNTL.ioctl(port.fd, TERMIOS.TIOCSSERIAL, buf) except IOError: raise ValueError('Failed to set custom baud rate: %r' % baudrate)
def set_special_baudrate(port, baudrate): # right size is 44 on x86_64, allow for some growth import array buf = array.array('i', [0] * 64) try: # get serial_struct FCNTL.ioctl(port.fd, TCGETS2, buf) # set custom speed buf[2] &= ~TERMIOS.CBAUD buf[2] |= BOTHER buf[9] = buf[10] = baudrate # set serial_struct res = FCNTL.ioctl(port.fd, TCSETS2, buf) except IOError, e: raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
cc[TERMIOS.VMIN] = vmin # vtime if vtime < 0 or vtime > 255: raise ValueError('Invalid vtime: %r' % vtime) cc[TERMIOS.VTIME] = vtime # activate settings 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: import array buf = array.array('i', [0] * 32) # get serial_struct FCNTL.ioctl(self.fd, TERMIOS.TIOCGSERIAL, buf) # set custom divisor buf[6] = buf[7] / custom_baud # update flags buf[4] &= ~ASYNC_SPD_MASK buf[4] |= ASYNC_SPD_CUST # set serial_struct try: res = FCNTL.ioctl(self.fd, TERMIOS.TIOCSSERIAL, buf) except IOError: raise ValueError('Failed to set custom baud rate: %r' % self._baudrate)
raise ValueError('Invalid vmin: %r ' % vmin) cc[TERMIOS.VMIN] = vmin #vtime if vtime < 0 or vtime > 255: raise ValueError('Invalid vtime: %r' % vtime) cc[TERMIOS.VTIME] = vtime #activate settings 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: import array buf = array.array('i', [0] * 32) # get serial_struct FCNTL.ioctl(self.fd, TERMIOS.TIOCGSERIAL, buf) # set custom divisor buf[6] = buf[7] / custom_baud # update flags buf[4] &= ~ASYNC_SPD_MASK buf[4] |= ASYNC_SPD_CUST # set serial_struct try: res = FCNTL.ioctl(self.fd, TERMIOS.TIOCSSERIAL, buf) except IOError: raise ValueError('Failed to set custom baud rate: %r' % self._baudrate) def close(self):
def _reconfigurePort(self): """Set communication parameters on opened port.""" if self.fd is None: raise SerialException("Can only operate on a valid port handle") custom_baud = None vmin = vtime = 0 # timeout is done via select if self._interCharTimeout is not None: vmin = 1 vtime = int(self._interCharTimeout * 10) try: iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr( self.fd) # if a port is nonexistent but has a /dev file, it'll fail here except termios.error as msg: raise SerialException("Could not configure port: %s" % 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) iflag &= ~(TERMIOS.INLCR | TERMIOS.IGNCR | TERMIOS.ICRNL | TERMIOS.IGNBRK) if hasattr(TERMIOS, 'IUCLC'): iflag &= ~TERMIOS.IUCLC if hasattr(TERMIOS, 'PARMRK'): iflag &= ~TERMIOS.PARMRK # setup baudrate try: ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate)) except AttributeError: try: ispeed = ospeed = baudrate_constants[self._baudrate] except KeyError: #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) # may need custom baud rate, it isnt in our list. ispeed = ospeed = getattr(TERMIOS, 'B38400') custom_baud = int(self._baudrate) # store for later # 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' % self._bytesize) # setup stopbits if self._stopbits == STOPBITS_ONE: cflag &= ~(TERMIOS.CSTOPB) elif self._stopbits == STOPBITS_TWO: cflag |= (TERMIOS.CSTOPB) else: raise ValueError('Invalid stopit specification: %r' % self._stopbits) # setup parity iflag &= ~(TERMIOS.INPCK | TERMIOS.ISTRIP) if self._parity == PARITY_NONE: cflag &= ~(TERMIOS.PARENB | TERMIOS.PARODD) elif self._parity == PARITY_EVEN: cflag &= ~(TERMIOS.PARODD) cflag |= (TERMIOS.PARENB) elif self._parity == PARITY_ODD: cflag |= (TERMIOS.PARENB | TERMIOS.PARODD) else: raise ValueError('Invalid parity: %r' % 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) # try it with alternate constant name elif hasattr(TERMIOS, 'CNEW_RTSCTS'): 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. = for non blocking" if vmin < 0 or vmin > 255: raise ValueError('Invalid vmin: %r ' % vmin) cc[TERMIOS.VMIN] = vmin # vtime if vtime < 0 or vtime > 255: raise ValueError('Invalid vtime: %r' % vtime) cc[TERMIOS.VTIME] = vtime # activate settings 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: import array buf = array.array('i', [0] * 32) # get serial_struct FCNTL.ioctl(self.fd, TERMIOS.TIOCGSERIAL, buf) # set custom divisor buf[6] = buf[7] / custom_baud # update flags buf[4] &= ~ASYNC_SPD_MASK buf[4] |= ASYNC_SPD_CUST # set serial_struct try: res = FCNTL.ioctl(self.fd, TERMIOS.TIOCSSERIAL, buf) except IOError: raise ValueError('Failed to set custom baud rate: %r' % self._baudrate)
def _reconfigurePort(self): """Set communication parameters on opened port.""" if self.fd is None: raise SerialException("Can only operate on a valid port handle") custom_baud = None vmin = vtime = 0 # timeout is done via select if self._interCharTimeout is not None: vmin = 1 vtime = int(self._interCharTimeout * 10) try: iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr( self.fd) # if a port is nonexistent but has a /dev file, it'll fail here except termios.error as msg: raise SerialException("Could not configure port: %s" % 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) iflag &= ~( TERMIOS.INLCR | TERMIOS.IGNCR | TERMIOS.ICRNL | TERMIOS.IGNBRK) if hasattr(TERMIOS, 'IUCLC'): iflag &= ~TERMIOS.IUCLC if hasattr(TERMIOS, 'PARMRK'): iflag &= ~TERMIOS.PARMRK # setup baudrate try: ispeed = ospeed = getattr(TERMIOS, 'B%s' % (self._baudrate)) except AttributeError: try: ispeed = ospeed = baudrate_constants[self._baudrate] except KeyError: #~ raise ValueError('Invalid baud rate: %r' % self._baudrate) # may need custom baud rate, it isnt in our list. ispeed = ospeed = getattr(TERMIOS, 'B38400') custom_baud = int(self._baudrate) # store for later # 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' % self._bytesize) # setup stopbits if self._stopbits == STOPBITS_ONE: cflag &= ~(TERMIOS.CSTOPB) elif self._stopbits == STOPBITS_TWO: cflag |= (TERMIOS.CSTOPB) else: raise ValueError( 'Invalid stopit specification: %r' % self._stopbits) # setup parity iflag &= ~(TERMIOS.INPCK | TERMIOS.ISTRIP) if self._parity == PARITY_NONE: cflag &= ~(TERMIOS.PARENB | TERMIOS.PARODD) elif self._parity == PARITY_EVEN: cflag &= ~(TERMIOS.PARODD) cflag |= (TERMIOS.PARENB) elif self._parity == PARITY_ODD: cflag |= (TERMIOS.PARENB | TERMIOS.PARODD) else: raise ValueError('Invalid parity: %r' % 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) # try it with alternate constant name elif hasattr(TERMIOS, 'CNEW_RTSCTS'): 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. = for non blocking" if vmin < 0 or vmin > 255: raise ValueError('Invalid vmin: %r ' % vmin) cc[TERMIOS.VMIN] = vmin # vtime if vtime < 0 or vtime > 255: raise ValueError('Invalid vtime: %r' % vtime) cc[TERMIOS.VTIME] = vtime # activate settings 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: import array buf = array.array('i', [0] * 32) # get serial_struct FCNTL.ioctl(self.fd, TERMIOS.TIOCGSERIAL, buf) # set custom divisor buf[6] = buf[7] / custom_baud # update flags buf[4] &= ~ASYNC_SPD_MASK buf[4] |= ASYNC_SPD_CUST # set serial_struct try: res = FCNTL.ioctl(self.fd, TERMIOS.TIOCSSERIAL, buf) except IOError: raise ValueError( 'Failed to set custom baud rate: %r' % self._baudrate)