使用Python在Windows中获取电池容量 [英] Getting Battery Capacity in Windows with Python

查看:243
本文介绍了使用Python在Windows中获取电池容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望弄清楚当前的电池容量和设计容量​​。

I am looking to figure out both the current Battery Capacity and the Design Capacity.

到目前为止,我可以使用的是 Win32_Battery()类不会提供我需要的所有信息(至少不在我的系统上)。我为此使用了纯Python wmi库

So far what I could get to work is using the Win32_Battery() class which doesn't give all the information I need (at least not on my system). I used the pure-python wmi library for that.

另一方面,我发现此方法有效在Python中,如何检测计算机是否由电池供电?,但是不幸的是,它也没有提供有关Capacity的任何信息。

On the other hand I found this which works In Python, how can I detect whether the computer is on battery power?, but unfortunately it doesn't provide any information on Capacity neither.

电池信息结构电池状态结构对此似乎很完美。现在,我知道我必须使用 DeviceIoControl函数这样做,我发现了 C ++代码对此进行了一些解释。

The Battery Information structure and the Battery Status structure seem perfect for this. Now I know that I have to use the DeviceIoControl function to do so and I found this C++ code that explains it a little.

我希望只使用ctypes而不使用 pywin32
如果您有一个如何在python中执行此操作的想法,请告诉我!

I would prefer something that simply uses ctypes and not the python win32api provided by pywin32. If you have an idea how to do this in python please let me know!

预先感谢。

推荐答案

Tim Golden出色的 wmi 模块将,我相信,给您您想要的一切。您只需要做几个查询就可以获取所有内容:

Tim Golden's excellent wmi module will, I believe, give you everything you want. You'll just have to do several queries to get everything:

import wmi

c = wmi.WMI()
t = wmi.WMI(moniker = "//./root/wmi")

batts1 = c.CIM_Battery(Caption = 'Portable Battery')
for i, b in enumerate(batts1):
    print 'Battery %d Design Capacity: %d mWh' % (i, b.DesignCapacity or 0)


batts = t.ExecQuery('Select * from BatteryFullChargedCapacity')
for i, b in enumerate(batts):
    print ('Battery %d Fully Charged Capacity: %d mWh' % 
          (i, b.FullChargedCapacity))

batts = t.ExecQuery('Select * from BatteryStatus where Voltage > 0')
for i, b in enumerate(batts):
    print '\nBattery %d ***************' % i
    print 'Tag:               ' + str(b.Tag)
    print 'Name:              ' + b.InstanceName

    print 'PowerOnline:       ' + str(b.PowerOnline)
    print 'Discharging:       ' + str(b.Discharging)
    print 'Charging:          ' + str(b.Charging)
    print 'Voltage:           ' + str(b.Voltage)
    print 'DischargeRate:     ' + str(b.DischargeRate)
    print 'ChargeRate:        ' + str(b.ChargeRate)
    print 'RemainingCapacity: ' + str(b.RemainingCapacity)
    print 'Active:            ' + str(b.Active)
    print 'Critical:          ' + str(b.Critical)

这当然不是跨平台的,它需要第三方资源,但确实工作很好。

This is certainly not cross-platform, and it requires a 3rd party resource, but it does work very well.

这篇关于使用Python在Windows中获取电池容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆