tcflag_t c_lflag; /* 本地模式标志 */ cc_t c_line; /* 行控制 line discipline */ cc_t c_cc[NCCS]; /* 控制字符 control characters */ }
This file also includes all flag definitions. The input mode flags in c_iflag handle all input processing, which means that the characters sent from the device can be processed before they are read with read. Similarly c_oflag handles the output processing. c_cflag contains the settings for the port, as the baudrate, bits per character, stop bits, etc.. The local mode flags stored in c_lflag determine if characters are echoed, signals are sent to your program, etc.. Finally the array c_cc defines the control characters for end of file, stop, etc.. Default values for the control characters are defined in <asm/termios.h>. The flags are described in the manual page termios(3). The structure termios contains the c_line (line discipline) element, which is not used in POSIX compliant systems.
这个文件也包括了所有标志 (flag) 的定义。c_iflag 中的输入模式标志,进行所有的输入处理,也就是说从其他设备传来的字符,在被read函数读入之前,会先按照标志进行预处理。同样的,c_oflag 定义了所有的输出处理。c_cflag 包括了对端口的设置,比如波特率,停止符号等等。c_lflag 包括了,决定了字符是否回显,信号是否发送到你的程序,等等所有的本地工作方式。c_cc 定义了所有的控制符号,例如文件结束符和停止符等等,所有的这些参数的默认值都定义在<asm/termios.h>中,man手册页termios(3)中有这些参数的具体描述。termio结构体中还包括在不能在 POSIX 系统中使用的c_line参数(行控制)
2.3. Input Concepts for Serial Devices 串口设备的输入概念
Here three different input concepts will be presented. The appropriate concept has to be chosen for the intended application. Whenever possible, do not loop reading single characters to get a complete string. When I did this, I lost characters, whereas a read for the whole string did not show any errors.
这里将介绍串行设备三种不同的输入方式,你需要为你的程序选择合适的工作方式。任何可能的情况下,不要采用循环读取单字符的方式来获取一个字符串。我以前这样做的时候,就丢失了字符,而读取整个字符串的 read 方法,则没有这种错误。
2.3.1. Canonical Input Processing 标准输入模式
This is the normal processing mode for terminals, but can also be useful for communicating with other dl input is processed in units of lines, which means that a read will only return a full line of input. A line is by default terminated by a NL (ASCII LF), an end of file, or an end of line character. A CR (the DOS/Windows default end-of-line) will not terminate a line with the default settings.
Canonical input processing can also handle the erase, delete word, and reprint characters, translate CR to NL, etc..
这是终端设备的标准处理模式, 在与其它 dl 的以行为单位的输入通讯中也很有用。这种方式中, read 会传回一整行完整的输入. 一行的结束,默认是以 NL (ASCII值 LF), 文件结束符, 或是一个行结束字符。默认设置中, CR ( DOS/Windows 里的默认行结束符) 并不是行结束标志。
标准的输入处理还可以处理 清除, 删除字, 重画字符, 转换 CR 为 NL 等等功能。
2.3.2. Non-Canonical Input Processing 非标准输入模式
Non-Canonical Input Processing will handle a fixed amount of characters per read, and allows for a character timer. This mode should be used if your application will always read a fixed number of characters, or if the connected device sends bursts of characters.
非标准输入处理可以用于需要每次读取固定数量字符的情况下, 并允许使用字符接收时间的定时器。这种模式可以用在每次读取固定长度字符串的程序中, 或者所连接的设备会突然送出大量字符的情况下。
2.3.3. Asynchronous Input 异步输入模式
The two modes described above can be used in synchronous and asynchronous mode. Synchronous is the default, where a read statement will block, until the read is satisfied. In asynchronous mode the read statement will return immediatly and send a signal to the calling program upon completion. This signal can be received by a signal handler.
前面敘述的兩種模式都可以用在同步與异步的傳輸模式。默认是在同步的模式下工作的, 也就是在尚未讀完数据之前, read 的狀態會被阻塞(block)。而在异步模式下,read 的狀態會立即返回並送出一个信号到所调用的程式直到完成工作。這個信号可以由信号处理程式 handler來接收。
2.3.4. Waiting for Input from Multiple Sources 等待来自多信号源的输入
This is not a different input mode, but might be useful, if you are handling multiple devices. In my application I was handling input over a TCP/IP socket and input over a serial connection from another computer quasi-simultaneously. The program example given below will wait for input from two different input sources. If input from one source becomes available, it will be processed, and the program will then wait for new input.
The approach presented below seems rather complex, but it is important to keep in mind that Linux is a multi-processing operating system. The select system call will not load the CPU while waiting for input, whereas looping until input becomes available would slow down other processes executing at the same time.
本节介绍的不是另一个输入模式,不过如果你要处理来自多个设备的数据的话,可能会很有用。在我的应用程序中,我需要同时通过一个 TCP/IP socket 和一个串口来处理其它计算机传来的输入。下面给出的示例程序将等待来自两个不同输入源的输入。如果其中一个信号源出现, 程序就会进行相应处理, 同时程序会继续等待新的输入。
后面提出的方法看起来相当覆杂, 但请记住 Linux 是一个多进程的操作系统。 系统调用 select 并不会在等待输入信号时增加 CPU 的负担,而如果使用轮询方式来等待输入信号的话,则将拖慢其它正在执行的进程。
上一页 [1] [2] [3] |