From db177d484da448b8874a452129e8d0246ebbe43f Mon Sep 17 00:00:00 2001 From: false Date: Mon, 8 Nov 2021 16:46:05 +0900 Subject: [PATCH 1/3] validate checksum and retry --- mh_z19.py | 70 +++++++++++++++++++-------------------- pypi/mh_z19/__init__.py | 72 ++++++++++++++++++++--------------------- 2 files changed, 69 insertions(+), 73 deletions(-) diff --git a/mh_z19.py b/mh_z19.py index 1a90f54..c98cca3 100644 --- a/mh_z19.py +++ b/mh_z19.py @@ -21,6 +21,7 @@ version = "3.0.2" pimodel = getrpimodel.model pimodel_strict = getrpimodel.model_strict() +retry_count = 3 # exception class GPIO_Edge_Timeout(Exception): @@ -63,20 +64,15 @@ def connect_serial(): def mh_z19(): try: ser = connect_serial() - while 1: + for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=ser.read(9) - - if p_ver == '2': - if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86": - return {'co2': ord(s[2])*256 + ord(s[3])} - break - else: - if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86: - return {'co2': s[2]*256 + s[3]} - break + s=convert_telegram(ser.read(9)) + + if validate_telegram(s, 0x86): + return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() + return {} def read(serial_console_untouched=False): if not serial_console_untouched: @@ -86,43 +82,30 @@ def read(serial_console_untouched=False): if not serial_console_untouched: start_getty() - if result is not None: - return result + return result def read_all(serial_console_untouched=False): if not serial_console_untouched: stop_getty() try: ser = connect_serial() - while 1: + for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=ser.read(9) - - if p_ver == '2': - if len(s) >= 9 and s[0] == "\xff" and s[1] == "\x86": - return {'co2': ord(s[2])*256 + ord(s[3]), - 'temperature': ord(s[4]) - 40, - 'TT': ord(s[4]), - 'SS': ord(s[5]), - 'UhUl': ord(s[6])*256 + ord(s[7]) - } - break - else: - if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86: - return {'co2': s[2]*256 + s[3], - 'temperature': s[4] - 40, - 'TT': s[4], - 'SS': s[5], - 'UhUl': s[6]*256 + s[7] - } - break + s=convert_telegram(ser.read(9)) + + if validate_telegram(s, 0x86): + return {'co2': s[2]*256 + s[3], + 'temperature': s[4] - 40, + 'TT': s[4], + 'SS': s[5], + 'UhUl': s[6]*256 + s[7] + } except: traceback.print_exc() if not serial_console_untouched: start_getty() - if result is not None: - return result + return {} def abc_on(serial_console_untouched=False): if not serial_console_untouched: @@ -230,6 +213,21 @@ def read_from_pwm(gpio=12, range=5000): def checksum(array): return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) +def convert_telegram(telegram): + result = telegram + if p_ver == "2": + result = [ord(c) for c in telegram] + return result + +def validate_telegram(telegram, command): + result = False + if len(telegram) == 9 and telegram[0] == 0xFF and telegram[1] == command: + csum = ord(checksum(telegram[1:8])) + result = csum == telegram[8] + else: + print(telegram) + return result + if __name__ == '__main__': # value = read() # print (value) diff --git a/pypi/mh_z19/__init__.py b/pypi/mh_z19/__init__.py index c6d8e68..538fe68 100644 --- a/pypi/mh_z19/__init__.py +++ b/pypi/mh_z19/__init__.py @@ -18,6 +18,7 @@ version = "3.0.2" pimodel = getrpimodel.model pimodel_strict = getrpimodel.model_strict() +retry_count = 3 # exception class GPIO_Edge_Timeout(Exception): @@ -64,20 +65,15 @@ def connect_serial(): def mh_z19(): try: ser = connect_serial() - while 1: + for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=ser.read(9) - - if p_ver == '2': - if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86": - return {'co2': ord(s[2])*256 + ord(s[3])} - break - else: - if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86: - return {'co2': s[2]*256 + s[3]} - break + s=convert_telegram(ser.read(9)) + + if validate_telegram(s, 0x86): + return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() + return {} def read(serial_console_untouched=False): if not serial_console_untouched: @@ -87,43 +83,30 @@ def read(serial_console_untouched=False): if not serial_console_untouched: start_getty() - if result is not None: - return result + return result def read_all(serial_console_untouched=False): if not serial_console_untouched: stop_getty() try: ser = connect_serial() - while 1: + for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=ser.read(9) - - if p_ver == '2': - if len(s) >= 9 and s[0] == "\xff" and s[1] == "\x86": - return {'co2': ord(s[2])*256 + ord(s[3]), - 'temperature': ord(s[4]) - 40, - 'TT': ord(s[4]), - 'SS': ord(s[5]), - 'UhUl': ord(s[6])*256 + ord(s[7]) - } - break - else: - if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86: - return {'co2': s[2]*256 + s[3], - 'temperature': s[4] - 40, - 'TT': s[4], - 'SS': s[5], - 'UhUl': s[6]*256 + s[7] - } - break + s=convert_telegram(ser.read(9)) + + if validate_telegram(s, 0x86): + return {'co2': s[2]*256 + s[3], + 'temperature': s[4] - 40, + 'TT': s[4], + 'SS': s[5], + 'UhUl': s[6]*256 + s[7] + } except: traceback.print_exc() if not serial_console_untouched: start_getty() - if result is not None: - return result + return {} def abc_on(serial_console_untouched=False): if not serial_console_untouched: @@ -229,4 +212,19 @@ def read_from_pwm(gpio=12, range=5000): return {'co2': int(falling -rising - CYCLE_START_HIGHT_TIME) / 2 *(range/500)} def checksum(array): - return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) \ No newline at end of file + return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) + +def convert_telegram(telegram): + result = telegram + if p_ver == "2": + result = [ord(c) for c in telegram] + return result + +def validate_telegram(telegram, command): + result = False + if len(telegram) == 9 and telegram[0] == 0xFF and telegram[1] == command: + csum = ord(checksum(telegram[1:8])) + result = csum == telegram[8] + else: + print(telegram) + return result From 14156b58efa9feb587d6b8b9c34e2b411879dcee Mon Sep 17 00:00:00 2001 From: false Date: Mon, 8 Nov 2021 18:02:10 +0900 Subject: [PATCH 2/3] fix telegram to response, don't check response command --- mh_z19.py | 24 +++++++++++------------- pypi/mh_z19/__init__.py | 24 +++++++++++------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/mh_z19.py b/mh_z19.py index c98cca3..861c699 100644 --- a/mh_z19.py +++ b/mh_z19.py @@ -66,9 +66,9 @@ def mh_z19(): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_telegram(ser.read(9)) + s=convert_response(ser.read(9)) - if validate_telegram(s, 0x86): + if validate_response(s): return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() @@ -91,9 +91,9 @@ def read_all(serial_console_untouched=False): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_telegram(ser.read(9)) + s=convert_response(ser.read(9)) - if validate_telegram(s, 0x86): + if validate_response(s): return {'co2': s[2]*256 + s[3], 'temperature': s[4] - 40, 'TT': s[4], @@ -213,19 +213,17 @@ def read_from_pwm(gpio=12, range=5000): def checksum(array): return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) -def convert_telegram(telegram): - result = telegram +def convert_response(response): + result = response if p_ver == "2": - result = [ord(c) for c in telegram] + result = [ord(c) for c in response] return result -def validate_telegram(telegram, command): +def validate_response(response): result = False - if len(telegram) == 9 and telegram[0] == 0xFF and telegram[1] == command: - csum = ord(checksum(telegram[1:8])) - result = csum == telegram[8] - else: - print(telegram) + if len(response) == 9 and response[0] == 0xFF: + csum = ord(checksum(response[1:8])) + result = csum == response[8] return result if __name__ == '__main__': diff --git a/pypi/mh_z19/__init__.py b/pypi/mh_z19/__init__.py index 538fe68..160f29a 100644 --- a/pypi/mh_z19/__init__.py +++ b/pypi/mh_z19/__init__.py @@ -67,9 +67,9 @@ def mh_z19(): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_telegram(ser.read(9)) + s=convert_response(ser.read(9)) - if validate_telegram(s, 0x86): + if validate_response(s): return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() @@ -92,9 +92,9 @@ def read_all(serial_console_untouched=False): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_telegram(ser.read(9)) + s=convert_response(ser.read(9)) - if validate_telegram(s, 0x86): + if validate_response(s): return {'co2': s[2]*256 + s[3], 'temperature': s[4] - 40, 'TT': s[4], @@ -214,17 +214,15 @@ def read_from_pwm(gpio=12, range=5000): def checksum(array): return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) -def convert_telegram(telegram): - result = telegram +def convert_response(response): + result = response if p_ver == "2": - result = [ord(c) for c in telegram] + result = [ord(c) for c in response] return result -def validate_telegram(telegram, command): +def validate_response(response): result = False - if len(telegram) == 9 and telegram[0] == 0xFF and telegram[1] == command: - csum = ord(checksum(telegram[1:8])) - result = csum == telegram[8] - else: - print(telegram) + if len(response) == 9 and response[0] == 0xFF: + csum = ord(checksum(response[1:8])) + result = csum == response[8] return result From acbc523be3ec24e89ceb8bb040c806423eb3b605 Mon Sep 17 00:00:00 2001 From: false Date: Mon, 8 Nov 2021 20:48:47 +0900 Subject: [PATCH 3/3] remove function convert_response and validate_response. --- mh_z19.py | 55 ++++++++++++++++++++++------------------- pypi/mh_z19/__init__.py | 55 ++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/mh_z19.py b/mh_z19.py index 861c699..d40c039 100644 --- a/mh_z19.py +++ b/mh_z19.py @@ -66,10 +66,14 @@ def mh_z19(): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_response(ser.read(9)) - - if validate_response(s): - return {'co2': s[2]*256 + s[3]} + s=ser.read(9) + + if p_ver == '2': + if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[1:-1]) == s[-1]: + return {'co2': ord(s[2])*256 + ord(s[3])} + else: + if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86 and ord(checksum(s[1:-1])) == s[-1]: + return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() return {} @@ -91,15 +95,25 @@ def read_all(serial_console_untouched=False): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_response(ser.read(9)) - - if validate_response(s): - return {'co2': s[2]*256 + s[3], - 'temperature': s[4] - 40, - 'TT': s[4], - 'SS': s[5], - 'UhUl': s[6]*256 + s[7] - } + s=ser.read(9) + + if p_ver == '2': + if len(s) >= 9 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[1:-1]) == s[-1]: + return {'co2': ord(s[2])*256 + ord(s[3]), + 'temperature': ord(s[4]) - 40, + 'TT': ord(s[4]), + 'SS': ord(s[5]), + 'UhUl': ord(s[6])*256 + ord(s[7]) + } + break + else: + if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86 and ord(checksum(s[1:-1])) == s[-1]: + return {'co2': s[2]*256 + s[3], + 'temperature': s[4] - 40, + 'TT': s[4], + 'SS': s[5], + 'UhUl': s[6]*256 + s[7] + } except: traceback.print_exc() @@ -211,21 +225,10 @@ def read_from_pwm(gpio=12, range=5000): return {'co2': int(falling -rising - CYCLE_START_HIGHT_TIME) / 2 *(range/500)} def checksum(array): + if p_ver == '2': + array = [ord(c) for c in array] return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) -def convert_response(response): - result = response - if p_ver == "2": - result = [ord(c) for c in response] - return result - -def validate_response(response): - result = False - if len(response) == 9 and response[0] == 0xFF: - csum = ord(checksum(response[1:8])) - result = csum == response[8] - return result - if __name__ == '__main__': # value = read() # print (value) diff --git a/pypi/mh_z19/__init__.py b/pypi/mh_z19/__init__.py index 160f29a..9c15ea1 100644 --- a/pypi/mh_z19/__init__.py +++ b/pypi/mh_z19/__init__.py @@ -67,10 +67,14 @@ def mh_z19(): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_response(ser.read(9)) - - if validate_response(s): - return {'co2': s[2]*256 + s[3]} + s=ser.read(9) + + if p_ver == '2': + if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[1:-1]) == s[-1]: + return {'co2': ord(s[2])*256 + ord(s[3])} + else: + if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86 and ord(checksum(s[1:-1])) == s[-1]: + return {'co2': s[2]*256 + s[3]} except: traceback.print_exc() return {} @@ -92,15 +96,25 @@ def read_all(serial_console_untouched=False): ser = connect_serial() for retry in range(retry_count): result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79") - s=convert_response(ser.read(9)) - - if validate_response(s): - return {'co2': s[2]*256 + s[3], - 'temperature': s[4] - 40, - 'TT': s[4], - 'SS': s[5], - 'UhUl': s[6]*256 + s[7] - } + s=ser.read(9) + + if p_ver == '2': + if len(s) >= 9 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[1:-1]) == s[-1]: + return {'co2': ord(s[2])*256 + ord(s[3]), + 'temperature': ord(s[4]) - 40, + 'TT': ord(s[4]), + 'SS': ord(s[5]), + 'UhUl': ord(s[6])*256 + ord(s[7]) + } + break + else: + if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86 and ord(checksum(s[1:-1])) == s[-1]: + return {'co2': s[2]*256 + s[3], + 'temperature': s[4] - 40, + 'TT': s[4], + 'SS': s[5], + 'UhUl': s[6]*256 + s[7] + } except: traceback.print_exc() @@ -212,17 +226,6 @@ def read_from_pwm(gpio=12, range=5000): return {'co2': int(falling -rising - CYCLE_START_HIGHT_TIME) / 2 *(range/500)} def checksum(array): + if p_ver == '2': + array = [ord(c) for c in array] return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) - -def convert_response(response): - result = response - if p_ver == "2": - result = [ord(c) for c in response] - return result - -def validate_response(response): - result = False - if len(response) == 9 and response[0] == 0xFF: - csum = ord(checksum(response[1:8])) - result = csum == response[8] - return result