树莓派获取温度—c语言(修改版)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
int get_temp(float *temp);
int main(int argc,char *argv[])
{
        float temp;
        int right;
        right = get_temp(&temp);
        if(right<0)
        {
          printf("The failure is:%s\n",right);
          return -1;
        }
        printf("The temp is:%f\n",temp);
        return 0;
}

int get_temp(float *temp)



{
        int           fd=-1;
        char          buf[128];
        char         *ptr;
        DIR          *dirp = NULL;
        struct dirent *direntp =NULL;
        char          w1_path[64]= "/sys/bus/w1/devices/";
        char          chip_sn[64];
        int            n = 0;
        char          ds_path[64];

        dirp = opendir(w1_path);

        if(!dirp)
        {

                printf("open folder %s failure:%s\n",w1_path,strerror(errno));
                return -1;

        }
        while(NULL !=(direntp=readdir(dirp)))
        {
                if(strstr(direntp->d_name,"28-"))
                {
                        strncpy(chip_sn,direntp->d_name,sizeof(chip_sn));
                        n =1;
                }


        }

        closedir(dirp);
        if(!n)
        {

          printf("Can not find it\n");
          return -2;
        }

//      snprintf(ds_path,sizeof(ds_path),"%s%s/w1_slave",w1_path,chip_sn);//用这个或者下两个函数都可以
        strncat(w1_path,chip_sn,sizeof(w1_path)-strlen(w1_path));
        strncat(w1_path,"/w1_slave",sizeof(w1_path)-strlen(w1_path));

        printf("The path is:%s\n",w1_path);

        if((fd=open(w1_path,O_RDONLY))<0 )
        {
          printf("Can not open the path failure %s\n",strerror(errno));
          return -3;

        }


        memset(buf,0,sizeof(buf));
        if(read(fd,buf,sizeof(buf))<0)
        {
          printf("read date from fd=%d failure %s\n",fd,strerror(errno));
          return -4;
        }

        printf("buf:\n%s\n",buf);
        ptr=strstr(buf,"t=");
        if(!ptr)
        {


        printf("Can not find \n");

        return -5;

        }

        ptr +=2;

        *temp = (atof(ptr))/1000;
        printf("temp:%f\n",*temp);
        close(fd);

        return 0;



}

输出结果

详解:

当我们写程序时应该养成一个功能写成一个函数的习惯,

在设置文件操作符的时候我们应习惯将其设置为返回值错误如-1

opendir

即打开其中的目录,成功则返回指针,失败则返回NULL,其返回类型是DIR*类型

readdir

          struct dirent
             {
             ino_t                     d_ino;                         //d_ino 此目录进入点的inode
             off_t                        d_off;                             //d_off 目录文件开头至此目录进入点的位移
             signed short         int d_reclen;  //d_reclen _name 的长度, 不包含NULL 字符
             unsigned char      d_type;       //d_type d_name 所指的文件类型
             char                       d_name[256];              //d_name 文件名
             };

readdir一般和opendir一起使用,毕竟打开了才能读取,且读取的内容需是DIR*类型的,一般我们用到的是最后一个文件名。其返回类型为结构体指针类型,因此需要提前定义一个结构体指针,我们在使用相关的头文件的时候已经包括了相关的结构。

strncpy

 拷贝字符串

strcpy函数和strncpy的区别的后者控制字节数, dest为目的的,src为要复制的字符串

closedir

关闭目录,为在这个代码中我已经把找到的相关文字拷贝到其他数组中了。

snprintf

 str表示存储字符串的地方,size表示存储的大小,一般用sizeof来计算,后面则跟要打印的

strncat

把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。该处用saizeof和strlen来计算n是为了防止溢出

另外,若是想在该函数中传入参数并在函数外打印结果时,应该传入该参数的地址。

接下里的与上一篇相同,详情请看上一篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值