-
Notifications
You must be signed in to change notification settings - Fork 37
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
Validate checksum and retry #35
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to add any new function.
I think it can be more simple just like as follows:
if p_ver == '2':
if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86" and checksum(s[0:-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 and checksum(s[0:-1]) == s[-1]:
return {'co2': s[2]*256 + s[3]}
break
Would you please try like above? thx!
python3 のコードは以下のように変えれば動きます。(チェックサムを求める範囲を 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]} しかし、python2の方は、 |
Yes. You are right. |
Followings are also sloppy work, but working well on both p2 and p3.
and
To be frank, the latter one seems slightly clumsy, so I'm eager for your nicer fix, ths! |
7a55420
to
acbc523
Compare
checksumを以下のように修正し、他の部分の修正もなるべく元の形に近いようにしてみました。 def checksum(array):
if p_ver == '2':
array = [ord(c) for c in array]
return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) |
I really appreciate your contribution! |
Thank you! |
I'll soon update the version on PyPI (after dinner, sorry). |
すいません、元々 def checksum(array):
if p_ver == '2' and isinstance(array, str):
array = [ord(c) for c in array]
return struct.pack('B', 0xff - (sum(array) % 0x100) + 1) |
sure |
I've updated the Pypi. have fun! |
Discussions にコメントいただいた点を修正してPRを出してみます。
修正すべき点があったらご指摘ください。