http://dns.easysw.com/~mike/serial/serial.html#9_1
串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线Universal Serial Bus或者USB混淆)。大多数计算机包含两个基于RS232的串口。串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS- 232口。同时,串口通信协议也可以用于获取远程采集设备的数据。
串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另 一根线接收数据。它很简单并且能够实现远距离通信。比如IEEE488定义并行通行状态时,规定设备线总常不得超过20米,并且任意两个设备间的长度不得 超过2米;而对于串口而言,长度可达1200米。
典型地,串口用于ASCII码字符的传输。通信使用3根线完成:(1)地线,(2)发送,(3)接收。由于串口通信是异步的,端口能够在一根线上发 送数据同时在另一根线上接收数据。其他线用于握手,但是不是必须的。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通行的端口, 这些参数必须匹配:
a,波特率:这是一个衡量通信速度的参数。它表示每秒钟传送的bit的个数。例如300波特表示每秒钟发送300个bit。 当我们提到时钟周期时,我们就是指波特率例如如果协议需要4800波特率,那么时钟是4800Hz。这意味着串口通信在数据线上的采样率为4800Hz。 通常电话线的波特率为14400,28800和36600。波特率可以远远大于这些值,但是波特率和距离成反比。高波特率常常用于放置的很近的仪器间的通 信,典型的例子就是GPIB设备的通信。
b,数据位:这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据不会是8位的,标准 的值是5、7和8位。如何设置取决于你想传送的信息。比如,标准的ASCII码是0~127(7位)。扩展的ASCII码是0~255(8位)。如果数据 使用简单的文本(标准 ASCII码),那么每个数据包使用7位数据。每个包是指一个字节,包括开始/停止位,数据位和奇偶校验位。由于实际数据位取决于通信协议的选取,术语 “包”指任何通信的情况。
c,停止位:用于表示单个包的最后一位。典型的值为1,1.5和2位。由于数据是在传输线上定时的,并且每一个设备有 其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。适用于停止位的位数 越多,不同时钟同步的容忍程度越大,但是数据传输率同时也越慢。
d,奇偶校验位:在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高 和低。当然没有校验位也是可以的。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位。例如, 如果数据是011,那么对于偶校验,校验位为0,保证逻辑高的位数是偶数个。如果是奇校验,校验位位1,这样就有3个逻辑高位。高位和低位不真正的检查数 据,简单置位逻辑高或者逻辑低校验。这样使得接收设备能够知道一个位的状态,有机会判断是否有噪声干扰了通信或者是否传输和接收数据是否不同步
Signal Definitions
The RS-232 standard defines some 18 different signals for serial communications. Of these, only six are generally available in the UNIX environment.
RS-232标准定义了18个不同的信号类型,但是通常只有6个常用
GND - Logic Ground
Technically the logic ground is not a signal, but without it none of the other signals will operate. Basically, the logic ground acts as a reference voltage so that the electronics know which voltages are positive or negative.
GND 从技术上讲并不是一个信号,但是没有它别的信号却无法运作。基本上,GND扮演一个参考电压的角色,设备以此来判定电压的正负性。
TXD - Transmitted Data
The TXD signal carries data transmitted from your workstation to the computer or device on the other end (like a MODEM). A mark voltage is interpreted as a value of 1, while a space voltage is interpreted as a value of 0.
TXD 信号发送引脚,是由你发出的信号(可能发给猫,或者发给另一端的计算机),有电压表示1,没有电压表示0.
RXD - Received Data
The RXD signal carries data transmitted from the computer or device on the other end to your workstation. Like TXD, mark and space voltages are interpreted as 1 and 0, respectively.
RSD 信号接收引脚,是另一端发给你的信号。
DCD - Data Carrier Detect
The DCD signal is received from the computer or device on the other end of your serial cable. A space voltage on this signal line indicates that the computer or device is currently connected or on line. DCD is not always used or available.
DCD 接受来自另一端的信息,以确定对方是否在线和连接正常,这个引脚很少使用。
DTR - Data Terminal Ready
The DTR signal is generated by your workstation and tells the computer or device on the other end that you are ready (a space voltage) or not-ready (a mark voltage). DTR is usually enabled automatically whenever you open the serial interface on the workstation.
DTR 信号由你发出,告诉对方你已经准备好了(a space voltage) 或者没有准备好(a mark voltage). DTR功能会自动启动
CTS - Clear To Send
The CTS signal is received from the other end of the serial cable. A space voltage indicates that it is alright to send more serial data from your workstation.
CTS 信号来自接收端, a space voltage 表示你可以继续发送信号
CTS is usually used to regulate the flow of serial data from your workstation to the other end.
CTS 通常用于校准由你发出的串行数据
RTS - Request To Send
The RTS signal is set to the space voltage by your workstation to indicate that more data is ready to be sent.
Like CTS, RTS helps to regulate the flow of data between your workstation and the computer or device on the other end of the serial cable. Most workstations leave this signal set to the space voltage all the time.
当你将要发送的数据准备好了后可以将该引脚置为 space voltage 以示你请求发送该数据,但是一般我们会将该信号一直设为 space voltage 而不管是否真的有数据要发送!
Asynchronous Communications 异步传输模式
For the computer to understand the serial data coming into it, it needs some way to determine where one character ends and the next begins. This guide deals exclusively with asynchronous serial data.
为 了让计算机知道串行数据的抵达,需要一种方法去决定何时一个字符传送结束,而下一个又是何时开始的,这个指南用异步串行数据处理互斥
In asynchronous mode the serial data line stays in the mark (1) state until a character is transmitted. A start bit preceeds each character and is followed immediately by each bit in the character, an optional parity bit, and one or more stop bits. The start bit is always a space (0) and tells the computer that new serial data is available. Data can be sent or received at any time, thus the name asynchronous.
在异步模式下,串行数据信号会一直保持在 mark state 直到一个字符被传送。每个字符的传送会用一个开始位作为先头,然后紧接下来是数据位(字符本身),一个可选的奇偶位,一个或多个停止位。起始位通常是 space state 这告诉计算机串行数据已经可用,(任何时候都可以发送和接收数据,这就是所谓异步)
The optional parity bit is a simple sum of the data bits indicating whether or not the data contains an even or odd number of 1 bits. With even parity, the parity bit is 0 if there is an even number of 1's in the character. With odd parity, the parity bit is 0 if there is an odd number of 1's in the data. You may also hear the terms space parity, mark parity, and no parity. Space parity means that the parity bit is always 0, while mark parity means the bit is always 1. No parity means that no parity bit is present or transmitted.
那个可选 的奇偶位只是一个简单的关于数据位的和,用以表示是否在数据中存在奇数个或偶数个1 。对于偶校验,如果奇偶位为0表示数据中(指的是数据部分)含有偶数个1。对于奇校验来说,奇偶位为0表示数据部分包含奇数个1。也许你还听说过 space parity,mark parity ,and no parity. space parity 的意思是奇偶位永远被置0,mark parity 意思是奇偶位永远被置1。no parity 意思是无奇偶位或者不发送这个位。
The remaining bits are called stop bits. There can be 1, 1.5, or 2 stop bits between characters and they always have a value of 1. Stop bits traditionally were used to give the computer time to process the previous character, but now only serve to synchronize the receiving computer to the incoming characters.
剩下的那个停止位,可以为1位,1.5位,或者2位, 值一般为1。传统的停止位一般用于给计算机一段时间用来处理先前发送的字符,但是现在只用于为同步接收端到来的字符服务。
Asynchronous data formats are usually expressed as "8N1", "7E1", and so forth. These stand for "8 data bits, no parity, 1 stop bit" and "7 data bits, even parity, 1 stop bit" respectively.
What Are Full Duplex and Half Duplex? 全双工和半双工
Full duplex means that the computer can send and receive data simultaneously同时的 - there are two separate data channels (one coming in, one going out).
Half duplex means that the computer cannot send or receive data at the same time. Usually this means there is only a single data channel to talk over. This does not mean that any of the RS-232 signals are not used. Rather, it usually means that the communications link uses some standard other than RS-232 that does not support full duplex operation.
Flow Control 流控制
It is often necessary to regulate the flow of data when transferring data between two serial interfaces. This can be due to limitations界限 in an intermediate中间的 serial communications link, one of the serial interfaces, or some storage media. Two methods are commonly used for asynchronous data.
在两个串行接口之间传输数据时通常需要调整数据流,这可以 归因于中间的串行链路(也许是这二者中的其中一个接口,也许是缓存)达到极限 。 有两种方法用于异步数据传输。
The first method is often called "software" flow control and uses special characters to start (XON or DC1, 021 octal) or stop (XOFF or DC3, 023 octal) the flow of data. These characters are defined in the American Standard Code for Information Interchange ("ASCII"). While these codes are useful when transferring textual information, they cannot be used when transferring other types of information without special programming.
The second method is called "hardware" flow control and uses the RS-232 CTS and RTS signals instead of special characters. The receiver sets CTS to the space voltage when it is ready to receive more data and to the mark voltage when it is not ready. Likewise, the sender sets RTS to the space voltage when it is ready to send more data. Because hardware flow control uses a separate set of signals, it is much faster than software flow control which needs to send or receive multiple bits of information to do the same thing. CTS/RTS flow control is not supported by all hardware or operating systems.
What Is a Break?
Normally a receive or transmit data signal stays at the mark voltage until a new character is transferred. If the signal is dropped to the space voltage for a long period of time, usually 1/4 to 1/2 second, then a break condition is said to exist.
A break is sometimes used to reset a communications line or change the operating mode of communications hardware like a MODEM. Chapter 3, Talking to MODEMs covers these applications in more depth.