写在C的Linux的sysfs节点 [英] Write to Linux sysfs node in C

查看:435
本文介绍了写在C的Linux的sysfs节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从贝我能激活我的系统是这样的LED:

From the shell I can activate the leds on my system like this:

#echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness

我想从C程序做同样的事情,但我一直没能找到有关如何完成一个简单的例子?

I want to do the exact same thing from a C program, but I have not been able to find a simple example on how to accomplish this?

推荐答案

打开像一个文件,写1吧,sysfs的节点,并再次将其关闭。

Open the sysfs node like a file, write '1' to it, and close it again.

例如:

#include <stdio.h>
#include <fcntl.h>

void enable_led() {
  int fd;
  char d = '1';
  fd = open("sys/class/leds/NAME:COLOR:LOCATION/brightness", O_WRONLY);
  write (fd, &d, 1);
  close(fd);
}

这篇关于写在C的Linux的sysfs节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆