On Thu, 1 Oct 2020 00:03:30 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user
alex.huf80-Re5JQ...@public.gmane.org wrote:
>
>
>
>Hi,
>
>the function below ist running for hours but sometimes an error no 9
>occours (which must be "Bad File Number*"). *Does anyone have an idea or
>------------------------------------------------------------------------------------------------------------------------------------
>uint16_t read_raw_adc_ch(int channel)
> {
>
> uint8_t error_ctr;
> char str[MAX_BUF];
> int output_fd;
> extern int errno;
> error_ctr = 0;
> while (error_ctr < 8)
> {
>
> // oeffnen loest eine messung des adc aus
> if (channel==0) output_fd =
>open("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", O_RDONLY |
>O_ASYNC);
> if (channel==1) output_fd =
>open("/sys/bus/iio/devices/iio:device0/in_voltage1_raw", O_RDONLY |
>O_ASYNC);
Pardon, but this set of statements seems rather crude and redundant.
One improvement would be to use a switch/case structure
switch (channel)
{
case 0:
output_fd = ...;
break;
case 1:
...;
default:
//value is not 0..7, dump an error and exit?
}
but that still has the redundancy in the open calls.
if (channel < 0 || channel > 7)
{
//value is not 0..7, dump an error and exit?
}
// path needs to be a character buffer of sufficient length
sprintf(path, "/sys/bus/iio/devices/iio:device0/in_voltage%1d_raw",
channel)
output_fd = open(path, ...)
Unfortunately I have no help for the "bad file number" condition. My
naive thought was that the runtime keeps incrementing the file number of
the fd on each open, and isn't recycling around to reuse file numbers.
However, that doesn't match the description for POSIX compliant open()
call.
Might it be possible to only open the 8 channels at the start of the
program, outside of the loop, and use something like
lseek(channel_fd, 0, SEEK_SET);
//and then read the channel_fd
{I've not tried seeking on the sysfs entries}
--
Dennis L Bieber