在 C 代码中读取温度的 DS18b20 BBB 问题
DS18b20 BBB problem with reading temp in C code
我无法从安装在 BeagleBone black 中的 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
控制台中的输出如下所示:
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 F\n\n", (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-00000624ec04
、dirent->d_type = 4
和DT_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)
{
之后一切正常。
我无法从安装在 BeagleBone black 中的 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
控制台中的输出如下所示:
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 F\n\n", (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-00000624ec04
、dirent->d_type = 4
和DT_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)
{
之后一切正常。