树莓派----温度传感器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dian0dian0/article/details/82806814

目的:通过温度传感器获取温度值,打印输出。


#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>

int get_temperature( float *temperature);


int main(int argc, char *argv[])

    {
        float temperature;
        if(get_temperature(temperature)<0)
           printf("get temperature error\n");
        else
           printf("The temperature is %f\n",temperature);
        }


int get_temperature(float *temperature)
    {
        char path[64] = "/sys/bus/w1/devices/";
        char chip[20];
        DIR  *dirp;
        struct dirent *direntp;
        int  found = 0;
        float value;
        char buf[128];
        char *ptr;
        int  fd = 0;
     if((dirp = opendir(path)) == NULL)
         {
             printf("can not open the directory according the path:s%\n",path);
             return -2;
         }

     while((direntp = readdir(dirp)) != NULL)
         {
              if(strstr(direntp->d_name,"28-"))
                   {
                       strcpy(chip,direntp->d_name);
                       found = 1;
                   }
          }
       closedir(dirp);

     if(!found)
        {
           printf("the chip is not exist\n");
           return -3;
          } 
     strncat(path,chip,sizeof(path));
     strncat(path,"/w1_slave",sizeof(path));


      if((fd = open(path,O_RDONLY))<0)
      {
          printf("read the %s error\n ",path);
          return -4;
      }
      else
      {
          if(read(fd,buf,sizeof(buf))<0)
          {
                  printf("read %s error %s\n",path,strerror(errno));
                  return -5;
          }
          ptr = strstr(buf,"t=")+2;
          if(!ptr)
          {

树莓派里面的设置懒得改,也不知道怎么改所以我也没有改,但是不改就没有办法识别中文都会识别成“?”,所以没有什么注释也就没有了。

基本步骤:

打开指定路径的文件夹--->返回结构体,

在结构体成员变量中有d_name 用于记载芯片的名称,虽然芯片的名称各不相同,但是都是以“28_"开头,

利用strstr字符串的查找函数找到芯片的ID,芯片中的文件名称都是相同的,然后利用已经读取的芯片的id,生成最后的绝对路径.

打开指定路径的文件,然后读取其中内容(read),寻找到温度值“t=”,获取温度值,最后对数值的转换输出。

主要函数分析:

1 opendir :

pi@raspberrypi:~ $ man 3 opendir

执行结果:

NAME
       opendir, fdopendir - open a directory    //opendir和fdopendir都是打开一个文件夹

SYNOPSIS
       #include <sys/types.h>
       #include <dirent.h>    //库函数

       DIR *opendir(const char *name);
       DIR *fdopendir(int fd);    //函数定义方式  返回值为一个DIR类型的返回值

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       fdopendir():
           Since glibc 2.10:
               _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
               _GNU_SOURCE            //这个不懂

DESCRIPTION
       The  opendir()  function  opens  a  directory stream corresponding to the directory name, and
       returns a pointer to the directory stream.  The stream is positioned at the  first  entry  in
       the directory. //返回文件流的指针,文件流的读取位置位于文件头~

2 符号的优先级

C语言符号优先级:https://zhinan.sogou.com/guide/detail/?id=316512393514

3 对于  int main(int argc, char *argv[]) 的理解

首先它是不同于c语言我们常用的main函数的书写方式,这是我在学习了linux之后才遇到的。

For example :

gcc hello.c -o hello

第一个int 为返回值的类型,这个很容易理解。

第二个int 为参数的个数,即命令行的参数个数。(该例子中就是argc = 4).

第三个char类型 表示的是一个字符串的二位数组,即参数组成的二维数组。(该例子中argv= "gcc hello.c -o hello").

4 如果c语言的文件没有close回会怎样?

正确解释:

如果是服务器程序是 24小时,360天一直要跑的,这时候文件描述符就会被一直被占用, 他最终也会被用完啊,就像 malloc()的内存一定要释放,如果小程序没啥影响,进程退出的时候他就释放了;而你服务器程序的话一直跑,那他就一直占用,可用的就多了啊,所谓的内存泄露(点击了解内存泄露)和文件描述符的资源占用,都是在程序一直运行,进程不退出的情况下成立的,一旦进程退出时,操作系统都会把这些资源释放。

猜你喜欢

转载自blog.csdn.net/Dian0dian0/article/details/82806814