读取 ADC(比格尔骨黑),而不关闭文件描述符
Read ADC (Beaglebone Black) without closing File Descriptor
是否可以在不关闭文件描述符的情况下读取Beaglebone Black或其他嵌入式Linux系统的ADC?
我在read()
之前尝试了选择. select()
返回 1,但read()
在第一次迭代后返回 0,因此我无法获得任何数据。有什么想法吗?关闭和打开文件描述符是否需要大量的 CPU 能力?
我的代码:
#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);
}
问题可能是read()
更改了文件偏移量。尝试在读取后以 lseek(2)
查找回文件的开头,或使用 pread(2)
从偏移量 0 显式读取。– 超声刀
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- I2C 文件描述符上的 I2C 总线可写/可读标志
- 许多文件描述符在调用sys_clone时
- AMQP-CPP >处理程序中的错误文件描述符
- 如何使用 WINAPI 和 C++ 提取可执行文件的文件描述?
- 正在等待在非阻塞文件描述符上长时间运行ioctl
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 如何强制文件描述符缓冲我的输出
- 如何从 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 获取本机套接字文件描述器?
- 哪个更适合从C++写入敏感的日志文件,在文件描述符上写()或文件上的fprintf()?
- 将 select() 与非基于文件描述符的输入一起使用
- accept(..) 似乎正在修改我给它的文件描述符参数
- 使用 Select 多路复用未命名的管道和其他文件描述符
- 提升 ASIO 绑定:错误的文件描述符
- 使用文件描述符移动对象
- 无法从零MQ ZMQ_SERVER套接字中获取文件描述符
- read() 上的不同行为取决于写入不可写内存时表示文件、匿名管道或套接字的文件描述符
- 我可以在不使用 FIOCANCEL 的情况下关闭 VxWorks 中的文件描述符吗?
- 将 Boost Asio 与 ZeroMQ 集成,文件描述符错误?