DS18B20 BBB在C代码中读取温度的问题



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

控制台中的输出看起来像:

 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

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

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 Fnn", (tempC / 1000) * 9 / 5 + 32);
            }
         close(fd);
        }
 return 0;
}

但仍然功能readTemp不会向我显示实际温度。

我在此功能中发现:

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 = 4DT_LNK && strstr(dirent->d_name, "28-") = 1的参数时,dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-")不正确

也许这将帮助解决这个问题的人,我解决了这个问题。

原始循环看起来像:

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

我将其更改为:

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

之后,一切正常。

最新更新