Skip to content

Commit

Permalink
remove function convert_response and validate_response.
Browse files Browse the repository at this point in the history
  • Loading branch information
false-git committed Nov 8, 2021
1 parent 14156b5 commit 7a55420
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
55 changes: 29 additions & 26 deletions mh_z19.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
53 changes: 27 additions & 26 deletions pypi/mh_z19/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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()

Expand Down Expand Up @@ -213,16 +227,3 @@ def read_from_pwm(gpio=12, range=5000):

def checksum(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

0 comments on commit 7a55420

Please sign in to comment.