Linux 串口编程 中英文简体对照 b

来源:互联网 发布:三国志13帧数优化 3dm 编辑:程序博客网 时间:2024/05/01 09:32
2. Getting started 入门

2.1. Debugging 调试


The best way to debug your code is to set up another Linux box, and connect the two
computers via a null-modem cable. Use miniterm (available from the LDP programmers
guide (ftp://sunsite.unc.edu/pub/Linux/docs/LDP/programmers-guide/lpg-0.4.tar.gz
in the examples directory) to transmit characters to your Linux box. Miniterm can
be compiled very easily and will transmit all keyboard input raw over the serial
port. Only the define statement #define MODEMDEVICE "/dev/ttyS0" has to be checked.
Set it to ttyS0 for COM1, ttyS1 for COM2, etc.. It is essential for testing, that
all characters are transmitted raw (without output processing) over the line. To
test your connection, start miniterm on both computers and just type away. The
characters input on one computer should appear on the other computer and vice versa.
The input will not be echoed to the attached screen.

调试代码最好的方法,是另外建立一台Linux主机(Linux box),采用非调制解调器的串口
线(null-modem)连接两台机器。还可以使用minicom (可以从 LDP 编程指南上获得:
ftp://sunsite.unc.edu/pub/Linux/docs/LDP/.../lpg-0.4.tar.gz 里的
examples 目录)来传输字符到你的 Linux 主机。Miniterm 很容易编译而且会直接把键盘的
输入不做处理(raw 方式)地传到串口。只需要把 定义申明 #define MODEMDIVICE “/dev/ttyS0”
改一下。 COM1 口就设置成 ttyS0, COM2 口就是 ttyS1, 以此类推… 测试是必需的,所有
的字符直接通过缆线传输,不进行任何输出处理。为了测试你的连接,在你的两台机器上开
启minicom,然后随意输入一些字符。从一台电脑中输入的字符应该能显示在另一台设备
上,反之亦然。输入的字符不会回显(echo) 在本地的屏幕上.

To make a null-modem cable you have to cross the TxD (transmit) and RxD (receive)
lines. For a description of a cable see sect. 7 of the Serial-HOWTO.

自制非调制解调器的串口连接线(null-modem cable)时,你需要将一端的传送端(TxD)
与另一端的接收端(RxD)连接,一端的接收端与另外一端的传送端连接,详情见
Serial-HOWTO第七节。

It is also possible to perform this testing with only one computer, if you have
two unused serial ports. You can then run two miniterms off two virtual consoles.
If you free a serial port by disconnecting the mouse, remember to redirect /dev/mouse
if it exists. If you use a multiport serial card, be sure to configure it correctly.
I had mine configured wrong and everything worked fine as long as I was testing
only on my computer. When I connected to another computer, the port started loosing
characters. Executing two programs on one computer just isn't fully asynchronous.

如果你的电脑有两个空闲的串口端口的话,那幺只要使用一台机器就可以做这些试验了,你
可以在两个虚拟控制台上各运行一个miniterm,分别用来发送和接收结果。如果你使用串口
鼠标,记得在试验前将 /dev/mouse 复位向。如果你使用多端口的串口卡(multiport serial
card),一定要确保此设备配置正确,我的电脑就曾因为配置错误,而出现这样的问题:当
我在自己的机器上测试的时候一切正常,而连接到其它电脑上的时候,端口开始丢失数据。
注意,在一台机器上运行两个串口应用程序,并不是完全异步的。

2.2. Port Settings 端口设置

The devices /dev/ttyS* are intended to hook up terminals to your Linux box, and
are configured for this use after startup. This has to be kept in mind when programming
communication with a raw device. E.g. the ports are configured to echo characters
sent from the device back to it, which normally has to be changed for data transmission.

设备 /dev/ttyS* 会被当作连接到你的 Linux 机器的终端设备,而且在系统启动后就已经
配置好了。这一点是在你写 raw 设备的串口通信程序时必需牢记的. 举例来说,这个串口
被设定为回显(echo)所有自此设备送出的字符, 通常在做数据传输时,需要改变这种工作
模式。

All parameters can be easily configured from within a program. The configuration
is stored in a structure struct termios, which is defined in <asm/termbits.h>:
CODE
#define NCCS 19        
struct termios {            
  tcflag_t c_iflag;            /* input mode flags */    
  tcflag_t c_oflag;            /* output mode flags */      
  tcflag_t c_cflag;            /* control mode flags */      
  tcflag_t c_lflag;            /* local mode flags */    
  cc_t c_line;            /* line discipline */    
  cc_t c_cc[NCCS];            /* control characters */
}


所有的参数都可以在程序中轻松配置。配置保存在结构体 struct termios 中,这个结构是在 <asm/termbits.h> 中定义的
CODE
#define NCCS 19        
struct termios {              
 tcflag_t c_iflag;            /* 输入模式标志 */      
 tcflag_t c_oflag;            /* 输出模式标志 */      
 tcflag_t c_cflag;            /* 控制模式标志 */      
 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参数(行控制)
原创粉丝点击