|
发表于 2012-8-25 16:52:53
|
显示全部楼层
好东西,帮顶!分享个自己写的读取温度小程序。- import os
- import re
- class ds18b20:
- path = "/sys/bus/w1/devices/"
- def __init__(self):
- pattern = re.compile(r'''
- (^\\d{2}) #
- (-)
- ''',re.VERBOSE)
- lines = os.popen("ls "+ self.path).readlines()
- for line in lines:
- l = pattern.search(line)
- if not l is None:
- self.path = '%s%s/%s' % (self.path,l.string[:-1],"w1_slave")
- def get_temperature(self):
- lines = os.popen("cat "+ self.path).readlines()
- pattern = re.compile(r'''
- (t=\\d{5})
- ''',re.VERBOSE)
- for line in lines:
- l = pattern.search(line)
- if not l is None:
- vaule = line[l.start():l.end()]
- return float(vaule[2:])/1000
- return None
- if __name__ == "__main__":
- ds = ds18b20()
- print ds.get_temperature()
复制代码 |
|