From e3d6dc26dd29f687abbce466421bc0a585b95352 Mon Sep 17 00:00:00 2001 From: Amir Smajevic Date: Mon, 19 Dec 2016 09:13:43 +0100 Subject: [PATCH] fixed w1 thermosensor reading algorithm in means of adding a loop and CRC check --- controller/drivers/onewire/__init__.py | 31 +++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/controller/drivers/onewire/__init__.py b/controller/drivers/onewire/__init__.py index 80c08d4..10ffc55 100644 --- a/controller/drivers/onewire/__init__.py +++ b/controller/drivers/onewire/__init__.py @@ -1,25 +1,29 @@ + import re def read_one_thermo(filename): try: NO_OF_DECIMALS = 3 - textfile = open(filename, 'r') - filetext = textfile.read() - textfile.close() - - matches = re.findall("t=(\-?)(\d+)", filetext) - sign = -1 if matches[0][0] == '-' else 1 - whole_reading = matches[0][1] - first_part = matches[0][1][0:-NO_OF_DECIMALS] - second_part = matches[0][1][-NO_OF_DECIMALS:] - first_part = '0' if first_part == '' else first_part - - return (int(first_part) + (int(second_part) / (10.0 ** NO_OF_DECIMALS) )) * sign + for x in range(0, 3): + textfile = open(filename, 'r') + filetext = textfile.read() + textfile.close() + if 'YES' in filetext: + matches = re.findall("t=(\-?)(\d+)", filetext) + sign = -1 if matches[0][0] == '-' else 1 + whole_reading = matches[0][1] + first_part = matches[0][1][0:-NO_OF_DECIMALS] + second_part = matches[0][1][-NO_OF_DECIMALS:] + first_part = '0' if first_part == '' else first_part + return (int(first_part) + (int(second_part) / (10.0 ** NO_OF_DECIMALS) )) * sign + print 'error' + print e + return -101.0 except Exception as e: print 'errror' print e return -100.0 - + def read_many_thermo(filenames): result = [] for filename in list(filenames): @@ -27,3 +31,4 @@ def read_many_thermo(filenames): continue result.append(read_one_thermo(filename)) return result +