Reputation: 1161
I'm getting an EAGAIN error right after opening my serial port. The code is used in a shared library and called by Python code.
I know the port (/dev/ttyUSB0) is good. I used the same port to communicate directly with Python (PySerial) and it is working fine. In that case, my device answers.
But when the code below is called from ctypes (Python)... I get the EAGAIN error.
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?
Upvotes: 0
Views: 205
Reputation: 2361
The value of errno
after a successful call to any POSIX system function is unspecified, at least per Single Unix Specification v6. You should see if open()
was successful (i.e. returned non-negative handle), and analyze errno
only if open()
failed.
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.
Upvotes: 2