2016-03-27 17:43:24 books_world 阅读数 1697


http://blog.chinaunix.net/uid-20564848-id-74763.html


tcflush(int fd, int queue_selector)   //对以写但未发出,或已接收但未读数据的flush.
discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of
       queue_selector:

       TCIFLUSH
              flushes data received but not read.

       TCOFLUSH
              flushes data written but not transmitted.

       TCIOFLUSH
              flushes both data received but not read, and data written but not transmitted.
              
tcgetattr(int fd, struct termios *termios_p)   //get串口termios结构
gets the parameters associated with the object referred by fd and stores them in the termios structure referenced by termios_p. This function may be invoked from a background process; however, the terminal attributes may be subsequently changed by a foreground process.

tcsetattr(int fd, int optional_actions, const struct termios *termios_p)   //设置串口termios结构
sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from the termios structure referred to by termios_p.   optional_actions specifies when the changes take effect:
TCSANOW
   the change occurs immediately.

TCSADRAIN
   the change occurs after all output written to fd has been transmitted. This function should be used when changing parameters that affect output.

TCSAFLUSH
   the change occurs after all output written to the object referred by fd has been transmitted, and all input that has been received but not read will be discarded before the change is made.

2018-08-01 15:39:25 zuoao123 阅读数 2851

背景

在我们的工作中,串口是一种比较常用的数据传输方式。在某些应用场景下,当程序启动并打开串口后,我们不希望读出串口打开之前的输入缓存数据(即内核驱动已接收但是并未被应用层读出的数据),因此需要对输入缓存数据进行清空(刷清、丢弃)操作。下面以Linux系统上串口编程为例进行说明:


测试环境

硬件环境:PC
操作系统:Ubuntu 14.04虚拟机


测试代码

  • 方法一
fd = open("devpath", O_RDWR|O_NOCTTY);
usleep(200000);
tcflush(fd, TCIFLUSH);    //清空输入缓存
tcflush(fd, TCOFLUSH);    //清空输出缓存
tcflush(fd, TCIOFLUSH);   //清空输入输出缓存
tcsetattr(m_ifd, TCSANOW, &strctNewTermios);
  • 方法二
fd = open("devpath", O_RDWR|O_NOCTTY);
usleep(200000);
ioctl(fd, TCFLSH, 0);    //清空输入缓存
ioctl(fd, TCFLSH, 1);    //清空输出缓存
ioctl(fd, TCFLSH, 2);    //清空输入输出缓存
tcsetattr(fd, TCSANOW, &strctNewTermios);

总结

  1. 必须在open和tcflush(或ioctl)操作间进行延时操作(具体时间未做验证),否则没有清空效果,原因未知,可能跟Linux内核版本有关
  2. 可自由选择只清空输入或只清空输出或两者都清空
  3. tcflush和ioctl两种方法均有效,注意ioctl的第二个参数是TCFLSH
2016-10-07 17:17:24 u010936131 阅读数 705
Unix终端I/O函数。作用:清空终端未完成的输入/输出请求及数据。
包含头文件:
1
#include <termios.h>
1
int tcflush(int fd, int queue_selector);
参数说明:[1] 
1
2
3
4
5
6
7
8
9
fd                // 终端I/O打开的句柄
  
queue_selector    // 控制tcflush的操作,取值为下面三个常数中的一个:
  
    TCIFLUSH  // 清除正收到的数据,且不会读取出来。
  
    TCOFLUSH  // 清除正写入的数据,且不会发送至终端。
  
    TCIOFLUSH // 清除所有正在发生的I/O数据。
返回值说明:
**在往串口里面读写的时候,如果读和写的时间间隔比较短的切记不要在write函数之后加tcflush函数在
用reader函数读,这样容易出现读取失败。
造成失败的可能原因,当write函数写完数据之后会立马返回到串口一段数据,执行tcflush之后就会将串
口的数据清除,从而导致read读不到数据。
2019-04-13 14:20:29 as3522 阅读数 12

Linux串口编程——清空输入输出缓存

背景

在我们的工作中,串口是一种比较常用的数据传输方式。在某些应用场景下,当程序启动并打开串口后,我们不希望读出串口打开之前的输入缓存数据(即内核驱动已接收但是并未被应用层读出的数据),因此需要对输入缓存数据进行清空(刷清、丢弃)操作。下面以Linux系统上串口编程为例进行说明:

测试环境
硬件环境:PC
操作系统:Ubuntu 14.04虚拟机

测试代码

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

方法一

fd = open("devpath", O_RDWR|O_NOCTTY);
usleep(200000);
tcflush(fd, TCIFLUSH);    //清空输入缓存
tcflush(fd, TCOFLUSH);    //清空输出缓存
tcflush(fd, TCIOFLUSH);   //清空输入输出缓存
tcsetattr(m_ifd, TCSANOW, &strctNewTermios);

方法二

fd = open("devpath", O_RDWR|O_NOCTTY);
usleep(200000);
ioctl(fd, TCFLSH, 0);    //清空输入缓存
ioctl(fd, TCFLSH, 1);    //清空输出缓存
ioctl(fd, TCFLSH, 2);    //清空输入输出缓存
tcsetattr(fd, TCSANOW, &strctNewTermios);

总结

必须在open和tcflush(或ioctl)操作间进行延时操作(具体时间未做验证),否则没有清空效果,原因未知,可能跟Linux内核版本有关
可自由选择只清空输入或只清空输出或两者都清空
tcflush和ioctl两种方法均有效,注意ioctl的第二个参数是TCFLSH


原文

2015-04-10 11:08:40 luoyouren 阅读数 15919


http://blog.chinaunix.net/uid-20564848-id-74763.html


tcflush(int fd, int queue_selector)   //对以写但未发出,或已接收但未读数据的flush.
discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of
       queue_selector:

       TCIFLUSH
              flushes data received but not read.

       TCOFLUSH
              flushes data written but not transmitted.

       TCIOFLUSH
              flushes both data received but not read, and data written but not transmitted.
              
tcgetattr(int fd, struct termios *termios_p)   //get串口termios结构
gets the parameters associated with the object referred by fd and stores them in the termios structure referenced by termios_p. This function may be invoked from a background process; however, the terminal attributes may be subsequently changed by a foreground process.

tcsetattr(int fd, int optional_actions, const struct termios *termios_p)   //设置串口termios结构
sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from the termios structure referred to by termios_p.   optional_actions specifies when the changes take effect:
TCSANOW
   the change occurs immediately.

TCSADRAIN
   the change occurs after all output written to fd has been transmitted. This function should be used when changing parameters that affect output.

TCSAFLUSH
   the change occurs after all output written to the object referred by fd has been transmitted, and all input that has been received but not read will be discarded before the change is made.

linux串口调试记录

阅读数 521

没有更多推荐了,返回首页