串口定义参数详解

本文详细介绍了在Linux系统中如何打开和配置串口,包括使用标准文件打开函数打开串口,如/dev/ttyS0,以及设置串口的波特率、校验位和停止位。重点解析了struct termios结构体在串口设置中的应用。
摘要由CSDN通过智能技术生成

打开串口

在 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 */
};

下面就是termios.h的定义:
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 chars

A 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  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值