打开设备时Linux串行端口缓冲区不为空 [英] Linux serial port buffer not empty when opening device

查看:104
本文介绍了打开设备时Linux串行端口缓冲区不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,我在意外的串行端口上看到异常的行为.以前,我有时通过usb到串行适配器看到这种情况,但现在我也可以在本机串行端口上看到它,而且频率更高.

I have a system where I am seeing strange behavior with the serial ports that I don't expect. I've previously seen this on occasion with usb-to-serial adapters, but now I'm seeing it on native serial ports as well, with much greater frequency.

系统已设置为运行自动化测试,并且将首先执行一些任务,这些任务导致在我没有打开端口的情况下从串行设备输出大量数据.设备还将自行重置.仅连接了tx/rx线.没有流量控制.

The system is set up to run automated tests and will first perform some tasks that cause a large amount of data to be outputted from the serial device while I do not have the ports open. The device will also reset itself. Only the tx/rx lines are connected. There is no flow control.

完成这些任务后,测试软件会打开串行端口,并立即失败,因为它会收到意外的响应.重现此内容时,我发现如果在终端程序中打开串行端口,则会看到立即清除了几千字节的旧数据(关闭端口后似乎已发送).关闭该程序后,即可按预期运行测试.

After these tasks complete, the testware opens the serial ports and immediately fails because it gets unexpected responses. When I reproduce this, I found that if I open the serial port in a terminal program, I see several kilobytes of old data (that appears to have been sent when the port was closed) immediately flushed out. Once I close this program, I can then run the tests as expected.

什么可能导致这种情况发生?设备关闭时,Linux如何处理缓冲串行端口?如果我打开一个设备,使其发送输出,然后关闭它而不读取它,是否会导致相同的问题?

What could cause this to happen? How does Linux handle buffering the serial port when the device is closed? If I opened a device, made it send output, and then closed it without reading from it, would this cause the same problem?

推荐答案

即使未打开输入,Linux终端驱动程序也会缓冲输入.这可能是一个有用的功能,特别是在速度/奇偶校验等情况下.设置正确.

The Linux terminal driver buffers input even if it is not opened. This can be a useful feature, especially if the speed/parity/etc. are set appropriately.

要复制较小操作系统的行为,请在端口打开后立即读取端口中所有未决的输入:

To replicate the behavior of lesser operating systems, read all pending input from the port as soon as it is open:

...
int fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
        exit (1);

set_blocking (fd, 0);   // disable reads blocked when no input ready

char buf [10000];
int n;
do {
        n = read (fd, buf, sizeof buf);
} while (n > 0);

set_blocking (fd, 1);  // enable read blocking (if desired)

...  // now there is no pending input



void set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error ("error %d getting term settings set_blocking", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = should_block ? 5 : 0; // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error ("error setting term %sblocking", should_block ? "" : "no");
}

这篇关于打开设备时Linux串行端口缓冲区不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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