qq_42048208 2020-03-30 16:15 采纳率: 0%
浏览 178

linux 该如何检测板子掉电

1.板子掉电,系统可检测出掉电,并且可以写入日志,但是检测掉电对应的IO口文件一直是高电平,感觉低电平信息还在缓冲,read读文件读不到,该用什么方法可以读到缓冲内的东西

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h> 
#include <termios.h> 
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <linux/if.h>
#include <syslog.h>

const char GPIO_FILE[] = "/sys/class/gpio/export";

int opengpio(int sum)
{   char ss[10];
    sprintf(ss,"%d",sum);
    int fd1=open(GPIO_FILE, O_WRONLY);
    //O_WRONLY|O_SYNC|O_RSYNC|O_DIRECT
    if(fd1<0)
    {
        perror("open fail");
        return 0;
    } 
    write(fd1,ss,strlen(ss));
    close(fd1);
    return 0;
}


int main(int argc, char const *argv[])
{
    int powser=53;
    char gpio2[50]={0};
    char buffer[5]={0};
    char oldbuffer[5]={0};
    int flag=0;
    FILE * fp;

    sprintf(gpio2,"/sys/class/gpio/gpio%d/value",powser);
    opengpio(powser);
    int fd3;
    while(1)
    {

    sync();
    fd3=open(gpio2, O_RDONLY|O_SYNC|O_RSYNC|O_NOCTTY|O_NONBLOCK); 
    int ret=read(fd3,buffer,sizeof(buffer));
    //  fp=popen("cat /sys/class/gpio/gpio53/value","r");
    //  fgets(buffer,sizeof(buffer),fp);
     printf(" bufffer=%s\n ",buffer);
     // printf(" buf=%s\n ",buf);
      if (strcmp(buffer,oldbuffer)==0)
      {
          continue;
      }
      else
      {
      if(buffer[0]=='1')
       {
         sleep(0.001);
         int ret=read(fd3,buffer,sizeof(buffer));
         if(buffer[0]=='1')
        {
        openlog("power status:",LOG_CONS|LOG_PID,LOG_LOCAL0);
        syslog(LOG_ERR,"Power UP\n");
        printf("Power UP\n");
        }
       }
      else if(buffer[0]=='0')
       {
         printf("Power DOWN\n");
         openlog("power status:",LOG_CONS|LOG_PID,LOG_LOCAL1);
         syslog(LOG_ERR,"Power DOWN\n");
       }
      }

    strcpy(oldbuffer,buffer);
    sleep(0.001);
   // pclose(fp);
     close(fd3);
    }


    return 0;
}


结果读掉电对应的IO口文件,掉电的低电平read一直都不到,用命令cat可以读到对应的低电平,用什么办法可以使read也读到io口的低电平

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-07-25 16:39
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    可能的原因是因为文件IO没有及时刷新,导致读取不到最新的低电平信息。在代码中,可以尝试在打开文件后使用`fsync()`或`fdatasync()`函数来强制刷新文件缓冲区,确保读取到最新的数据。 示例代码如下所示:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <sys/select.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <linux/if.h>
    #include <syslog.h>
    const char GPIO_FILE[] = "/sys/class/gpio/export";
    int opengpio(int sum) {
        char ss[10];
        sprintf(ss, "%d", sum);
        int fd1 = open(GPIO_FILE, O_WRONLY);
        if (fd1 < 0) {
            perror("open fail");
            return 0;
        }
        write(fd1, ss, strlen(ss));
        close(fd1);
        return 0;
    }
    int main(int argc, char const *argv[]) {
        int powser = 53;
        char gpio2[50] = {0};
        char buffer[5] = {0};
        char oldbuffer[5] = {0};
        int flag = 0;
        FILE *fp;
        
        sprintf(gpio2, "/sys/class/gpio/gpio%d/value", powser);
        opengpio(powser);
        int fd3;
        
        while (1) {
            sync();
            fd3 = open(gpio2, O_RDONLY);
            fsync(fd3); // 强制刷新文件缓冲区
            int ret = read(fd3, buffer, sizeof(buffer));
            
            if (strcmp(buffer, oldbuffer) == 0) {
                continue;
            } else {
                if (buffer[0] == '1') {
                    // do something
                } else if (buffer[0] == '0') {
                    // do something
                }
            }
            
            strcpy(oldbuffer, buffer);
            sleep(0.001);
            close(fd3);
        }
        
        return 0;
    }
    

    通过添加fsync(fd3)函数来强制刷新文件缓冲区,可以确保在读取文件时获取到最新的IO口状态信息。

    评论

报告相同问题?