打开串口
在 Linux 下串口文件是位于 /dev 下的。串口一 为 /dev/ttyS0,串口二 为 /dev/ttyS1。打开串口是通过使用标准的文件打开函数操作:
int fd;
/*以读写方式打开串口*/
fd = open( "/dev/ttyS0", O_RDWR);
if (-1 == fd){
/* 不能打开串口一*/
perror(" 提示错误!");
}
设置串口
最基本的设置串口包括波特率设置,效验位和停止位设置。串口的设置主要是设置 struct termios 结构体的各成员值。
struct termio
{ unsigned short c_iflag; /* 输入模式标志 */
unsigned short c_oflag; /* 输出模式标志 */
unsigned short c_cflag; /* 控制模式标志*/
unsigned short c_lflag; /* local mode flags */
unsigned char c_line; /* line discipline */
unsigned char c_cc[NCC]; /* control characters */
};
SYNOPSIS
#include <termios.h>
DESCRIPTION
The <termios.h> header contains the definitions used by the terminal I/O interfaces (see the XBD specification, General Terminal Interface for the structures and names defined).The termios Structure
The following data types are defined through typedef:cc_t
- Used for terminal special characters.
speed_t- Used for terminal baud rates.
tcflag_t- Used for terminal modes.
The above types are all unsigned integral types.
The termios structure is defined, and includes at least the following members:
tcflag_t c_iflag input modes
tcflag_t c_oflag output modes
tcflag_t c_cflag control modes
tcflag_t c_lflag local modes
cc_t c_cc[NCCS] control charsA definition is given for:
NCCS
- Size of the array c_cc for control characters.
The following subscript names for the array c_cc are defined:
Subscript Usage Canonical Mode Non-canonical Mode Description VEOF EOF character VEOL EOL character VERASE ERASE character VINTR VINTR INTR character VKILL