ADC - C++ code hanging while executing read from file

We just updated the OS from Ubuntu 12 to latest Ubuntu 14.04 with 3.14.37 kernel.
Since we are using UART4 and ADC, we had to update the initialization, but it appears to be working. Doing a ‘cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw’, we get the ADC results.

However, sometimes, we get ‘Resource temporarily unavailable’ when reading from that file. The cat survives it and just exits. The problem is that we use the following C++ code to read the analog value from file

int fp = 0;
char pathTmp[128] = {0};
char readBuffer[10] = {0};
int ret = -1;
int adcVal = 0;

ret = sprintf(pathTmp, “/sys/bus/iio/devices/iio:device%d/in_voltage%d_raw”, 0, inputNum);
fp = open(pathTmp, O_RDONLY | O_NONBLOCK);
if (fp < 0)
{
printf(“Failed to open %s\r\n”, pathTmp);
return -1;
}

printf("_aR");

fd_set read_fds;

FD_ZERO(&read_fds);
FD_SET(fp, &read_fds);

struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 1000;

if (select(fp + 1, &read_fds, NULL, NULL, &timeout) != 1)
{
printf("_aT!");
close(fp);
return -1;
}

if (read(fp, readBuffer, 5) < 0)
{
printf(“Failed to read\r\n”);
close(fp);
return -1;
}
close(fp);

printf("_aC");

adcVal = atoi(readBuffer);

In this code, the execution just randomly hangs on executing ‘read’ command (the buffer size doesn’t effect), but as more tasks (ROS nodes) are executed on BeagleBone, earlier it happens.
The problem is that when the application hangs at ‘read’ command, the whole ROS node hangs…

We have checked the source code of ‘cat’ command and it doesn’t have anything obviously different than what we are using.

Does anybody has any idea on how to proceed?

Regards,
Matevž

Have you tried using strace on your executable to see what is happening behind the scenes ?

Have you tried using strace on your executable to see what is happening
behind the scenes ?

Besides that I don't see any C++ in the below C code, it's obviously the same problem as so often if people are doing their first steps with read(), select() and similar. :wink:

I suggest to read up about O_NONBLOCK in order to avoid all the problems if blocking I/O calls are handled wrong.

To give a hint, just because select() returned without a timeout doesn't mean read() will not block.

Alexander Holler