📄 ch41_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 41] 41.2 stty and All That Stuff </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:51:49Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch41_01.htm"TITLE="41. Terminal and Serial Line Settings"><LINKREL="prev"HREF="ch41_01.htm"TITLE="41.1 Delving a Little Deeper "><LINKREL="next"HREF="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch41_01.htm"TITLE="41.1 Delving a Little Deeper "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 41.1 Delving a Little Deeper "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 41<BR>Terminal and Serial Line Settings</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch41_03.htm"TITLE="41.3 Find Out Terminal Settings with stty "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 41.3 Find Out Terminal Settings with stty "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1140">41.2 stty and All That Stuff </A></H2><PCLASS="para">[...all that <ICLASS="filename">useful</I> stuff!This article has a lot of good background for understanding howcommunications works between the UNIX host and your terminal or window.Chris has been in the business since way back; there's lots ofinteresting history in here, too.You might want a copy of your system's <ICLASS="filename">stty</I> manual page close bywhile you read this article. -JP]</P><DIVCLASS="msgset"><DIVCLASS="msgentry"><DIVCLASS="msg"><DIVCLASS="msgtext"><PCLASS="para"><BCLASS="msgentry.role">Q:</B> What is <EMCLASS="emphasis">stty</EM> all about? Why does it have so many options?</P></DIV></DIV></DIV><DIVCLASS="msgentry"><DIVCLASS="msg"><DIVCLASS="msgtext"><PCLASS="para"><BCLASS="msgentry.role">A:</B> Serial ports - indeed, computer communications in general - are atangled and complicated area. The demands made for serialport communication, and hence the support for it in UNIX systems, begansimply, but then grew in raging, uncontrolled bursts.</P></DIV></DIV></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1140-SECT-1.1">41.2.1 How We Made It This Far (Back?) </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45390"></A><ACLASS="indexterm"NAME="AUTOID-45392"></A><ACLASS="indexterm"NAME="AUTOID-45394"></A><ACLASS="indexterm"NAME="AUTOID-45396"></A><ACLASS="indexterm"NAME="AUTOID-45398"></A>Originally, UNIX ran on a small machine that talked only to<EMCLASS="emphasis">teletypes</EM>, or <EMCLASS="emphasis">ttys</EM> for short. The UNIX kernel had tocollect up input lines, allowing minor corrections - erasingthe previous character and killing (erasing wholly) the inputline - and translating a few "special" characters for controllingprograms. Teletypes were printers, incapable of erasing, sothe erase and kill characters were just ordinary printingcharacters, namely <CODECLASS="literal">#</CODE> and <CODECLASS="literal">@</CODE>. The original special characterswere CTRL-d (for end-of-file), DEL (to interrupt), and CTRL-\ (toquit). The kernel also mapped input RETURN codes to the newlinecharacter, so that users could push the big RETURN key, on teletypesthat had those.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45405"></A><ACLASS="indexterm"NAME="AUTOID-45408"></A><ACLASS="indexterm"NAME="AUTOID-45410"></A><ACLASS="indexterm"NAME="AUTOID-45413"></A><ACLASS="indexterm"NAME="AUTOID-45415"></A>These teletypes had some peculiarities. In particular, they used amoving print head (or <EMCLASS="emphasis">carriage</EM>), and this print head took anoticeable amount of time to return from the right margin to the left. If sent continuous printing text, a teletype could smearcharacters all over the paper during a carriage return.[1]The UNIXkernel therefore had to allow for a delay after a carriage return. Atthe same time, the kernel did "output processing" by changingnewlines to the teletype's carriage return and linefeed codes, ifnecessary.[2]A few teletypes allowed only uppercase characters, andUNIX grew support for these as well. UNIX did get away withoutsomething common to other operating systems, however: UNIX systemsassumed that all teletypes were "full duplex" and used "remoteecho." This meant, in essence, that both the teletype and the UNIXsystem could send to each other at the same time; and the teletypewould not print what you typed until told to do so by the UNIX host.[3]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] This is an exaggeration. Printing during a carriage return wasoccasionally used as a diagnostic for checking the motor speed. Thecharacter printed during the return was supposed to appear exactlyhalfway along the line.</P><PCLASS="para">[2] Some teletypes really processed a newline as a "new line," i.e., acarriage return and linefeed, but most left this up to the hostcomputer.</P><PCLASS="para">[3] <ACLASS="indexterm"NAME="AUTOID-45425"></A><ACLASS="indexterm"NAME="AUTOID-45427"></A>Full duplex/remote echo and half duplex/local echo tended to gotogether. In particular, a half duplex system - which was not thesame as a simplex system - had to have local echo to avoid beingannoying to use. Fortunately, this is irrelevant today. The conceptof "duplex" has fallen by the wayside, and everything is fullduplex, or at least simulates it internally.</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45430"></A>UNIX also had to provide a way for special applications, such as<SPANCLASS="link">UUCP (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,to get input characters without any processing. This was the so-calledraw mode. The kernel service was all-or-nothing: in raw mode,every input and output character was left alone, and passed directly - and immediately - to the application. In "cooked" mode, thekernel did input and output translations and delays.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45434"></A><ACLASS="indexterm"NAME="AUTOID-45436"></A><ACLASS="indexterm"NAME="AUTOID-45438"></A><ACLASS="indexterm"NAME="AUTOID-45440"></A>Along with the ability to set raw or cooked mode, the kernel allowedchanging each of the<SPANCLASS="link">special characters (<ACLASS="linkend"HREF="ch05_09.htm"TITLE="Setting Your Erase, Kill, and Interrupt Characters ">5.9</A>)</SPAN>and allowed control of some ofthe simpler aspects of the serial port interface, such as parity andbaud rate.[4]The baud rate, perhaps better called the bit rate, of theoriginal teletype was 110 bits per second (bps), or 11 characters persecond. (The machines really did print exactly 11 times each second,with one possibility being quietly to print nothing.) Early computermodems ran at 110 and 300 baud, and there were a standard set of serialport speeds: 50, 75, 110, 134.5, 150, 200, 300, 600, 1200, 1800, 2400,4800, and even 9600 bps, which was considered terribly fast. UNIXsystems used serial cards with two additional "external control"<ACLASS="indexterm"NAME="AUTOID-45446"></A>rates labeled A and B; these became <EMCLASS="emphasis">exta</EM> and <EMCLASS="emphasis">extb</EM>. Some UNIXsystems still support exactly (and only) these rates, and tie <EMCLASS="emphasis">exta</EM>to 19200 bps and <EMCLASS="emphasis">extb</EM> to 38400 bps.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[4] Parity is used for error checking. Parity is simply the numberof "1" bits. If you have the value 1001001, and even parity, theparity bit should be 1, because 1001001 has three 1 bits - an oddnumber - and adding another 1 makes this even. If the parity bitfails to match, at least one bit is wrong. It could, of course, bethe parity bit itself. Moreover, with a <EMCLASS="emphasis">tty</EM> port, there may not beanything you can do to fix the error - most UNIX kernels just dropthe bad input character - but the check is available.</P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-45453"></A><ACLASS="indexterm"NAME="AUTOID-45455"></A>Eventually, teletype printers began to be displaced. First there cameso-called glass ttys-CRT displays that tried to act just like ateletype - and then smarter terminals, ones that could (gasp) move acursor around the screen, and edit the display in place. These usedspecial control and escape codes to do the editing. They also providedthe opportunity to write full-screen editors. UNIX had to evolve toadapt to these new constraints. Unfortunately, by this time there weretwo main branches of UNIX. One would eventually become 4BSD, orBerkeley UNIX; the other was to become System V.</P><PCLASS="para">The goals for both systems were similar, and thus both wound up withcomparable approaches. Berkeley UNIX, however, attempted both toretain backwards compatibility and to provide a nice user interface,while the original System V system discarded compatibility in favor of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -