长话短说,我试图写一个应用程序,可以检查cpu温度。使用libsensors(3)手册页,我至少能够得到libsensors_version编号。到目前为止,这是我的代码:
#include <sensors/sensors.h>
#include "SensorData.h"
#include <string>
#include <sstream>
using namespace std;
SensorData::SensorData()
{
sensors_init(NULL);
}
SensorData::~SensorData()
{
sensors_cleanup();
}
string SensorData::GetVersion()
{
ostringstream Converter;
Converter<<"Version: "<<libsensors_version;
return Converter.str();
}
void SensorData::FetchTemp()
{
//sensors_get_value()
}我知道sensors_get_value期望的手册页
const sensors_chip_name *name
int subfeat_nr
double *value 传递给它。问题是我不知道这些到底是什么。几乎所有文档中的函数都有这个问题。他们都期待着我不知道如何提供的模糊的东西。
因此,主要问题是:是否有人可以使用工作的示例来查看这个库?或者,至少有人知道如何赋予这些函数所需的值吗?
编辑:
既然似乎没有人对这个图书馆了解很多,那么有没有人知道另一种获取温度的方法呢?
发布于 2011-12-19 17:40:30
您可以通过浏览源代码了解如何使用API。sensors程序的代码并不太复杂。
为了让您开始,下面是一个快速的函数:
枚举所有chips
。
您可以按原样将其添加到现有的骨架类中。
(这段代码仅用于演示,根本没有经过彻底的测试。)
void SensorData::FetchTemp()
{
sensors_chip_name const * cn;
int c = 0;
while ((cn = sensors_get_detected_chips(0, &c)) != 0) {
std::cout << "Chip: " << cn->prefix << "/" << cn->path << std::endl;
sensors_feature const *feat;
int f = 0;
while ((feat = sensors_get_features(cn, &f)) != 0) {
std::cout << f << ": " << feat->name << std::endl;
sensors_subfeature const *subf;
int s = 0;
while ((subf = sensors_get_all_subfeatures(cn, feat, &s)) != 0) {
std::cout << f << ":" << s << ":" << subf->name
<< "/" << subf->number << " = ";
double val;
if (subf->flags & SENSORS_MODE_R) {
int rc = sensors_get_value(cn, subf->number, &val);
if (rc < 0) {
std::cout << "err: " << rc;
} else {
std::cout << val;
}
}
std::cout << std::endl;
}
}
}
}发布于 2011-12-19 17:55:11
Gnome面板传感器applet与libsensors (和其他后端)一起工作;完整的源代码可以从Sourceforge获得,这里是:http://sensors-applet.sourceforge.net/index.php?content=source
…特别是,libsensors插件看起来相当容易读懂…。我认为这应该是直接指向代码:http://sensors-applet.git.sourceforge.net/git/gitweb.cgi?p=sensors-applet/sensors-applet;a=blob;f=plugins/libsensors/libsensors-plugin.c;h=960c19f4c36902dee4e20b690f2e3dfe6c715279;hb=HEAD的一个可用的gitweb链接。
发布于 2021-11-19 09:52:07
您的代码应该如下所示:
/* Read /etc/sensors.d to get the names or use code in above post */
std::string chip_name = "CHIP_NAME-*";
/* Here you get the path to the chip you want to read */
int rc;
sensors_chip_name name;
rc = sensors_parse_chip_name(chip_name.c_str(), &name);
/* Check rc != 0 */
/* Here you get the internal structure */
int nr = 0; //Here I silently assume you have only one chip to read
const sensors_chip_name* p_chip;
p_chip = sensors_get_detected_chips(&name, &nr);
/* Check p_chip != 0 */
/* Now you read the value - this you can repeat in some for/while cycle */
double val;
/* Replace the "1" with the feature you want to read */
rc = sensors_get_value(p_chip, 1, &val);
std::cout << "Now I can use sensors library " << val << std::endl;希望它能有所帮助,尽管它不是复制/粘贴解决方案。
您也可以从上面的post代码中获得const sensors_chip_name* p_chip;。
我认为问题在于,事实上,const sensors_chip_name必须由传感器库返回和填充。
https://stackoverflow.com/questions/8556551
复制相似问题