现在的位置: 首页 > 综合 > 正文

Linux串口参数设置

2013年08月02日 ⁄ 综合 ⁄ 共 1728字 ⁄ 字号 评论关闭

在Linux中man串口有关的信息,在以下链接中http://www.9linux.com/tcgetattr.html

termios,  tcgetattr,  tcsetattr,  tcsendbreak,  tcdrain,  tcflush,  tcflow,  cfmakeraw,  cfgetospeed,cfgetispeed, cfsetispeed, cfsetospeed, cfsetspeed - 这些函数都是获取或者设置串口属性,控制线,获取和设置波特率

       #include <termios.h>
       #include <unistd.h>

       int tcgetattr(int fd, struct termios *termios_p);

       int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);

       int tcsendbreak(int fd, int duration);

       int tcdrain(int fd);

       int tcflush(int fd, int queue_selector);

       int tcflow(int fd, int action);

       void cfmakeraw(struct termios *termios_p);

       speed_t cfgetispeed(const struct termios *termios_p);

       speed_t cfgetospeed(const struct termios *termios_p);

       int cfsetispeed(struct termios *termios_p, speed_t speed);

       int cfsetospeed(struct termios *termios_p, speed_t speed);

描述:在termios函数描述了一个通用终端接口,提供给控制异步通信端口。在这里描述的许多功能有termios_p论点是对一个termios指针结构件,真实存在。这个结构至少包含以下成员:
       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 */

每个flag下面都有一大堆东西,不再赘述。

 

函数说明:
tcgetattr - 获取终端相关参数
#include <termios.h>
int tcgetattr(int fildes, struct termios * termios_p );
tcgetattr( )函数把获得文件句柄所表示的文件的终端参数,然后将获得的信息保存在struct termios * termios_p定义的变量中
termios_p是一个指向termios结构体的指针
成功返回0,失败返回-1

cfsetispeed - 设置输入波特率函数
cfsetospeed - 设置输出波特率函数
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
一般地用户需要将输入输出函数的波特率设置成一样的。这几个函数在成功时返回0,失败-1。

tcsetattr - 设置终端相关参数
函数原型:tcsetattr(fd,OPTION,&newtio);
这里的newtio就是termios类型的变量,OPTION可能的取值如下:
TCSANOW:改变的配置立即生效
TCSADRAIN:改变的配置在所有写入fd的输出都结束后生效
TCSAFLUSH:改变的配置自爱所有写入fd引用对象的输出都被结束后生效,所有已接受但为读入的输入都在改变发生前丢弃。
该函数调用成功返回0,失败-1.

 

抱歉!评论已关闭.