Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

i2X-6UB-GW网关支持3路数字输入(Digital Input)和3路数字输出(Digital Output)。

数字输入


Code Block
echo#include 68<stdio.h>
> /sys/class/gpio/export
echo 118 > /sys/class/gpio/export
echo 5 > /sys/class/gpio/export
 
echo in > /sys/class/gpio/gpio68/direction
echo in > /sys/class/gpio/gpio118/direction
echo in > /sys/class/gpio/gpio5/direction
 
cat /sys/class/gpio/gpio68/value
cat /sys/class/gpio/gpio118/value
cat /sys/class/gpio/gpio5/value

没有电压的时候value为1,外部有电压后value为0。

数字输出

Code Block
echo 0 > /sys/class/gpio/export
echo 1 > /sys/class/gpio/export
echo 67 > /sys/class/gpio/export

echo out > /sys/class/gpio/gpio0/direction
echo out > /sys/class/gpio/gpio1/direction
echo out > /sys/class/gpio/gpio67/direction

echo 0 > /sys/class/gpio/gpio0/value
echo 0 > /sys/class/gpio/gpio1/value
echo 0 > /sys/class/gpio/gpio67/value

echo 1 > /sys/class/gpio/gpio0/value
echo 1 > /sys/class/gpio/gpio1/value
echo 1 > /sys/class/gpio/gpio67/value#include <stdlib.h>
#include <stdbool.h>
#include "gpio.h"

int main(void) {
    gpio_t *gpio_in, *gpio_out;
    bool value;

    gpio_in = gpio_new();
    gpio_out = gpio_new();

    /* Open GPIO /dev/gpiochip0 line 10 with input direction */
    if (gpio_open(gpio_in, "/dev/gpiochip0", 10, GPIO_DIR_IN) < 0) {
        fprintf(stderr, "gpio_open(): %s\n", gpio_errmsg(gpio_in));
        exit(1);
    }

    /* Open GPIO /dev/gpiochip0 line 12 with output direction */
    if (gpio_open(gpio_out, "/dev/gpiochip0", 12, GPIO_DIR_OUT) < 0) {
        fprintf(stderr, "gpio_open(): %s\n", gpio_errmsg(gpio_out));
        exit(1);
    }

    /* Read input GPIO into value */
    if (gpio_read(gpio_in, &value) < 0) {
        fprintf(stderr, "gpio_read(): %s\n", gpio_errmsg(gpio_in));
        exit(1);
    }

    /* Write output GPIO with !value */
    if (gpio_write(gpio_out, !value) < 0) {
        fprintf(stderr, "gpio_write(): %s\n", gpio_errmsg(gpio_out));
        exit(1);
    }

    gpio_close(gpio_in);
    gpio_close(gpio_out);

    gpio_free(gpio_in);
    gpio_free(gpio_out);

    return 0;
}