简体   繁体   English

串口打开返回EAGAIN

[英]Serial port open returning EAGAIN

I'm getting an EAGAIN error right after opening my serial port.打开串行端口后,我立即收到 EAGAIN 错误。 The code is used in a shared library and called by Python code.该代码在共享库中使用,并由 Python 代码调用。

I know the port (/dev/ttyUSB0) is good.我知道端口(/dev/ttyUSB0)很好。 I used the same port to communicate directly with Python (PySerial) and it is working fine.我使用同一个端口直接与 Python (PySerial) 通信,它工作正常。 In that case, my device answers.在这种情况下,我的设备会回答。

But when the code below is called from ctypes (Python)... I get the EAGAIN error.但是当从 ctypes (Python) 调用下面的代码时......我得到了 EAGAIN 错误。

ERROR_CODES SerialPortLinux::openCommunication() {
    ERROR_CODES error_code;
    hComm = open(port_name.c_str(), O_RDWR | O_NOCTTY | O_SYNC);

    error_code = getPortErrorCode();
    if (error_code == ERROR_CODES::SUCCESS) {
        ...
    } else {
        close(hComm);
    }
    return error_code;
}


ERROR_CODES SerialPortLinux::getPortErrorCode(){
    ERROR_CODES error_code;
    auto error_number = errno;
    switch(error_number){
        ...
    }
}

Is there a configuration to be done prior to getting the handle?在获取手柄之前是否需要进行配置? Am I missing something obvious?我错过了一些明显的东西吗?

The value of errno after a successful call to any POSIX system function is unspecified, at least per Single Unix Specification v6.成功调用任何 POSIX 系统 function 后, errno的值未指定,至少根据 Single Unix 规范 v6。 You should see if open() was successful (ie returned non-negative handle), and analyze errno only if open() failed.您应该查看open()是否成功(即返回非负句柄),并仅在open()失败时分析errno

So, your code should be:所以,你的代码应该是:

ERROR_CODES SerialPortLinux::openCommunication()
{
    ERROR_CODES error_code;
    hComm = open(port_name.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
    if (hComm == -1)
    {
        error_code = getPortErrorCode();
        // Treat this error condition somehow
        return error_code;
    }

    return ERROR_CODES::SUCCESS;
}

In your particular case, I guess that the open() operation was successful, but you still got EAGAIN from errno because that value was there since the last failed operation.在您的特定情况下,我猜open()操作是成功的,但是您仍然从errno获得 EAGAIN,因为自上次操作失败以来该值就在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM