Alex,

The EBADF may not be your actual problem, because immediately before
exiting the loop you attempt to close an invalid file descriptor:  In the
"then" part of your if statement, you verify in the condition that the
output_fd is less than zero (an invalid file descriptor), then you call
close() on that invalid file descriptor (which also returns an error, but
you don't see it because you're not checking the return value of close()
there).  Once you break out of the loop, errno is set to EBADF from the
close() call, not from the open() call.

You should add the printout as the first line inside of the "then" block of
your if ( output_fd < 0 ) -- that will tell you what errno was returned
from the open() call.

Jeremy Trimble
Principal Research Engineer
Two Six Labs, LLC  *:|:::*
https://twosixlabs.com/


On Thu, Oct 1, 2020 at 6:59 AM <[email protected]> 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
> suggestions for other flags? This is the only function in the whole
> programm that is accessing the ADC. After the exit systemd restarts the
> programm and it runs fine again for hours. The function is called from a
> loop while another file with another fd is open (logfile for the adc-data,
> with a size of about 500bytes then a new logfile is started). The
> read_raw_adc_ch(0); is called every 10 seconds. As you can see I've tried
> to write a workaround with the error_ctr loop, but even the wait for one
> second does not help.
>
> Thanks for any ideas or suggestions.
>
> Alex
>
>
> $ cat /proc/version
> Linux version 4.19.94-ti-r42 (voodoo@x3-am57xx-beagle-x15-2gb) (gcc
> version 8.3.0 (Debian 8.3.0-6)) #1buster SMP PREEMPT Tue Mar 31 19:38:29
> UTC 2020
>
> cat /etc/debian_version
> 10.3
>
> Board: BeagleBoneBlack Rev. C
>
>
>
> ------------------------------------------------------------------------------------------------------------------------------------
> 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);
>         if (channel==2) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage2_raw", O_RDONLY |
> O_ASYNC);
>         if (channel==3) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage3_raw", O_RDONLY |
> O_ASYNC);
>         if (channel==4) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage4_raw", O_RDONLY |
> O_ASYNC);
>         if (channel==5) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage5_raw", O_RDONLY |
> O_ASYNC);
>         if (channel==6) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage6_raw", O_RDONLY |
> O_ASYNC);
>         if (channel==7) output_fd =
> open("/sys/bus/iio/devices/iio:device0/in_voltage7_raw", O_RDONLY |
> O_ASYNC);
>
>         if (output_fd < 0)
>         {
>         error_ctr++;
>         close(output_fd);
>         sleep(1);
>         }
>         else
>         {
>         read(output_fd, str, 10);
>         close(output_fd);
>         return atoi(str);
>         }
>
>     }
>
>
>     printf("ERROR OPEN ADC DEVICE, ERRNO: %d", errno);
>         exit(EXIT_FAILURE);
>
> return 0;
> }
>
> ------------------------------------------------------------------------------------------------------------------------------------
>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/beagleboard/add6f258-c7f8-48ff-870f-b6f093838902o%40googlegroups.com
> <https://groups.google.com/d/msgid/beagleboard/add6f258-c7f8-48ff-870f-b6f093838902o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/CACS14pi-bcm4i1-SvHeBaqwMDJc46UkXAHOAFeJ95MLL7VXiyQ%40mail.gmail.com.

Reply via email to