Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 用lm传感器解析芯片名称_C_Linux_Lm Sensors - Fatal编程技术网

C 用lm传感器解析芯片名称

C 用lm传感器解析芯片名称,c,linux,lm-sensors,C,Linux,Lm Sensors,首先,为了避免XY问题,我的主要目标是获取CPU包的温度,以便记录并绘制它。我想我应该用lm传感器来做这个。我尝试的解决方案是将我想要的传感器放入sensors\u chip\u name变量中,然后从那里读取它报告的温度。在寻求这个解决方案的过程中,我感到困惑,因此我在这里发表了文章。因此: 我在理解如何使用C中的lm传感器获取特定的传感器时遇到了一些困难 我做的第一件事是在终端中运行传感器,以获得系统中所有传感器的列表,然后确定我感兴趣的传感器,即coretemp-isa-0000 接下来我

首先,为了避免XY问题,我的主要目标是获取CPU包的温度,以便记录并绘制它。我想我应该用lm传感器来做这个。我尝试的解决方案是将我想要的传感器放入
sensors\u chip\u name
变量中,然后从那里读取它报告的温度。在寻求这个解决方案的过程中,我感到困惑,因此我在这里发表了文章。因此:

我在理解如何使用C中的lm传感器获取特定的传感器时遇到了一些困难

我做的第一件事是在终端中运行
传感器
,以获得系统中所有传感器的列表,然后确定我感兴趣的传感器,即
coretemp-isa-0000

接下来我要做的就是开始编写我的C程序,我在这一点上感到困惑。我正试图让
cpu\u package\u name
变量引用
coretep-isa-0000
。为了确保我得到了正确的芯片,一旦它被加载,我就想在后面显示它的名字,但是它没有显示任何东西。我正在使用的代码:

sensors_chip_name cpu_package_name;
int parse_error;
if (parse_error = sensors_parse_chip_name("coretemp-isa-0000", &cpu_package_name)) {
    eprintf("error getting chip name: %s\n", sensors_strerror(parse_error));
    goto cleanup;
} else {    
    #define BUFSIZE 200
    char buf[BUFSIZE];
    sensors_snprintf_chip_name(buf, BUFSIZE, &cpu_package_name);
    printf("got chip: %s\n", buf);
}
输出:
got芯片:

现在我注意到,如果我用
cpu\u package\u name
作为第一个参数向
sensors\u get\u detected\u chips
添加一个调用,然后只取它产生的第一个结果,并尝试显示芯片的名称,我确实会得到预期的
coretmp-isa-0000

这是我正在使用的新代码,正确显示我想要的传感器名称:

const sensors_chip_name *chip;
sensors_chip_name cpu_package_name;
int parse_error;
if (parse_error = sensors_parse_chip_name("coretemp-isa-0000", &cpu_package_name)) {
    eprintf("error getting chip name: %s\n", sensors_strerror(parse_error));
    goto cleanup;
} else {
    int chipno = 0;

    /* don't iterate, just take the first result for testing */
    chip = sensors_get_detected_chips(&cpu_package_name, &chipno);

    #define BUFSIZE 200
    char buf[BUFSIZE];
    sensors_snprintf_chip_name(buf, BUFSIZE, chip);
    printf("got chip: %s\n", buf);
}
输出:
got芯片:coretep-isa-0000

我的问题是:
sensors\u get\u detected\u chips
调用
sensors\u snprintf\u chip\u name
以产生正确的结果,尽管
coretmp-isa-0000
调用
sensors\u parse\u chip\u name
中已经正确提供了
?我很困惑,因为
sensors\u parse\u chip\u name
sensors\u get\u detected\u chips
的结果都存储在同一类型
sensors\u chip\u name
,所以在我看来,中间结果似乎与最终结果存储在同一类型

我的理解是,
sensors\u get\u detected\u chips
是在while循环中使用的,所以直觉上,我需要
strcmp
它的结果来确保我得到正确的传感器,因为它使
sensors\u parse\u chip\u name
的结果似乎可以指向许多传感器。我如何精确定位一个特定的传感器,就像现在我想硬编码它的名称一样?这是正确的方法,还是我错过了什么