Errors reading multiple Analog Inputs from Beaglebone Black using C++

Hi friends,

I am trying to voltage values from 2 analog sensors which are connected to pins AIN0 and AIN1 using C++. I have taken Derek Molloy’s code for reading one voltage value and edited it to add a second pin to read values from. The code is as follows:

#include
#include
#include
#include
#include
using namespace std;

#define LDR_PATH “/sys/bus/iio/devices/iio:device0/in_voltage”

int readAnalog(int number){

stringstream ss;

ss << LDR_PATH << number << “_raw”;

fstream fs;

fs.open(ss.str().c_str(), fstream::in);

fs >> number;

fs.close();

return number;

}

int main(int argc, char* argv[]){

cout << “Starting the IR distance sensor program:” << endl;

while(1) {

int value0 = readAnalog(0);
int value1 = readAnalog(1);
float V0 = ((float)value0 / 4096) * 1.8;
float V1 = ((float)value1 / 4096) * 1.8;

cout << "Sensor0 is: " << V0 << " Sensor1 is: " << V1 << ‘\r’ << flush;

usleep(100000);

}

return 0;
}

However, when I run this code, after 10-15 successful iterations, I receive this error message:

terminate called after throwing an instance of ‘std::ios_base::failure’
what(): basic_filebuf::underflow error reading the file
Aborted

I am not sure as to why I am receiving this error, or really what this error message even means. My guess it is some access error when trying to read the pin value.

The Adafruit BBIO python library actually found a work around this issue in C, but I’m not sure how I can extrapolate this to C++ (see: https://github.com/adafruit/adafruit-beaglebone-io-python/blob/master/source/c_adc.c)

If anyone has any insight or help they could offer, that would be very helpful!

Thanks

It’s been a while since I’ve written anything in C++, but I notice you’re not doing any error checking on your filestream. For instance check out the very simply code listing I demonstrate here ( in C ). http://www.embeddedhobbyist.com/2015/10/beaglebone-black-adc/

Notice that I’m checking if length is -1, and if so, moving code execution back up to the top of the loop. Using continue. You should be doing something similar( whatever filestream allows ). Also, I’ve not idea how filestream works in Linux, but if you’re opening a new file descriptor every function call, you’ll want to refactor that out. e.g. you get your file descriptor only once when the executable is first run, and then operate off that.

Another thing that you have not really asked about but deserves mention. You’re declaring 2 int’s and two floats every program loop. You’ll want to fix that . . .

What version of the kernel are you using? There's a problem with some of them.

The ints and floats inside the loop are not a problem. Only one of each is allocated on the stack when the function is entered, and then they're assigned each time through the loop. Perfectly fine (and good practice for limiting the scope of variables).

The ints and floats inside the loop are not a problem. Only one of each is allocated on the stack when the function is entered, and then they’re assigned each time through the loop. Perfectly fine (and good practice for limiting the scope of variables).

This would assume you know which compiler is being used, and you’ve actually disassembled the executable to see what the compiler does. Generally, it is a bad idea to assume you know what is happening. Because of this ambiguity, it is considered bad form.

A while statement has no concept of scope. So main() has scope whether inside the while statement, or not.

Hi Sunny!

ADC over sysfs is buggy (at least in kernel versions 3.8.x). That’s why I wrote libpruio. It provides full control over the ADC subsystem at full speed (200 kHz). Find a simple C example to scan ADC values at this links: Description and Source Code.

Some users told me that they compile against the library from C++ code.

BR

I'm not sure what you think is going on in that code. A while loop most certainly has a scope, in that the variables declared within the while block are not visible before nor after the loop. Any compiler that accepts the syntax (i.e. any modern C or C++ compiler) will treat that exactly the same as if the variables were declared at the top of the function, except that the name will only be visible within the loop.

I’m not sure what you think is going on in that code. A while loop most certainly has a scope, in that the variables declared within the while block are not visible before nor after the loop. Any compiler that accepts the syntax (i.e. any modern C or C++ compiler) will treat that exactly the same as if the variables were declared at the top of the function, except that the name will only be visible within the loop.

You are absolutely correct, and I do not know what the hell I was thinking. Generally I always declare variables outside of loops, because I think it is clearer. But what should have made it clear to me is that anything inside of curly brackets is limited in scope to that set of brackets. You do not even need a function or loop, etc to limit scope in this manner.