Skip to content

Instantly share code, notes, and snippets.

@chronopoulos
Created June 29, 2021 19:09
Show Gist options
  • Save chronopoulos/da1bbdb414245288fb7ffaa495533ee2 to your computer and use it in GitHub Desktop.
Save chronopoulos/da1bbdb414245288fb7ffaa495533ee2 to your computer and use it in GitHub Desktop.
read the R_WHO_AM_I register from a Harp device
#!/usr/bin/python3 -u
import serial
import struct
import sys
# set up the serial device
if len(sys.argv) > 1:
deviceFile = sys.argv[1]
else:
deviceFile = '/dev/ttyUSB0'
harp = serial.Serial(deviceFile, 115200)
# first send a command message
msgType = 1 # read
length = 0 # tmp
address = 0 # R_WHO_AM_I
port = 255 # this device
payloadType = 1 # uint8_t
payload = 0 # don't care
checksum = 0 # tmp
#cmd = [msgType, length, address, port, payloadType, payload, checksum]
cmd = [msgType, length, address, port, payloadType, checksum] # no payload for a read?
length = len(cmd) - 2 # "the number of bytes after the field [Length]"
cmd[1] = length
checksum = 0
for v in cmd:
checksum += v
checksum = checksum % 256
cmd[-1] = checksum
print("Sending Command:", cmd)
cmd = bytes(cmd)
harp.write(cmd)
# then observe the response
while True:
result = harp.read(1)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment