linux O_WRONLY 头文件 #include "fcntl.h"

#include <bits/stdc++.h>
#include "unistd.h"
#include "fcntl.h"

using namespace std;
#define MAX_BUF 256

int main(int argc, char* argv[])
{
    int fd;
    char buf[MAX_BUF]; 
    int gpio = 1;

    fd = open("/sys/class/gpio/export", O_WRONLY);
    sprintf(buf, "%d", gpio); 
    write(fd, buf, strlen(buf));
    close(fd);

    sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
    fd = open(buf, O_WRONLY);
    // Set out direction
    write(fd, "out", 3); 
    // Set in direction
    write(fd, "in", 2); 
    close(fd);

    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    fd = open(buf, O_WRONLY);
    // Set GPIO high status
    write(fd, "1", 1); 
    // Set GPIO low status 
    write(fd, "0", 1);
    close(fd);

    char value;
    sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
    fd = open(buf, O_RDONLY);
    read(fd, &value, 1);
    if(value == '0')
    { 
        // Current GPIO status low
    }
    else
    {
        // Current GPIO status high
    }
    close(fd);

    fd = open("/sys/class/gpio/unexport", O_WRONLY);
    sprintf(buf, "%d", gpio);
    write(fd, buf, strlen(buf));
    close(fd);

    return 0;
}
本文链接地址:https://const.net.cn/611.html

标签: GPIO

添加新评论