Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Serial Programming/termio

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Introduction <termio.h> (note the missing 's') is the older System V terminal I/O API. It has been replaced by <termios.h> on modern systems. It is still in use on many systems, e.g. on embedded Unix systems or those based on Unix System V. Often modern systems still also provide the old API, too. All settings to a serial device are done via the ioctl(2) system call, as opposite to the special functions as provided by <termios.h>. Special Characters INTR (default Ctrl-C or ASCII ETX) QUIT (default Ctrl-\, or ASCII ES) ERASE (default backspace or ASCII BS) KILL (Ctrl-U, or ASCII NAK) EOF (Ctrl-D, or ASCII EOT) NL (ASCII LF) EOL (ASCII LF) STOP (Ctrl-S, or ASCII DC3) START (Ctrl-Q, or ASCII DC1) Setting and Getting Parameters via Termio Primary commands The main commands for controlling serial (terminal) I/O with termio all use an ioctl() call of the form ioctl(int fileDescriptor, int termioCommand, struct termio *arg); The following commands use this ioctl() form: TCGETA Get current parameters #include <termio.h> : . struct termio params; ioctl(fileDescriptor, TCGETA, &params); TCSETA Set parameters immediately TCSETAW Set parameters when output is empty (waits with change until all buffered data has been sent). TCSETAF Wait until output is empty, then flush input, then set parameters. The struct termio argument as used in all the above ioctl(2) command looks as it follows: struct termio /* * Classic struct termio. More modern Unix versions * contain additional information. Unix versions who * support termio and termios often use the same * structure for termio and termios, so termio * contains the full termios data on this systems. */ #define NCC 8 struct termio { int c_iflag; /* input modes */ int c_oflag; /* output modes */ int c_cflag; /* control modes */ int c_lflag; /* local modes */ char c_cc[NCC]; /* control chars */ }; c_cc array in struct termio c_iflag input mode flag in struct termio IGNBRK BRKINT IGNPAR PARMRK INPCK ISTRIP INLCR IGNCR ICRNL IUCLC IXON IXANY IXOFF c_oflag output mode flag in struct termio OPOST OLCUC ONLCR OCRNL ONOCR ONLRET OFILL OFDEL NLDLY NL0 NL1 CRDLY CR0 CR1 CR2 CR3 TABDLY TAB0 TAB1 TAB2 TAB3 BSDLY BS0 BS1 VTDLY VT0 VT1 FFDLY FF0 FF1 c_cflag in struct termio B0, B50, B75, B110, B134, B150, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400 This are to select baud rate CSIZE CS5, CS6, CS7, CS8 These are for setting data bit length, between 5..8 CSTOPB CREAD PARENB This is for enabling parity check PARODD This is to select odd parity HUPCL CLOCAL c_lflag in struct termio ISIG ICANON XCASE ECHO ECHOE ECHOK ECHONL NOFLSH Additional Commands The additional commands use the following form of ioctl() calls: ioctl(int fileDescriptor, int termioCommand, int arg); The following termio commands use this form: TCSBRK Wait until output is empty (drain). Optionally, a break can be sent when this happens. In fact, this is the most common application #include <termio.h> /* Convenience macros for the waitBreak cmd argument */ #define WAIT_N_BREAK 0 #define WAIT_ONLY 1 /* * Function for waiting until output is empty and opt. sending a break */ tcWaitBreak(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCSBRK, cmd); } /* * Send break when output is empty */ tcBreak(int fileDescriptor) { tcWaitBreak(fileDescriptor, WAIT_N_BREAK); } TCXONC Start or stop the output. #include <termio.h> /* Convenience macros for the start/stop cmd argument */ #define STOP 0 #define START 1 /* * Function for starting /stopping output */ tcStartStop(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCXONC, cmd); } /* * stop output */ tcStop(int fileDescriptor) { tcStartStop(fileDescriptor, STOP); } /* * start output */ tcStart(int fileDescriptor) { tcStartStop(fileDescriptor, START); } TCFLSH Flush the input, output, or both queues. #include <termio.h> /* Convenience macros for the flush cmd argument */ #define FLUSH_IN 0 #define FLUSH_OUT 1 #define FLUSH_BOTH 2 /* * Function for flushing a terminal/serial device */ tcFlush(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCFLSH, cmd); } S