串行端口读取返回EAGAIN

串行端口读取返回EAGAIN

本文介绍了Linux-串行端口读取返回EAGAIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从以下列方式打开的串行端口读取某些数据时遇到麻烦.我已经多次使用此代码实例,并且一切正常,但是现在,由于某种原因,我无法弄清楚,我完全无法从串行端口读取任何内容.

I am having some trouble reading some data from a serial port I opened the following way. I've used this instance of code plenty of times and all worked fine, but now, for some reason that I cant figure out, I am completely unable to read anything from the serial port.

我能够写信,并且在另一端正确接收了所有消息,但从未收到答复(正确发送)(不,电缆都还好;))

I am able to write and all is correctly received on the other end, but the replies (which are correctly sent) are never received (No, the cables are all ok ;) )

我用来打开串行端口的代码如下:

The code I used to open the serial port is the following:

fd = open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd == -1)
{
    Aviso("Unable to open port");
    return (fd);
}
else
{
    //Get the current options for the port...
    bzero(&options, sizeof(options)); /* clear struct for new port settings */
    tcgetattr(fd, &options);

    /*-- Set baud rate -------------------------------------------------------*/
    if (cfsetispeed(&options, SerialBaudInterp(BaudRate))==-1)
        perror("On cfsetispeed:");
    if (cfsetospeed(&options, SerialBaudInterp(BaudRate))==-1)
        perror("On cfsetospeed:");

    //Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB; /* Parity disabled */
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;  /* Mask the character size bits */
    options.c_cflag |= SerialDataBitsInterp(8);           /* CS8 - Selects 8 data bits */
    options.c_cflag &= ~CRTSCTS;                            // disable hardware flow control
    options.c_iflag &= ~(IXON | IXOFF | IXANY);           // disable XON XOFF (for transmit and receive)
    options.c_cflag |= CRTSCTS;                         /* enable hardware flow control */

    options.c_cc[VMIN] = 0;     //min carachters to be read
    options.c_cc[VTIME] = 0;    //Time to wait for data (tenths of seconds)

    //Set the new options for the port...
    tcflush(fd, TCIFLUSH);
    if (tcsetattr(fd, TCSANOW, &options)==-1)
    {
        perror("On tcsetattr:");
    }

    PortOpen[ComPort] = fd;
}

return PortOpen[ComPort];

端口初始化后,我通过简单的写命令向其中写一些东西...

After the port is initializeed I write some stuff to it through simple write command...

int nc = write(hCom, txchar, n);

其中hCom是文件描述符(没关系),并且(如我所说)可以工作.但是...当我稍后进行读取时,我从errno收到资源暂时不可用"错误.

where hCom is the file descriptor (and it's ok), and (as I said) this works. But... when I do a read afterwards, I get a "Resource Temporarily Unavailable" error from errno.

我测试了select以查看何时无法读取文件描述符...但是它总是超时!

I tested select to see when the file descriptor had something t read... but it always times out!

我这样读取数据:

ret = read(hCom, rxchar, n);

我总是得到一个EAGAIN,我也不知道为什么.

and I always get an EAGAIN and I have no idea why.

硬件运行正常!我可以看到串行端口上有入站数据,因为我已经制作了一条调试电缆来读取另一个终端上发生的事情.所以...

The HW is working fine! I can see that there is inbound data on the serial port because I've made a debug cable to read whats going on on another terminal. So...

我知道非阻塞应该做什么.我的问题是...为什么什么都没有读!相同的设置在Windows上可以正常工作,因此所有硬件都可以正常工作...

I know what nonblocking should do. My question is... why isn't anything getting read!. The same setup works fine on windows, so all hardware is working fine...

这真让我发疯!我敢肯定这很简单!我什至试着摆脱O_NONBLOCK来看看什么时候可以收到东西...但是什么都没有...

This is driving me nuts! I'm sure it's something simple as hell! I even tried getting rid of O_NONBLOCK to see when I would receive something... but nothing...

推荐答案

阅读.

这篇关于Linux-串行端口读取返回EAGAIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 08:54