Чтение АЦП (Beaglebone Black) без закрытия дескриптора файла

Можно ли прочитать АЦП Beaglebone Black или другой встроенной системы Linux без закрытия дескриптора файла?

Я попробовал это с выбором раньше read(), select() возвращает 1, но read() возвращает 0 после первой итерации, и поэтому я не могу получить какие-либо данные. Есть идеи? Требует ли закрытие и открытие файлового дескриптора большой мощности процессора?

Мой код:

 #include<iostream>
#include<fstream>
#include<string.h>
#include<sstream>
#include<fcntl.h>
#include<unistd.h>
#include<sys/select.h>
#include <sys/time.h>

using namespace std;

#define LDR_PATH "/sys/bus/iio/devices/iio:device0/in_voltage"
int main(int argc, char* argv[]){
int number = 1;
int AdcConnection = 0;

stringstream AdcPath;
AdcPath << LDR_PATH << number << "_raw";AdcConnection = open(AdcPath.str().c_str(),O_RDONLY |O_NONBLOCK);

if (AdcConnection <0)
{
perror("UART: Failed to open the file.\n");
close(AdcConnection);
return -1;
}

fd_set fdsAdcRead;
struct timeval timeout = {5, 0};

unsigned char receive[5];
int FlagRead = -1;
int FlagSelect = -1;

while (1)
{
FD_ZERO(&fdsAdcRead); //clear the file descriptor
FD_SET(AdcConnection,&fdsAdcRead); //Set the descriptor

FlagSelect = select(AdcConnection+1,&fdsAdcRead,NULL,NULL,&timeout);//check if data are available

if (FlagSelect <0)
{
perror("Failed to check if data are available.\n");
close(AdcConnection);
return -1;
}
else if (FlagSelect ==0)
{
cout << "There were no Data" << endl;
timeout.tv_sec = 5;
}
else
{
memset(&receive,0,sizeof(receive));
FlagRead = read(AdcConnection, (void*)receive, 5);
cout << receive << endl << FlagRead << FlagSelect << endl;
timeout.tv_sec = 5;
}usleep(1000000);
}

0

Решение

Проблема, вероятно, в том, что read() изменяет смещение файла. Попробуйте вернуться к началу файла с помощью lseek(2) после прочтения или использования pread(2) явно читать со смещения 0. — Ulfalizer

0

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector