简体   繁体   English

无法从用户空间读取iio文件

[英]not able to read iio file from userspace

I am trying to read following file from C code. 我试图从C代码中读取以下文件。

file: /sys/bus/iio/devices/iio\\:device0/in_voltage7_raw file: /sys/bus/iio/devices/iio\\:device0/in_voltage7_raw

but file pointer I am getting is -1. 但我得到的文件指针是-1。

Using cat command it is able to read the file. 使用cat命令可以读取文件。

But I am trying to read the same from my code as follows: 但我试图从我的代码中读取相同内容如下:

nos_int32 nos_adc_read_port (ADC_PORT_DB *p_port, nos_int32 *data)
{

    char file_name[VALUE_MAX];
    int value;
    char buffer[BUFFER_LENGTH];
    char intBuffer[INT_BUFFER_LENGTH];
    int fd;

    sprintf(file_name, "/sys/bus/iio/devices/iio\\:device0/in_voltage7_raw");

    fd = open(file_name, O_RDONLY);

    if (fd == -1) {
        return(-1);
    }
    if (read(fd, buffer, BUFFER_LENGTH) == -1) {
        return(-1);
    }
    close(fd);
    memcpy(intBuffer, buffer, BUFFER_LENGTH);
    intBuffer[INT_BUFFER_LENGTH-1] = '\0';
    value = atoi(intBuffer);
    *data = value;
    return(0);
}

After the line: fd = open(file_name, O_RDONLY); 行后: fd = open(file_name, O_RDONLY);

value of fd is -1. fd的值为-1。 How can it be solved? 怎么解决?

Most command line shells use some characters for special actions and if you're trying to use them as their actual character, you need to prefix them with a backslash to escape them. 大多数命令行shell使用某些字符进行特殊操作,如果您尝试将它们用作实际字符,则需要使用反斜杠作为前缀来对其进行转义。 In this case, your shell needs you to escape the colon when accessing that filename. 在这种情况下,您的shell需要您在访问该文件名时转义冒号。

In C you don't have this issue so you can put in your code the filename as it truly is, such as: 在C中你没有这个问题所以你可以在你的代码中输入真正的文件名,例如:

"/sys/bus/iio/devices/iio:device0/in_voltage7_raw"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ARM linux用户空间gpio操作使用mmap / dev / mem方法(能够写入GPIO寄存器,但无法读取它们) - ARM linux userspace gpio operations using mmap /dev/mem approach (able to write to GPIO registers, but fail to read from them) 从用户空间读取一个字(2 字节)而不提供寄存器地址 - Read a word (2 byte) without providing a register address from userspace read(2) 和 write(2) 系统调用在哪里以及为什么在用户空间之间进行复制? - Where and why do read(2) and write(2) system calls copy to and from userspace? 从Linux内核模块从用户空间打开文件 - Opening a file from userspace from a Linux kernel module 无法从文本文件中读取数据。 - Not able to read data in from a text file. 无法使用fread从文件中读取数据 - not able to read data from file using fread 使用proc文件系统将信息从LKM写入用户空间 - Writing information from a LKM to userspace using the proc file system 使用fread并且无法从文件读取数据时出现分段错误 - segmentation fault while using fread and not able to read the data from the file 为什么此代码无法将文件中的内容读入链表? - Why is this code not able to read contents from a file into a linked list? 无法从标准输出读取 - Not able to read from stdout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM