Skip to content

Instantly share code, notes, and snippets.

@sfalexrog
Created May 17, 2019 10:36
Show Gist options
  • Save sfalexrog/b4d0609920465bd55af4fc9f2e56d8e4 to your computer and use it in GitHub Desktop.
Save sfalexrog/b4d0609920465bd55af4fc9f2e56d8e4 to your computer and use it in GitHub Desktop.
tfmini with checksum
# -*- 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