嵌入式Linux设备读取CPU温度的方法

http://embedded.kleier.selfhost.me/raspberry_cpu_temp.php

http://blog.csdn.net/finewind/article/details/48732581

http://elinux.org/Jetson/Thermal


To observe the temperature changes with small heat capacity the timely resolution must be as high as possible. Therefore the measuring script must be efficient.

How does compiled code compare with interpreted code in terms of speed?

ARM 平台可能是这样:

# cat /sys/devices/virtual/thermal/thermal_zone0/temp
# cat /sys/class/thermal/thermal_zone0/temp

x86 平台可能是这样:

# cat /proc/acpi/thermal_zone/THRM/temperature

为了更方便读取 CPU 温度信息,我们可以编写一些小程序来提高效率。

一、Bash 脚本

cat /proc/uptime > ${OUTFILE}
for ((i=0;i<10000;++i)) ; do
    ( date "+%s.%3N" ; cat /sys/devices/virtual/thermal/thermal_zone0/temp) |\
    awk '{if (NR%2) date = $1; else print date, $1;}'
done >> ${OUTFILE}
cat /proc/uptime >> ${OUTFILE}

二、C 语言

#define _POSIX_C_SOURCE 199309L // enable interface for struct timespec ...

#include <time.h>               // struct timespec, clock_gettime
#include <stdio.h>              // FILE, fscanf, stdout, printf

const unsigned N = 10000;

typedef struct
   {struct timespec t;
    unsigned        temp;}
RecT;

int main ()
   {RecT r [N];
    for (unsigned i = 0; i < N; ++i)
       {(void) clock_gettime (CLOCK_REALTIME, &r [i].t);
        FILE *fp = fopen ("/sys/devices/virtual/thermal/thermal_zone0/temp", "r");
        (void) fscanf (fp, "%u", &r [i].temp);
        (void) fclose (fp);}
    for (unsigned i = 0; i < N; ++i)
        printf ("%lu.%06llu %u\n", r [i].t.tv_sec, (r [i].t.tv_nsec + 500ull) / 1000ull, r [i].temp);
    return 0;}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿基米东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值