Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate checksum and retry #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions mh_z19.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -63,20 +64,19 @@ 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":
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])}
break
else:
if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86:
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]}
break
except:
traceback.print_exc()
return {}

def read(serial_console_untouched=False):
if not serial_console_untouched:
Expand All @@ -86,20 +86,19 @@ 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":
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]),
Expand All @@ -108,21 +107,19 @@ def read_all(serial_console_untouched=False):
}
break
else:
if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86:
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]
}
break
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:
Expand Down Expand Up @@ -228,6 +225,8 @@ 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)

if __name__ == '__main__':
Expand Down
27 changes: 13 additions & 14 deletions pypi/mh_z19/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -64,20 +65,19 @@ 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":
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])}
break
else:
if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86:
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]}
break
except:
traceback.print_exc()
return {}

def read(serial_console_untouched=False):
if not serial_console_untouched:
Expand All @@ -87,20 +87,19 @@ 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":
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]),
Expand All @@ -109,21 +108,19 @@ def read_all(serial_console_untouched=False):
}
break
else:
if len(s) >= 9 and s[0] == 0xff and s[1] == 0x86:
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]
}
break
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:
Expand Down Expand Up @@ -229,4 +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):
return struct.pack('B', 0xff - (sum(array) % 0x100) + 1)
if p_ver == '2':
array = [ord(c) for c in array]
return struct.pack('B', 0xff - (sum(array) % 0x100) + 1)