DS18b20 BBB问题,C语言中读取温度

时间:2019-05-05 11:31:38

标签: c embedded beagleboneblack

我无法从安装在BeagleBone黑色中的DS18B20温度传感器读取温度的结果。 我正在使用w1.dts,例如:http://www.bonebrews.com/temperature-monitoring-with-the-ds18b20-on-a-beaglebone-black/ 我的C代码来自这里:http://bradsmc.blogspot.com/2014/06/c-program-to-read-multiple-ds18b20-1.html

控制台中的输出如下所示:

SELECT YEAR(STR_TO_DATE(date, '%Y-%d-%m')) AS year_A, 
       MONTH(STR_TO_DATE(date, '%Y-%d-%m')) AS month_A, 
       count(*) AS number_A,
       count(CASE WHEN state = 0 THEN 1 END) AS number_B,
       count(CASE WHEN state = 1 THEN 1 END) AS number_C
FROM A_transaction 
GROUP BY year_A, month_A

我可以使用命令

读取温度
 Found 1 devices
 <blank line>
 <blank line>

我的读物是:

cat /sys/devices/w1_bus_master1/28-00000624ec04/w1_slave

我已经将路径代码更改为:

74 01 4b 46 7f ff 0c 10 55 : crc=55 YES
74 01 4b 46 7f ff 0c 10 55 t=23250

但是仍然可以使用readtemp来显示实际温度。

我发现此功能是

char path[] = "/sys/devices/w1_bus_master1/";
sprintf(newDev->devPath, "%s/%s/w1_slave", path, newDev->devID);




int8_t readTemp(struct ds18b20 *d) 
{
 while(d->next != NULL)
    {
    d = d->next;
    int fd = open(d->devPath, O_RDONLY);

    if(fd == -1)
        {
          perror ("Couldn't open the w1 device.");
                 return 1;
        }
    char buf[256];
    ssize_t numRead;
    while((numRead = read(fd, buf, 256)) > 0) 
            {
            strncpy(d->tempData, strstr(buf, "t=") + 0, 4);
            float tempC = strtof(d->tempData, NULL);
            printf("Device: %s  - ", d->devID);
            printf("Temp: %.3f C  ", tempC / 1000);
            printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32);
            }
         close(fd);
        }
 return 0;
}

结果:

int8_t findDevices(struct ds18b20 *d)
{
    DIR *dir;
        struct dirent *dirent;
        struct ds18b20 *newDev;
        char path[] = "/sys/devices/w1_bus_master1";
        int8_t i = 0;
        dir = opendir(path);
        if (dir != NULL)
        {

            while ((dirent = readdir(dir)))
            {

                // 1-wire devices are links beginning with 28-
                if(dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL)
                {
                    printf("DevId bef");
                    newDev = malloc(sizeof(struct ds18b20));
                    strcpy(newDev->devID, dirent->d_name);
                    // Assemble path to OneWire device
                    sprintf(newDev->devPath, "%s/%s/w1_slave", path, newDev->devID);
                    i++;

                    newDev->next = 0;
                    d->next = newDev;
                    d = d->next;
                }
                else
                {

                }
            }
            (void) closedir(dir);
        }
        else
        {
            perror ("Couldn't open the w1 devices directory");
            return 1;
        }
        return 1;
}

if(dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL) d_name = 28-00000624ec04dirent->d_type = 4的参数时,DT_LNK && strstr(dirent->d_name, "28-") = 1不正确

1 个答案:

答案 0 :(得分:0)

也许这会帮助有人解决这个问题,因为我解决了这个问题。

原始循环如下:

   if(dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL)

我将其更改为:

 if(dirent->d_type == 4)
        {
          if (strstr(dirent->d_name, "28-") != NULL)
          {

此后,一切正常。