Created
May 17, 2019 10:36
-
-
Save sfalexrog/b4d0609920465bd55af4fc9f2e56d8e4 to your computer and use it in GitHub Desktop.
tfmini with checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -* | |
import serial | |
ser = serial.Serial("/dev/ttyAMA0", 115200) | |
def getTFminiData(): | |
while True: | |
count = ser.in_waiting | |
if count > 8: | |
recv = ser.read(9) | |
ser.reset_input_buffer() | |
checksum = 0 | |
for j in range(0, 8): | |
checksum = checksum + ord(recv[j]) | |
checksum = checksum % 256 | |
if checksum == ord(recv[8]): | |
if recv[0] == 'Y' and recv[1] == 'Y': #python2 | |
lowD = int(recv[2].encode('hex'), 16) | |
highD = int(recv[3].encode('hex'), 16) | |
lowS = int(recv[4].encode('hex'), 16) | |
highS = int(recv[5].encode('hex'), 16) | |
distance = lowD + highD * 256 | |
strength = lowS + highS * 256 | |
print(distance, strength) | |
if __name__ == '__main__': | |
try: | |
if ser.is_open == False: | |
ser.open() | |
getTFminiData() | |
except KeyboardInterrupt: # Ctrl+C | |
if ser != None: | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment