我正在尝试编写一个c ++程序,将1到15之间的数字转换为二进制,并通过板载BeagleBone Black LED显示输出。
#include<iostream>
#include<stdio.h>
#include<unistd.h>
using namespace std;
int main(){
FILE *BINHandle = NULL;
const char *LED0="/sys/class/leds/beaglebone:green:usr0/brightness";
const char *LED1="/sys/class/leds/beaglebone:green:usr1/brightness";
const char *LED2="/sys/class/leds/beaglebone:green:usr2/brightness";
const char *LED3="/sys/class/leds/beaglebone:green:usr3/bringtness";
int a, b, c;
cout << "Enter an number between 1 and 15" << endl;
cin >> a;
cout << a << " in binary is:" << endl;
for(b = 3; b >=0; b--){
c = a >> b;
if(c & 1){
if(b = 3){
BINHandle = fopen(LED3, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED3, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
if(b = 2){
BINHandle = fopen(LED2, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED2, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
if(b = 1){
BINHandle = fopen(LED1, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED1, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
if(b = 0){
BINHandle = fopen(LED0, "r+");
fwrite("1", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
else{
BINHandle = fopen(LED0, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
}
}
usleep(2000000);
BINHandle = fopen(LED0, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
BINHandle = fopen(LED1, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
BINHandle = fopen(LED2, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
BINHandle = fopen(LED3, "r+");
fwrite("0", sizeof(char), 1, BINHandle);
fclose(BINHandle);
}
现在我试图在该位为1时单独访问LED。我没有错误,但是当我运行代码并输入一个数字时,当我得到一个输出时,我得到“Segmentation fault”
答案 0 :(得分:3)
const char *LED0="/sys/class/leds/beaglebone:green:usr0/brightness";
const char *LED1="/sys/class/leds/beaglebone:green:usr1/brightness";
const char *LED2="/sys/class/leds/beaglebone:green:usr2/brightness";
const char *LED3="/sys/class/leds/beaglebone:green:usr3/bringtness";
const char *BIN = (LED3<<3)+(LED2<<2)+(LED1<<1)+LED0;
你不能像这样将字符串组合在一起。
如果您想要读取或写入多个文件,您需要打开每个文件并单独读/写。