Further docs: man stty W. Richard Stewens Advanced Programming in the UNIX Environment Addison-Wesley professional computing series https://www.gnu.org/software/libc/manual/html_node/Low_002dLevel-Terminal-Interface.html https://man.freebsd.org/cgi/man.cgi?query=termios //////// Serial port drivers are ruled by the termios, which generally looks like: struct termios { tcflag_t c_iflag; /* input flags */ tcflag_t c_oflag; /* output flags */ tcflag_t c_cflag; /* control flags */ tcflag_t c_lflag; /* local flags */ cc_t c_cc[NCCS]; /* control characters */ } //////// input flags, c_iflag: BRKINT generate SIGINT on BREAK ICRNL map CR to NL on input IGNBRK ignore BRAEK condition IGNCR ignore CR IGNPAR ignore characters with parity errors IMAXBEL ring bell on input queue full INLCR map NL to CR on input INPCK enable input parity checking ISTRIP strip eighth bit off on input characters IUCLC map uppercase to lowercase on input IUTF8 assume input characters are UTF-8 encoded IXANY enable any characters to restart output IXOFF enable start/stop input flow control IXON enable start/stop output flow control PARMRK mark parity errors //////// output flags, c_oflag: BSDLY backspace dalay mask CRDLY CR delay mask FFDLY form feed delay mask NLDLY NL delay mask OCRNL map CR to nL on output OFDEL fill is DEL, else NUL OFILL use fill characters for delay OLCUC map lowercase to uppercase on output ONLCR map NL to CR-NL on output ONLRET NL performs CR function ONOCR no CR output at column 0 ONOEOT discard EOT (^D) on output OPOST perform output processing OXTABS expand tabs to spaces TABDLY horizontal tab delay mask VTDLY vertical tab delay mask For each xxDLY mask there are a few delay styles to choose from. BSDLY : BS0 BS1 CRDLY : CR0 CR1 CR2 CR3 FFDLY : FF0 FF1 NLDLY : NL0 NL1 TABDLY: TAB0 TAB1 TAB2 TAB3 VTDLY : VT0 VT1 All thoose deley things was for old output equipment like printers, teletypes, ... //////// control flags, c_cflag: CCTS_OFLOW CTS flow control of output CIGNORE ignore control flags CIBAUD mask for input baud rate bits CLOCAL ignore modem status lines CMSPAR mark or space (stick) parity CREAD enable CRTS_IFLOW RTS flow ontrol of input CRTSCTS RTS/CTS flow control CSIZE character size mask CSTOPB send two stop bits, else one HUPCL hangup on last close MDMBUF flow control output via carrier detect (CD) LOBLK ?? PARENB parity enable PARODD odd parity, else even CBAUD mask for baud rate bits CBAUDEX mask for baud rate bits extensions //////// local flags, c_lflag: ALTWERASE use alternate WERASE algorithm CRTERASE echo erase characters as backspace-space-backspace DEFECHO ?? ECHO enable echo ECHOCTL echo control chars as ^ ECHOE visually erase chars ECHOK echo kill ECHOKE visual erase for kill ECHONL echo NL ECHOPRT visual erase mode for hardcopy FLUSHO output being flushed ICANON canonical input IEXTEN enable extended (non-POSIX) input char processing ISIG enable terminal-generated signals NOFLSH disable flush after interrupt or quit NOKERNINFO no kernel output from STATUS PENDIN retype pending input TOSTOP send SiGTTOU for background output XCASE canonical upper/lower presentation //////// special input characters, c_cc: chr description c_cc[..] enabled by typ. CR carriage return - ICANON \r DISCARD discard output VDISCARD IEXTEN ^O DSUSP delayed suspend VDSUSP ISIG ^Y SIGTSTP EOF end-of-file VEOF ICANON ^D EOL end-of-line VEOL ICANON EOL2 alt. end-of-line VEOL2 ICANON ERASE backspace 1 char. VERASE ICANON ^H INTR interrupt signal VINTR ISIG ^? ^C SIGINT KILL erase line VKILL ICANON ^U LNEXT literal next VLNEXT IEXTEN ^V NL linefeed - ICANON \n QUIT quit signal VQUIT ISIG ^\ SIGQUIT REPRINT reprint all input VREPRINT ICANON ^R START resume output VSTART IXON/IXOFF ^Q STATUS status request VSTATUS ICANON ^T STOP stop output VSTOP IXON/IXOFF ^S SUSP suspend signal VSUSP ISIG ^Z SIGTSTP WERASE backspace 1 word VWERASE ICANON ^W all flags uses c_lflag, except IXON/IXOFF which uses c_iflag. //////////////////////////////////////// *** comments to termios flags: ** c_cflag flag constants: * CLOCAL Ignore modem control lines. if bit cleared: DCD going from high to low generates a SIGHUP. Use this and HUPCL if you have a modem connected, and set the modem to hangup or reset when DTR goes low. Saves you on the phone bill. Also useful for a computer to computer or terminal connection if you want the person on the other end to be logged out if the terminal is switched off (or when the serial cable is unplugged). * CREAD Enable receiver. practically always set * CRTSCTS (not in POSIX) Enable RTS/CTS (hardware) flow control. practically always set, unless the other end of the cable doesn't have it, e.g. RS232-RS485 converters do not normally have RTS/CTS flow control pp->c_cflag &= ~(CRTSCTS); // no hw flow control, RTS and CTS pins always high pp->c_cflag |= (CRTSCTS); // RTS and CTS pins handle flow control, so hw buffers won't overflow * CSIZE Character size mask. Values are CS5, CS6, CS7, or CS8. used when setting byte size: pp->c_cflag &= ~(CSIZE); // clear bits first pp->c_cflag |= (CS5); // then set byte size * CSTOPB Set two stop bits, rather than one. pp->c_cflag &= ~(CSTOPB); // one stop bit, or pp->c_cflag |= (CSTOPB); // two stop bits * HUPCL Lower modem control lines after last process closes the device (hang up). pp->c_cflag &= ~(HUPCL); // DTR is always high pp->c_cflag |= (HUPCL); // DTR is high when device is open()'ed, else low * PARENB Enable parity generation on output and parity checking for input. Parity bit is a useless check, disable unless other end require it. pp->c_cflag &= ~(PARENB); // parity is diabled pp->c_cflag |= (PARENB); // parity is enabled There is an INPCK flag also, don't know how it relates to PARENB. * PARODD Parity for input and output is odd. Parity can be: disabled, even, odd, mark (always 1) or space (always 0) I don't know how to set mark or space parity i unix. pp->c_cflag &= ~(PARODD); // if parity is anbled, use even parity pp->c_cflag |= (PARODD); // if parity is anbled, use odd parity ** c_iflag flag constants: * IGNBRK Ignore BREAK condition on input. if you is a getty program, you can unset this so user at terminal can press BREAK and get another baudrate otherwise, set this bit * BRKINT If IGNBRK is set, a BREAK is ignored. If it is not set but BRKINT is set, then a BREAK causes the input and output queues to be flushed, and if the terminal is the controlling terminal of a foreground process group, it will cause a SIGINT to be sent to this foreground process group. When neither IGNBRK nor BRKINT are set, a BREAK reads as a NUL character, except when PARMRK is set, in which case it reads as the sequence \377 \0 \0. just set IGNBRK, unless you really need the break * IGNPAR Ignore framing errors and parity errors. parity bits are useless, set this flag (if you want error detection add a lrc/crc to the end of data) * PARMRK If IGNPAR is not set, prefix a character with a parity error or framing error with \377 \0. If neither IGNPAR nor PARMRK is set, read a character with a parity error or framing error as \0. parity bits are useless, ignore this one * INPCK Enable input parity checking. parity bits are useless, clear this flag * ISTRIP Strip off eighth bit. Who wants anything other than 8bit chars today I don't know how this one interacts with parity bits and byte size * INLCR Translate NL to CR on input. usually not set * IGNCR Ignore carriage return on input. usually not set * ICRNL Translate carriage return to newline on input (unless IGNCR is set). usually set when user at other end (enter key sends a CR, and unix expects NL as line terminator) * IUCLC (not in POSIX) Map uppercase characters to lowercase on input. Don't set this unless you have an ancient terminal which can't send lowercase chars. * IXON Enable XON/XOFF flow control on output. * IXANY (not in POSIX.1; XSI) Enable any character to restart output. * IXOFF Enable XON/XOFF flow control on input. If you have to use XON/XOFF (CRTSCTS is better), enable IXON, if other end is a user at a terminal, that is suficcient, you could possible set IXANY also if other end is a computer, enable IXOFF also and clear IXANY Consider that you can't then send ^Q or ^S to e.g. emacs with xon/xoff flow control. * IMAXBEL (not in POSIX) Ring bell when input queue is full. Linux does not implement this bit, and acts as if it is always set. use this if you care ** c_oflag flag constants defined in POSIX.1: * OPOST Enable implementation-defined output processing. * ONLCR (XSI) Map NL to CR-NL on output. usually set for user at terminal or printer to communicate with some serial hw device, clear theese * OLCUC (not in POSIX) Map lowercase characters to uppercase on output. * OCRNL Map CR to NL on output. * ONOCR Don't output CR at column 0. * ONLRET Don't output CR. * OFILL Send fill characters for a delay, rather than using a timed delay. * OFDEL (not in POSIX) Fill character is ASCII DEL (0177). If unset, fill character is ASCII NUL. don't set theese bits * NLDLY Newline delay mask. Values are NL0 and NL1. * CRDLY Carriage return delay mask. Values are CR0, CR1, CR2, or CR3. * TABDLY Horizontal tab delay mask. Values are TAB0, TAB1, TAB2, TAB3 (or XTABS). A value of TAB3 (that is XTABS) expands tabs to spaces (with tab stops every eight columns). * BSDLY Backspace delay mask. Values are BS0 or BS1. (Has never been implemented.) * VTDLY Vertical tab delay mask. Values are VT0 or VT1. * FFDLY Form feed delay mask. Values are FF0 or FF1. If you have a slow device (e.g. a printer) connected to the other end, you can insert delays with this. First clear the xxDLY bits, and then set xx# bit, e.g. clear CRDLY and then set CR2. To use this, you have to test wheter the delays do what you want. Usually the xx0 (no delay) delay is used. ** c_lflag flag constants: for user at terminal, set theese: * ISIG When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal. * ICANON Enable canonical mode. This enables the special characters EOF, EOL, EOL2, ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines. * ECHO Echo input characters. * ECHOE If ICANON is also set, the ERASE character erases the preceding input character, and WERASE erases the preceding word. * ECHOK If ICANON is also set, the KILL character erases the current line. don't use theese: * XCASE (not in POSIX; not supported under Linux) If ICANON is also set, terminal is uppercase only. Input is converted to lowercase, except for characters preceded by \. On output, uppercase characters are preceded by \ and lowercase characters are converted to uppercase. * ECHONL If ICANON is also set, echo the NL character even if ECHO is not set. * ECHOCTL (not in POSIX) If ECHO is also set, ASCII control signals other than TAB, NL, START, and STOP are echoed as ^X, where X is the character with ASCII code 0x40 greater than the control signal. For example, character 0x08 (BS) is echoed as ^H. * ECHOPRT (not in POSIX) If ICANON and IECHO are also set, characters are printed as they are being erased. don't know about theese * ECHOKE (not in POSIX) If ICANON is also set, KILL is echoed by erasing each character on the line, as specified by ECHOE and ECHOPRT. * DEFECHO (not in POSIX) Echo only when a process is reading. * FLUSHO (not in POSIX; not supported under Linux) Output is being flushed. This flag is toggled by typing the DISCARD character. * NOFLSH Disable flushing the input and output queues when generating the SIGINT, SIGQUIT and SIGSUSP signals. * TOSTOP Send the SIGTTOU signal to the process group of a background process which tries to write to its controlling terminal. * PENDIN (not in POSIX; not supported under Linux) All characters in the input queue are reprinted when the next character is read. (bash handles typeahead this way.) * IEXTEN Enable implementation-defined input processing. This flag, as well as ICANON must be enabled for the special characters EOL2, LNEXT, REPRINT, WERASE to be interpreted, and for the IUCLC flag to be effective. //////////////////////////////////////// ** some alternatives from man stty: cbreak same as -icanon evenp same as parenb -parodd cs7 -evenp same as -parenb cs8 lcase same as xcase iuclc olcuc litout same as -parenb -istrip -opost cs8 -litout same as parenb istrip opost cs7 nl same as -icrnl -onlcr -nl same as icrnl -inlcr -igncr onlcr -ocrnl -onlret oddp same as parenb parodd cs7 -oddp same as -parenb cs8 [-]parity same as [-]evenp pass8 same as -parenb -istrip cs8 -pass8 same as parenb istrip cs7 default with baud 9600, Linux console (uses 38400), for an xterm add "-hupcl iexten" linux console: speed 38400 baud; rows 50; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke xterm: speed 9600 baud; rows 58; columns 156; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke //////////////////////////////////////// The RS232 Blues: Each pin is either a receiver or a transmitter or a ground pin. A transmitter (sender) outputs positive voltage (called SPACE, +25V to +5V) or negative (called MARK, -5V to -25V). A receiver treats incoming +25V to +3V as SPACE and -3V to -25V as MARK. To find out which pin is transmitters/receivers: transmitter pins have a voltage, + or -, commonly 12, 5, or 3V receiver pins: 0 volt (From what I have heard: -volt is called MARK since an ancient intrument made a line (a "mark") on a paper strip when negative voltage was applied. And SPACE since positive voltage made the pen to be lifted from the paper, thus creating a "space" in the line. This makes me think of JvAnka. He has some sort of instrument (inside a glass bulb), on a piedestal, out of which it comes a paper strip. Could it be such an instrument? ) There is data pins (TD and RD) and control pins. For data pins, TTL lvl function rs232 level MARK == 1 5V Deasserted −15 to −3 V SPACE == 0 0V Asserted +3 to +15 V For control pins: SPACE == 1, ON, or high else (e.g. no cable connected) 0, OFF, or low There is DTE (data terminal equipment) and DCE (data communication equipment). The difference: a DTE's TD is a transmitter and RD a receiver a DCE's RD is a transmitter and TD a receiver So look on the TD pin, if it has voltage it is a DTE, if instead RD has the voltage, it is an DCE. That's all to it. The golden rule of RS232 wiring: always wire a transmitter to a receiver. E.g. DTE TD <-> DCE TD, DTE RD <-> DCE TD, DTE TD <-> DTE RD, DCE TD <-> DCE RD If you use hw flow control (set CRTSCTS): Pin DTE DCE name RTS output input Request To Send CTS input output Clear To Send If you use modem control (clear CLOCAL, set HUPCL): Pin DTE DCE name DSR input output Data Set Ready DCD input output Data Carrier Detect DTR output input Data Terminal Ready References: a, Ed Ravin, Tim O'Reilly, Dale Dougherty, Grace Todino Using & Managing uucp O'Reilly & Assosiates, Inc (chapter 9, the physical connection) b, Evi Nemeth, Garth Snyder, Scott Seebass, Trent R. Hein Unix System Administration Handbook Prentice Hall (chapter 8, serial devices) c, David Fiedler, Bruce H. Hunter UNIX System Administration Hayden Book Company (appendix B) *** RS232 pinout Table 8.1 from ref b: 1 FG Frame ground 2 TD Transmitted data 3 RD Received data 4 RTS Request to send 5 CTS Clear to send 6 DSR Data set ready 7 SG Signal ground 8 DCD Data carrier detect 9 - positive voltage 10 - negative voltage 11 - Not assigned 12 SDCD secondary DCD 13 SCTS secondary CTS 14 STD secondary TD 15 TC transmit clock 16 SRD secondary RD 17 RC receive clock 18 - Not assigned 19 SRTS secondary RTS 20 DTR Data terminal ready 21 SQ Signal quality detector 22 RI Ring indicator 23 DRS Data rate selector 24 SCTE Clock tramsmit external 25 BUSY Busy On the inside of Comtest 100DLX case: CCITT EIA pin DTE-DCE description 101 AA 1 <> Frame ground 102 AB 7 <> Signal ground 103 BA 2 -> Transmitted data 104 BB 3 <- Received data 105 CA 4 -> Request to send 106 CB 5 <- Clear to send 107 CC 6 <- Data set ready 108.1 CD 20 -> Connect data set to line 108.2 CD 20 -> Data terminal ready 109 CF 8 <- Data carrier detect 111 CH 23 -> Data rate select 113 DA 24 -> Transmitter clock 114 DB 15 <- Transmitter clock 115 DD 17 <- Receiver clock 118 SBA 14 -> Transmitted data ch 2 119 SBB 16 <- Received data ch 2 120 SCA 19 -> Request to send ch 2 121 SCB 13 <- Clear to send ch 2 122 SCF 12 <- Data carrier detect ch 2 125 CE 22 <- Calling indicator 140 ... 21 -> Remote loopback 141 ... 18 -> Local loopback 142 ... 25 <- Test indicator ... ... 9 .. Positive test voltage ... ... 10 .. Negative test voltage Output from the program statserial: Signal Pin Pin Direction Status Full Name (25) (9) (computer) Name ----- --- --- --------- ------ ----- FG 1 - - - Frame Ground TxD 2 3 out - Transmit Data RxD 3 2 in - Receive Data RTS 4 7 out 1 Request To Send CTS 5 8 in 1 Clear To Send DSR 6 6 in 1 Data Set Ready GND 7 5 - - Signal Ground DCD 8 1 in 0 Data Carrier Detect DTR 20 4 out 1 Data Terminal Ready RI 22 9 in 0 Ring Indicator Standard COM-header/10pole flat cable, used on pc motherboards (same as 9-pin above): DCD 1 2 RxD TxD 3 4 DTR GND 5 6 DSR RTS 7 8 CTS RI 9 x ---