linux 清空串口数据_linux 串口清空 - CSDN
  • linux 清空串口缓存

    千次阅读 2016-03-27 17:43:26
    tcflush(int fd, int queue_selector) //对以写但未发出,或已接收但未读数据的flush. discards data written to the object referred to by fd but not transmitted,


    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.

    展开全文
  • tcflush(int fd, int queue_selector) //对以写但未发出,或已接收但未读数据的flush. discards data written to the object referred to by fd but not transmitted,
    
    


    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串口编程——清空输入输出缓存

    千次阅读 2018-08-01 16:00:58
    Linux串口编程——清空输入输出缓存 背景 在我们的工作中,串口是一种比较常用的数据传输方式。在某些应用场景下,当程序启动并打开串口后,我们不希望读出串口打开之前的输入缓存数据(即内核驱动已接收但是并...

    背景

    在我们的工作中,串口是一种比较常用的数据传输方式。在某些应用场景下,当程序启动并打开串口后,我们不希望读出串口打开之前的输入缓存数据(即内核驱动已接收但是并未被应用层读出的数据),因此需要对输入缓存数据进行清空(刷清、丢弃)操作。下面以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
    展开全文
  • stty设置串口参数 stty -F /dev/ttyO3 ispeed 115200 ospeed 115200 cs8 Linux 查看串口配置 ...监控串口数据接收 Cat /dev/ttyUSB0 & 记得选上发送新行 开发板上的串口工具 busybox m...

    stty设置串口参数

    stty -F /dev/ttyO3 ispeed 115200 ospeed 115200 cs8

    Linux 查看串口配置

    Stty -F /dev/ttyUSB0

    去掉串口回显

    Stty -F /dev/ttyUSB0 -echo

    监控串口数据接收

    Cat /dev/ttyUSB0 &

    记得选上发送新行

    开发板上的串口工具

    busybox microcom -t 5000 -s 9600 /dev/ttyUSB2

    如果要从终端输入内容,直接写入字符串,然后回车

     

     

    转载于:https://www.cnblogs.com/liushuhe1990/p/9609039.html

    展开全文
  • /////////////RS 232 FUCTIONS RS232 Fucitons //////////linux应用串口编程源,linux操作串口的代码,linux rs232串口例程///////////// ///////////////linux serial sample linux串口例程,linux RS232例程,////...
  • linux串口编程 select

    万次阅读 2016-06-08 14:40:22
     对于read,block指当串口输入缓冲区没有数据的时候,read函数将会阻塞在这里,一直到串口输入缓冲区中有数据可读取,read读到了需要的字节数之后,返回值为读到的字节数,然后整个程序才继续运行下去;(收)  ...
  • select()的机制中提供一fd_set的数据结构,实际上是一long类型的数组, 每一个数组元素都能与一打开的文件句柄(不管是Socket句柄,还是其他 文件或命名管道或设备句柄)建立联系,建立联系的工作由程序员完成, ...
  • Linux串口编程——清空输入输出缓存 背景 在我们的工作中,串口是一种比较常用的数据传输方式。在某些应用场景下,当程序启动并打开串口后,我们不希望读出串口打开之前的输入缓存数据(即内核驱动已接收但是并未...
  • 最近一个项目使用了嵌入式linxu串口通信,由于通信使用的是2进制数据,经常出现通信校验失败,数据帧大小没问题,就是校验出错 c_iflag参数表 键 值 说 明 IGNBRK 忽略BREAK键输入 ...
  • Linux串口操作之数据拼接

    千次阅读 2013-08-15 11:05:54
    串口操作中,特别以非阻塞的方式读取和发送数据,做好进程之间的同步很重要。有时我们会发现这样一个问题,在进行read操作时,一次read不能获得一个完整的数据帧,这就好比你买了一个电脑,送货的先把显示器送到你家...
  • linux串口通信主要有下面几个步骤 串口通信流程图 下面我会一一介绍这几个步骤。 1.打开串口 代码(串口为ttyUSB0) //打开串口 int open_port(void) { int fd; fd=open("/dev/ttyUSB0",O_RDWR | O_NOCTTY...
  • Linux下C语言串口通信

    万次阅读 2018-06-01 19:54:18
    如有不当之处,欢迎批评*/最近在做一个GPS项目,第一部分是将开发板和GPS用串口通信,接受GPS上传来的数据Linux下所有的设备都是以文件形式存储的,串口也是。整个串口通信的流程图为:所用到的头文件为:#...
  • linux串口的开发之非阻塞异步读

    千次阅读 2017-08-16 09:59:34
    读取串口数据使用文件操作read函数读取,如果设置为原始模式(Raw Mode)传输数据,那么read函数返回的字符数是实际串口收到的字符数。 char buff[1024]; int Len; int readByte = read(fd,buff,Len); ...
  • 打开串口后,用read读取串口数据,串口发来的 数据为20,而用read读取到的数据却是分多次得到的? 有时只读了8个数据便被截断了,这是为什么?? 怎样一次全部接收?? 谢谢 附代码: #include #...
  • linux串口发送流程

    千次阅读 2017-09-08 13:21:59
    void main() { int fd,wr_static,i=10; char *uart3 = "/dev/ttySAC3"; //ttySAC代表开发板上的串口 char *buffer = "hello world!\n"; printf("\r\nitop4412 ... /* 打开串口,可读写,不将该设备作为此进程
  • Linux 串口编程简介和实例学习

    千次阅读 2018-04-14 15:35:24
    Linux 串口编程简介和实例学习一、无论是从linux官方直接下载的原生态内核还是任何一家芯片厂家提供的linux内核,都已经把串口驱动写好了,所以在linux串口编程中,是完全不需要动手去写串口驱动的。对于一般的...
  • epoll机制读取linux串口数据

    千次阅读 2016-06-14 10:04:49
    epoll和select相比,最大不同在于: 1epoll返回时已经明确的知道哪个sokcet fd发生了事件,不用再一个个比对。这样就提高了效率。 2select的FD_SETSIZE是有限止的,而epoll是没有限止的只与系统资源有关。
1 2 3 4 5 ... 20
收藏数 2,752
精华内容 1,100
关键字:

linux 清空串口数据