12

长话短说,我正在尝试编写一个可以检查 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 

传递给它。问题是我不知道这些到底是什么。几乎文档中的每个函数都有这个问题。他们都期待我不知道如何提供的模糊的东西。

所以这里是大部分问题:有没有人有这个库的任何工作示例我可以看?或者至少有人知道如何为这些函数提供他们需要的值吗?

编辑:

由于似乎没有人对这个图书馆了解很多,有没有人知道另一种获取温度的方法?

4

3 回答 3

16

您可以通过浏览源代码了解如何使用 API。该sensors程序的代码并不太复杂,难以理解。

为了帮助您入门,这里有一个快速功能:

  • 枚举所有芯片
  • 列举了它们的所有特征
  • 打印其可读子特征的值

您可以按原样将其添加到现有的骨架类中。

(此代码仅用于演示目的,根本没有经过彻底测试。)

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-19T17:40:30.337 回答
2

Gnome 面板传感器小程序与 libsensors(和其他后端)一起使用;完整的源代码可从 Sourceforge 获得,这里:http ://sensors-applet.sourceforge.net/index.php?content=source

……特别是,libsensors 插件看起来相当清晰……我相信这应该是直接指向该代码的可用 gitweb 链接: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

于 2011-12-19T17:55:11.877 回答
0

您的代码应如下所示:

/* 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;

希望它有所帮助,尽管它不是复制/粘贴解决方案。

您可以获得 const sensors_chip_name* p_chip; 也来自上面的代码。

我相信问题实际上是const sensors_chip_name必须由传感器库返回和填充

于 2021-11-19T09:52:07.033 回答