tcgetattr学习

一、函数名称:

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

二、函数功能:

 The termios functions describe a general terminal interface that is provided to control asynchronous communications ports.
用于获取与终端相关的参数,提供异步通讯接口

三、头文件

termios.h
unistd.h


四、返回值

0:成功
-1:错误,

五、参数说明

fd:文件描述符
struct 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];   /* special characters */                      控制字符,保存中断驱动程序中的特殊字符

六、示例程序:


  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <unistd.h>
  4. #include <errno.h>

  5. int main(void)
  6. {
  7.     struct termios term; //get terminal interface
  8.     int err;

  9.     if(tcgetattr(STDIN_FILENO, &term)==-1)
  10.     {
  11.         perror("Cannot get standard input description.");
  12.         return 1;
  13.     }

  14.     term.c_cc[VEOF] = (cc_t)0x07;
  15.     err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
  16.     if(err==-1 && err==EINTR)
  17.     {
  18.         perror("Failed to change EOF character.");
  19.         return 1;
  20.     }

  21.     return 0;
  22. }
运行后可以使ctrl+D不起作用,使用ctrl+G替代ctrl+D.


七、补充:

函数tcsetattr 可以设置串口的结构属性,tcgetatt( ) 可以得到串口的结构属性。在termios 结构中,最重要的是c_cflag,用户通过对其进行赋值可以实现串口波特率、数据位、停止位、奇偶校验位等参数的设置。c_cc 数组中的两个变量VMIN 和VTIME 判断是否返回输入,c _cc[VTIME]设定字节输入时间计时器,c _cc[VMIN]设定满足读取功能的最低接收字节数。这两个变量的值要设定合理,才能保证串口的通信成功率。
http://www.hqew.com/tech/sheji/660631.html
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(20) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值