opening a serial port at a given baudrate. How?

David Arnold arnold at dstc.edu.au
Tue Apr 4 23:19:31 EDT 2000


-->"Jim" == Jim Richardson <warlock at eskimo.com> writes:

  Jim> I have been using open('/dev/ttyS0','r+') but need to control
  Jim> baud rate. fcntl.ioctl() will do this?

as someone else pointed out (sorry, lost the mail), the termios module
is the go for setting baud rates, parity, etc.

some old code:  not sure if it works, but might be useful ...


	#-- open serial port to device
	self.ir_file = open(str_port, "w+")
	ir_fh = self.ir_file.fileno()

	#-- get current settings
	[c_iflag, c_oflag, c_cflag, c_lflag, ispeed, ospeed, ccs] = \
		  termios.tcgetattr(ir_fh)

	#-- input flags
	off = 0
	off = off | TERMIOS.IGNBRK    # don't ignore break
	off = off | TERMIOS.BRKINT    # no interrupt on break
	off = off | TERMIOS.IGNPAR    # don't throw away parity errors
	off = off | TERMIOS.PARMRK    # don't mark parity errors
	off = off | TERMIOS.ISTRIP    # don't strip 8bit
	off = off | TERMIOS.INLCR     # no nl->cr xlate
	off = off | TERMIOS.IGNCR     # don't ignore CR
	off = off | TERMIOS.ICRNL     # no cr->nl xlate
	off = off | TERMIOS.IXON      # no output flow control
	off = off | TERMIOS.IXOFF     # no input flow control
	on  = 0
        on  = on  | TERMIOS.INPCK     # disable parity checking
	c_iflag = (c_iflag & ~off) | on

	#-- output flags
	off = 0
	off = off | TERMIOS.OPOST     # no ouput processing
	off = off | TERMIOS.OLCUC     # no lower to upper xlate
	off = off | TERMIOS.OCRNL     # no cr->nl xlate
	off = off | TERMIOS.ONOCR     # cr at 0 ok
	off = off | TERMIOS.ONLRET    # nl does not imply cr
	off = off | TERMIOS.OFILL     # no fill chars generated
        on  = 0
	c_oflag = (c_oflag & ~off) | on

	#-- control flags (see p10, CE-IR2 manual)
	off = 0
	off = off | TERMIOS.CSIZE     # zero out the char-size bits
	off = off | TERMIOS.CSTOPB    # one stop bit
	off = off | TERMIOS.HUPCL     # don't hangup on close
	on  = 0
	on  = on  | TERMIOS.CS8       # 8 bit chars
	on  = on  | TERMIOS.CREAD     # allow receives
	on  = on  | TERMIOS.PARENB    # enable parity generation
	on  = on  | TERMIOS.PARODD    # use odd parity
	on  = on  | TERMIOS.CLOCAL    # ignore modem status lines
        c_cflag = (c_cflag & ~off) | on

	#-- local flags
	off = 0
	off = off | TERMIOS.ECHO      # no local echo of output
	off = off | TERMIOS.ISIG      # no signals from special chars
	off = off | TERMIOS.ICANON    # disable canonical processing
	off = off | TERMIOS.IEXTEN    # disable other special chars
	off = off | TERMIOS.NOFLSH    # no flush on signal
	off = off | TERMIOS.TOSTOP    # no stop for background output
        on  = 0
	c_lflag = (c_lflag & ~off) | on

	#-- set VTIME and VMIN both to zero (ie. poll and return what's there)
	ccs[TERMIOS.VMIN] = 0     # read a minimum of 0 chars
	ccs[TERMIOS.VTIME] = 0    # wait for 0/10 sec for data to arrive

	termios.tcsetattr(ir_fh, TERMIOS.TCSANOW, \
			  [c_iflag,c_oflag,c_cflag,c_lflag,9600,9600,ccs])





More information about the Python-list mailing list