Unix的:如何清除串行端口I / O缓冲区? [英] Unix: How to clear the serial port I/O buffer?

查看:154
本文介绍了Unix的:如何清除串行端口I / O缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作高级别C ++为标准的PC串口接口上。当我打开端口,我想清楚了输入和输出缓冲器为了不接收或从端口的previous使用发送数据。要做到这一点,我用的是tcflush功能。但是,这是行不通的。怎么可能?我的端口开放code可以看到下面。是的,我使用C ++异常,但没有越来越抛出。这表明tcflush返回0,但它不清除缓冲区。

我可以清除输入缓冲区的唯一方法是,直到有没有留下从中读取字节。这通常需要几秒钟的时间,我不认为它是一个解决方案。

在此先感谢: - )

  FD = ::打开(port.c_str(),O_RDWR | O_NOCTTY);如果(FD℃,)
{
    扔OpenPortException(端口);
    返回;
}//获取选项
tcgetattr(FD,&安培;期权);//设置默认的波特率9600,1位停止位,8位数据长度,无奇偶校验
options.c_cflag&安培; =〜PARENB;
options.c_cflag&安培; =〜CSTOPB;
options.c_cflag&安培; =〜CSIZE;
options.c_cflag | = CS8;//默认超时(1000毫秒)
options.c_cc [VMIN] = 0;
options.c_cc [VTIME] = 10;//附加选项
options.c_cflag | =(CLOCAL | CREAD);这 - >港口=口;//现在应用设置
如果(tcsetattr(FD,TCSANOW,&放大器;!选项)= 0)
{
    扔PortSettingsException();
}//冲洗口
如果(tcflush(FD,TCIOFLUSH)!= 0)
{
    抛出IOException();
}


解决方案

尝试


  

的ioctl(FD,TCFLUSH,DIR)


用DIR等于0为接收,1为发射,2为。

I am working on a "high level" C++ interface for the standard PC serial port. When I open the port, I would like to clear the input and output buffers in order not to receive or send data from previous usage of the port. To do that, I use the tcflush function. However, it does not work. How can that be? My "port opening" code can be seen below. Yes I use C++ exceptions but none are getting thrown. That indicates that tcflush returns 0 but it does not clear the buffer.

The only way I can clear the input buffer is to read bytes from it until there is none left. This usually takes a couple of seconds and I do not think of it as a solution.

Thanks in advance :-)

fd = ::open(port.c_str(), O_RDWR | O_NOCTTY);

if (fd < 0)
{
    throw OpenPortException(port);
    return;
}

// Get options
tcgetattr(fd, &options);

// Set default baud rate 9600, 1 stop bit, 8 bit data length, no parity
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

// Default timeout (1000 ms)
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;

// Additional options
options.c_cflag |= (CLOCAL | CREAD);

this->port = port;

// Apply the settings now
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
    throw PortSettingsException();
}

// Flush the port
if (tcflush(fd, TCIOFLUSH) != 0)
{
    throw IOException();
}

解决方案

Try

ioctl(fd, TCFLUSH, dir)

with dir equal to 0 for receive, 1 for transmit, 2 for both.

这篇关于Unix的:如何清除串行端口I / O缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆