有时候需要看看CPU温度什么的,新装机时辅助调整风扇转速,有了这个程序就可以把需要的信息输出到任意设备,比如手机或网络, 方便进一步处理。网上有很多类似的文章,抄来抄去碰一鼻子灰,捡现成总是要付出点。。。

方案

  1. Python psutil ,可以方便实现,但是关键的温度信息目前只能 linux 环境可用
  2. Python3.8.5 + openhardwaremonitor, 略麻烦点, 不过终于实现了,至于更好的方案估计有,我没找到罢了。

环境准备

谈编程不谈环境就是耍流氓,很多网上例程还是 python2 的, 坑坑坑。

  1. windows10_x64_pro
  2. python-3.8.5-amd64.exe
  3. openhardwaremonitor.dll ,来至开源软件,找不到可以支持下我发布的资源
  4. 配置 python-3.8.5 ,默认安装完成居然不好使,自己配置下
    a. 此电脑–属性–环境变量–系统变量–Path–新建如下两条目,不加的话报错到郁闷
    C:\Users\yourname\AppData\Local\Programs\Python\Python38
    C:\Users\yourname\AppData\Local\Programs\Python\Python38\Scripts

    b. python 安装一些需要的组件,有网都是自动的,等一会就行


					python -m pip install --upgrade pip setuptools wheel pythonnet
  1. 终于可以跑程序了,不过注意需要管理员权限,不然部分传感器没数据
    https://blog.csdn.net/jiangge12/article/details/108478481

例程如下:

#!/usr/bin/env python
#coding=utf-8

# CMD RunAsAdmin , cd folder , py 1.py

import clr
import time
clr.AddReference("c:/OpenHardwareMonitorLib.dll") #加载C#的库这个库网上可以下载

from OpenHardwareMonitor.Hardware import Computer

computer_tmp = Computer() #实例这这个类

computer_tmp.CPUEnabled = True #获取CPU温度时用
computer_tmp.GPUEnabled = True #获取GPU温度时用
computer_tmp.HDDEnabled = True #获取HDD温度时用
computer_tmp.RAMEnabled = True #获取RAM温度时用

computer_tmp.Open()

print (computer_tmp.Hardware[0].Identifier)  # 0 CPU
print (computer_tmp.Hardware[1].Identifier)  # 1 Ram
print (computer_tmp.Hardware[2].Identifier)  # 2 GPU
print (computer_tmp.Hardware[3].Identifier)  # 3 HDD

print (len(computer_tmp.Hardware[0].Sensors))  # 25
print (len(computer_tmp.Hardware[1].Sensors))  # 3
print (len(computer_tmp.Hardware[2].Sensors))  # 17
print (len(computer_tmp.Hardware[3].Sensors))  # 6

while True:
    for a in range(0, len(computer_tmp.Hardware[0].Sensors)):
        print (computer_tmp.Hardware[0].Sensors[a].Identifier ,computer_tmp.Hardware[0].Sensors[a].Value)

    for b in range(0, len(computer_tmp.Hardware[1].Sensors)):
        print (computer_tmp.Hardware[1].Sensors[b].Identifier ,computer_tmp.Hardware[1].Sensors[b].Value)

    for c in range(0, len(computer_tmp.Hardware[2].Sensors)):
        print (computer_tmp.Hardware[2].Sensors[c].Identifier ,computer_tmp.Hardware[2].Sensors[c].Value)

    for d in range(0, len(computer_tmp.Hardware[3].Sensors)):
        print (computer_tmp.Hardware[3].Sensors[d].Identifier ,computer_tmp.Hardware[3].Sensors[d].Value)

    computer_tmp.Hardware[0].Update()
    computer_tmp.Hardware[1].Update()
    computer_tmp.Hardware[2].Update()
    computer_tmp.Hardware[3].Update()


    time.sleep(10)
    print ()


结果大概这样的:

C:\WINDOWS\system32>py c:\1.py
/intelcpu/0
/ram
/nvidiagpu/0
/hdd/0
25
3
17
6
/intelcpu/0/load/1 100.0
/intelcpu/0/load/2 0.0
/intelcpu/0/load/3 0.0
/intelcpu/0/load/4 0.0
/intelcpu/0/load/5 0.0
/intelcpu/0/load/6 0.0
/intelcpu/0/load/0 16.66667
/intelcpu/0/temperature/0 44.0
/intelcpu/0/temperature/1 39.0
/intelcpu/0/temperature/2 39.0
/intelcpu/0/temperature/3 37.0
/intelcpu/0/temperature/4 35.0
/intelcpu/0/temperature/5 40.0
/intelcpu/0/temperature/6 44.0
/intelcpu/0/clock/1 4100.076
/intelcpu/0/clock/2 4100.076
/intelcpu/0/clock/3 3300.061
/intelcpu/0/clock/4 800.0147
/intelcpu/0/clock/5 800.0147
/intelcpu/0/clock/6 800.0147
/intelcpu/0/power/0 14.73532
/intelcpu/0/power/1 1.978229
/intelcpu/0/power/2 0.0
/intelcpu/0/power/3 0.2917577
/intelcpu/0/clock/0 100.0018
/ram/load/0 None
/ram/data/0 None
/ram/data/1 None
/nvidiagpu/0/temperature/0 46.0
/nvidiagpu/0/clock/0 135.0
/nvidiagpu/0/clock/1 405.0
/nvidiagpu/0/clock/2 270.0
/nvidiagpu/0/fan/0 0.0
/nvidiagpu/0/load/0 0.0
/nvidiagpu/0/load/1 5.0
/nvidiagpu/0/load/2 0.0
/nvidiagpu/0/load/3 0.0
/nvidiagpu/0/control/0 0.0
/nvidiagpu/0/smalldata/3 2048.0
/nvidiagpu/0/smalldata/2 194.6172
/nvidiagpu/0/smalldata/1 1853.383
/nvidiagpu/0/load/4 9.502792
/nvidiagpu/0/power/0 9.495001
/nvidiagpu/0/throughput/0 0.0
/nvidiagpu/0/throughput/1 0.0
/hdd/0/temperature/0 None
/hdd/0/level/0 None
/hdd/0/data/0 None
/hdd/0/data/1 None
/hdd/0/data/2 None
/hdd/0/load/0 None
09-10 16:31