ラズベリーパイのためのDS18B20温度プローブをプログラミングLinuxの

ディレクトリ

(A)プロジェクト
(II)コード、およびデバッグ
(C)概要

(A)プロジェクト

     DS18B20デジタル温度センサは小型、低コストのハードウェア、耐干渉能力、高い精度を有するデジタル出力信号である、使用され、この実験は、プロジェクトをサポートは、ラズベリーパーティDS18B20ライン温度センサに基づいている。1(回線プロトコル)、及び、プログラミングすることによって達成温度を検出します。

DQは、デジタル信号の入力/出力端子であり、
電源のGND、
VCCは、外部電源入力端子であり、
二つの接続ポートは、次の通りである前記
接地端子ラズベリー派(ピン06フィート)を-------- ----ポートGND DS18B20の、
ラズベリーパイは04端子(ピン07フィート)-------------- DS18B20 DQのポートをGPIO、
ラズベリーパイ3.3V DS18B20の端子(ピン01ピン)-------------接続ポートVCC。

(B)コードとデバッグ

1.ラズベリーパイSecurtCRTリモートサーバにログインし、ルートディレクトリにw1_slaveファイルに以下のように、ファイルの場所を探します。
ファイルの場所
最初の呼び出しはオープン()、(読み取り)、クローズ() 三つの基本文書I / O機能は、ファイルの内容を読むには:

  int main(int argc, char **argv)
  {    
     int fd;
       
     char buf[128];
      
     fd=open("/sys/bus/w1/devices/28-041731f7c0ff/w1_slave",O_RDONLY);
      
     memset(buf,0,sizeof(buf);   //初始化buf的内存,避免结果出现多余的字符
     
     read(fd,buf,sizeof(buf));   
     
     printf("buf: %s\n"buf);
 
     close(fd);
 }

結果は次の通りです:
ここに画像を挿入説明
あなたは、他の余分な文字をフィルタリングしたい場合2.は、私たちが呼び出す必要がありはstrstr() )(ダイレクトプリントのprintfを使用し、「T =」の文字列を検索する機能を、そして、ビット浮動小数点温度は、順番に読み取ることがあるため我々は容易呼び出す必要がありatof()すなわち(フロートタイプ:ASCIIコードに:変換、F):floatに対応したASCII文字列を変換する機能を。
次のようにコードの最適化の結果は以下の通りでした。

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main(int argc, char **argv)
{
     int fd;
     char buf[128];
     char *ptr;
     float temp;
     
     fd=open("/sys/bus/w1/devices/28-041731f7c0ff/w1_slave",O_RDONLY);
    
     memset(buf,0,sizeof(buf);   
     
     read(fd,buf,sizeof(buf));
    
     printf("buf: %s\n"buf);
     
     ptr=strstr(buf,"t=");
     if(!ptr)
     {
         printf("can not find t=string\n");
         return -1;
     }
     
     ptr +=2;
    
     printf("ptr= %s\n",ptr);
    
     temp = atof(ptr)/1000;
    
     printf("temperature: %f\n",temp);

     close(fd);
}

結果は次の通りされています。
ここに画像を挿入説明
私たちが使用して、さらに最適化するためのコードが必要ですので、実際にはDS18B20チップのシリアル番号は3に変化しているが、「28-」に始めているSTRSTR()「/ SYS /バス/ W1に一致するように機能を/文字列28-」、再利用装置/「を含むパス」strncat()関数が低い経路にスプライスされ、その結果に関わらずチップシリアル番号の変化が正確に対応する経路に配置することができる方法のを

次のように最終的なコードです。

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<dirent.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
    int           fd=-1;
    char          buf[128];
    char          *ptr = NULL;
    float         temp;
    DIR           *dirp=NULL;
    struct dirent *direntp = NULL;
    char          w1_path[32]="/sys/bus/w1/devices/";
    char          chip_sn[32];
    int           found=0;

    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));
            found=1;
        }               

    }
    closedir(dirp);
    if(!found)
    {
        printf("can not find ds18b20 chipset\n");
        return -2;
    }
    strncat(w1_path,chip_sn,sizeof(w1_path)-strlen(w1_path));

    strncat(w1_path,"w1_slave",sizeof(w1_path)-strlen(w1_path));

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



    fd=open("w1_path",O_RDONLY);
    if(fd<0)
    {
        printf("open file failure: %s\n",strerror(errno));
        return -3;
    }
    memset(buf,0,sizeof(buf));   

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

    printf("buf: %s\n",buf);

    ptr=strstr(buf,"t=");
    if(!ptr)
    {
        printf("can not find t=string\n");
        return -5;
    }

    ptr +=2;

    printf("ptr= %s\n",ptr);

    temp = atof(ptr)/1000;

    printf("temperature: %f\n",temp);

    close(fd);

    return 0;
}

(C)概要

     このプロジェクトは、ラズベリーパイとDS18B20のハードウェアの知識のいくつかの習得を必要とするが、主な課題は、LinuxのファイルIOシステムコールの下で理解することであり、一部のCプログラミング言語能力、また展示会を喜ばせる場所がありません、私は私の要約はあなたに役立つことが期待しています。

出版元の記事 ウォンの賞賛1 ビュー20

おすすめ

転載: blog.csdn.net/weixin_45930204/article/details/104583400