Linux下用c程序读取树莓派DS18B20探测的温度


在写编程之前我们,我们先要了解文件I/O操作函数!我将介绍一下我们经常用到的几个函数,

文件I/O操作函数

一、open系统调用

int open(const char*path,int oflag,../*mode_t mode*/); 

1、函数说明:open()系统调用用来打开一个文件,并返回一个文件描述符(file description), 并且该文件描述符是当前进程最小、未使用的
文件描述符数值。
2、参数说明:(1)path: 要打开的文件、设备的路径。
(2)oflag: 由多个选项进行“或”运算构造oflag参数 。
①必选:

O_RDONLY (只读)
O_WRONLY(只写)
O_RDWR(读写)

② 可选:

O_APPEND 每次写时都追加到文件的尾端;
O_CREAT 文件不存在则创建它,使用该选项需要第三个参数mode
mode: oflag带O_CREAT选项时可以用来创建文件,这时必须带该参数用来指定创建文件的权限模式,如066。 否则不需要;
O_TRUNC 如果文件存在,而且为只写或读写成功打开,则将其长度截取为0;
O_NONBLOCK 如果path是一个FIFO、块设备、字符特殊文件则此选项为文件的本次打开和后续的I/O操作设置非阻塞模式方式。
O_EXEC、O_SEARCH、O_CLOEXEC、O_NOCTTY…

二、 close()系统调用

int close(int fd);

说明:该函数用来关闭一个打开的文件描述符,关闭一个文件时还会释放该进程加在该文件上的所有记录锁。当一个进程终止时,内核将会自动关闭它所有打开的文件。

三、write()系统调用

ssize_t write(int fd, const void *buf, size_t nbytes);

说明:write()函数用来往打开的文件描述符fd指向的文件中写入buf指向的数据,nbytes指定要写入的数据大小。如果返回值<0则说明写入出错,譬如尝试往一个只读的文件中写入则会抛错,错误的原因系统会保存到errno变量中去。如果>0则为实际写入的数据大小。

四、read()系统调用

ssize_t read(int fd, void *buf, size_t nbytes);

说明:ead()函数用来从打开的文件描述符对应的文件中读取数据放到buf指向的内存空间中去,最多不要超过nbytes个字节,这里
的nbytes一般是buf剩余的空间大小。如read成功,则返回实际读到的字节数(由nbytes或读到文件尾决定,其中EOF宏用来判断是否到了文件尾),如果返回值小于0则表示出错,如尝试读一个没有权限读的文件时就会抛错。

文件夹操作相关系统调用

我们在了解了文件的操作函数之后,文件夹的操作相对更加容易了

函数原型 函数
int mkdir(const char *pathname, mode_t mode); 创建文件夹
int rmdir(const char *pathname); 删除文件夹
DIR *opendir(const char *pathname); 打开文件夹
struct dirent * readdir(DIR *dp); 读文件夹
int closedir(IDR *dp); 关闭文件夹
int chdir(const char * pathname); 改变工作目录

其中readdir()系统调用的struct dirent定义如下:

struct dirent 
{ 
long d_ino; /* inode number 索引节点号 */ 
 off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ 
 unsigned short d_reclen; /* length of this d_name 文件名长 */ 
 unsigned char d_type; /* the type of d_name 文件类型 */ 
 char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */ 
}

Linux下用c程序读取树莓派DS18B20探测的温度

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <fcntl.h>
  5 #include <dirent.h>
  6 #include <string.h>
  7 #include <time.h>
  8 #include <errno.h>
  9 
 10 int ds18b20_get_temperature(float *temp);
 11 
 12 int main(int argc, char *argv[])
 13 {
 14         float temp;
 15 
 16         if( ds18b20_get_temperature(&temp) <0 )
 17         {
 18                 printf("ERROR:ds18b20 get temperature failure\n");
 19                 return 1;
 20         }
 21 
 22         printf("ds18b20 get temperature: %f \n",temp);
 23         return 0;
 24 }
 25 
 26 
 27  int ds18b20_get_temperature(float *temp)
 28 {
 29         char    w1_path[50] = "/sys/bus/w1/devices/";
 30         char    chip[20];
 31         char    buf[128];
 32         DIR     *dirp;
 33         struct  dirent  *direntp;
 34          int     fd = -1;
 35         char    *ptr;
 36         float   value;
 37         int     found = 0;
 38 
 39         if( !temp)
 40         {
 41                 return -1;
 42         }
 43 
 44         if( (dirp = opendir(w1_path)) == NULL)
 45         {
 46                 printf("opendir error:%s\n",strerror(errno));
 47                 return -2;
 48         }
 49 
 50         while((direntp = readdir(dirp)) !=NULL)
 51         {
 52                 if(strstr(direntp->d_name,"28-"))
 53                 {
 54                         strcpy(chip,direntp->d_name);
 55                         found = 1;
 56                         break;
 57                 }
 58         }
 59         closedir(dirp);
 60 
 61         if( !found )
 62         {
 63                 printf("can not find ds18b20 in %s\n",w1_path);
 64                 return -3;
 65         }
 66         strncat(w1_path,chip,sizeof(w1_path)-strlen(w1_path));
 67         strncat(w1_path,"/w1_slave",sizeof(w1_path)-strlen(w1_path));
 68         if( (fd=open(w1_path, O_RDONLY)) <0)
 69         {
 70                 printf("open %s error: %s\n", w1_path, strerror(errno));
 71                  return -4;
 72         }
 73         if(read(fd, buf, sizeof(buf)) < 0)
 74         {
 75                   printf("read %s error: %s\n", w1_path, strerror(errno));
 76                   return -5;
 77         }
 78         ptr = strstr(buf, "t=");
 79         if( !ptr )
 80         {
 81                 printf("ERROR: Can not get temperature\n");
 82                 return -6;
 83         }
 84         ptr+=2;
 85         *temp = atof(ptr)/1000.0;
 86         close(fd);
 87         return 0;
 88 }

代码结果展示:
在这里插入图片描述
代码要结合上面的知识点来看会更加容易理解,如果有不懂得地方,或者有错误的地方,还请多多指教!

发布了10 篇原创文章 · 获赞 44 · 访问量 6707

猜你喜欢

转载自blog.csdn.net/makunIT/article/details/104756393