1

以下 shell 命令可以很好地切换 Leopardboard gpio 31:

echo 31 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio31/direction
echo 0 > /sys/class/gpio/gpio31/value
echo 1 > /sys/class/gpio/gpio31/value
echo 0 > /sys/class/gpio/gpio31/value

用作以下代码的示例。知道为什么以下 .vala 代码不会切换 gpio 31 吗?

public void sync () {
    int fd = -1;
    fd = open("/sys/class/gpio/export", O_WRONLY);
    if (fd < 0) {
        GLib.stdout.printf("Error sync export\n");
        return;
    }
    write(fd, "31", 3);
    close(fd);
    fd = open("/sys/class/gpio/gpio31/direction", O_WRONLY);
    if (fd < 0) {
        GLib.stdout.printf("Error sync direction\n");
        return;
    }
    write(fd, "out", 4);
    close(fd);
    fd = open("/sys/class/gpio/gpio31/value", O_WRONLY);
    if (fd < 0) {
        GLib.stdout.printf("Error sync value\n");
        return;
    }
    write(fd, "0", 2);
    write(fd, "1", 2);
    Thread.usleep (1000); /* 1ms */
    write(fd, "0", 2);
    close(fd);
}
4

1 回答 1

3

echo 31实际上会导致 \x33\x31\x0a,但您的write调用将写入 \x33\x31\x00。尝试这样的事情:

write(fd, "31\n", 3);

并且您需要对其他调用进行类似的调整write

于 2012-07-25T04:04:35.310 回答