QT对linux下/sys/class/gpio中的gpio的控制

时间:2021-04-11 17:49:46

#define SYSFS_GPIO_EXPORT           "/sys/class/gpio/export"
#define SYSFS_GPIO_RST_PIN_VAL      "926"
#define SYSFS_GPIO_RST_DIR          "/sys/class/gpio/gpio926/direction"
#define SYSFS_GPIO_RST_DIR_VAL      "IN"

1.导出控制的GPIO引脚编号

fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
if(fd == -1)
{
            printf("ERR: Radio hard reset pin open error.\n");
            
 }
 write(fd, SYSFS_GPIO_RST_PIN_VAL ,sizeof(SYSFS_GPIO_RST_PIN_VAL));
 ::close(fd);

 

2.定义输入输入方向

fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);
 if(fd == -1)
 {
          printf("ERR: Radio hard reset pin direction open error.\n");
         
 }
write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));
 ::close(fd);

 

3.读取数据

 IOfd = open("/sys/class/gpio/gpio926/value",O_RDONLY);
char buf[1];
read(IOfd,buf,1);