[英]Write to Linux sysfs node in C
From the shell I can activate the leds on my system like this: 从shell我可以激活我系统上的LED,如下所示:
#echo 1 > /sys/class/leds/NAME:COLOR:LOCATION/brightness
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? 我想从C程序做同样的事情,但我还没有找到一个如何实现这个的简单例子?
Open the sysfs node like a file, write '1' to it, and close it again. 像文件一样打开sysfs节点,向其写入“1”,然后再次关闭它。
For example: 例如:
#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);
}
Something like this: 像这样的东西:
#include <stdio.h>
int main(int argc, char **argv)
{
FILE* f = fopen("/sys/class/leds/NAME:COLOR:LOCATION/brightness", "w");
if (f == NULL) {
fprintf(stderr, "Unable to open path for writing\n");
return 1;
}
fprintf(f, "1\n");
fclose(f);
return 0;
}
I'm not booted into my linux partition, but I suspect it goes something like this: 我没有启动到我的linux分区,但我怀疑它是这样的:
int f = open("/sys/class/leds/NAME:COLOR:LOCATION/brightness",O_WRONLY);
if (f != -1)
{
write(f, "1", 1);
close(f);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.