首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linux值文件未在导出中创建

Linux值文件未在导出中创建
EN

Stack Overflow用户
提问于 2017-11-01 15:10:33
回答 1查看 926关注 0票数 0

我试图编写一个基本的Linux用户空间应用程序。由于某些原因,我能够打开导出文件并以给定的编号导出GPIO。但是,导出它之后,我无法指定它是输入还是输出,因为没有创建/sys/class/gpio/gpio<#>/direction。结果,我的C犯了错误。

这是代码

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main()
{

    int valuefd, exportfd, directionfd;

    printf("GPIO test running...\n");

    exportfd = open("/sys/class/gpio/export", O_WRONLY);

    if(exportfd < 0)
    {
        printf("Cannot open GPIO to export it\n");
        exit(1);
    }

    write(exportfd, "971", 4);
    close(exportfd);

    printf("GPIO exported successfully\n");

    directionfd = open("/sys/class/gpio971/direction", O_RDWR);

    if(directionfd < 0)
    {
        printf("Cannot open GPIO direction it\n");
        exit(1);
    }

    write(directionfd, "out", 4);
    close(directionfd);

    printf("GPIO direction set as output successfully\n");

    valuefd = open("/sys/class/gpio/gpio971/value", O_RDWR);

    if(valuefd < 0)
    {
        printf("Cannot open GPIO value\n");
        exit(1);
    }

    printf("GPIO value opened, now toggling...\n");

    while(1)
    {
        write(valuefd, "1", 2);
        write(valuefd, "0", 2);
    }


    return 0;
}

run的输出:

root@plnx_arm:~# /usr/bin/basic-gpio GPIO测试运行..。 成功导出GPIO 无法打开GPIO方向

档案在那里

root@plnx_arm:~# ls /sys/class/gpio/gpio 971/ active_low器件方向边功率子系统value值

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-01 16:01:28

您需要打开文件“/sys/class/gpio/gpio 971/direction”,而不是“/sys/class/gpio 971/direction”。

代码语言:javascript
运行
复制
   directionfd = open("/sys/class/gpio/gpio971/direction", O_RDWR);

    if(directionfd < 0)
    {
        printf("Cannot open GPIO direction it\n");
        exit(1);
    }

您可以引用1,并获得导出/取消导出/设置方向/读/写gpio引脚的代码。

1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47057966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档