Datasheet

filename = "hrm.py"
ser = None
serio = None
verbose = True # Set this to True to see all of the incoming serial data
def usage():
"""Displays information on the command-line parameters for this script"""
print "Usage: " + filename + " <serialPort>\n"
print "For example:\n"
print " Windows : " + filename + " COM14"
print " OS X : " + filename + " /dev/tty.usbserial-DN009WNO"
print " Linux : " + filename + " /dev/ttyACM0"
return
def checkargs():
"""Validates the command-line arguments for this script"""
if len(sys.argv) < 2:
print "ERROR: Missing serialPort"
usage()
sys.exit(-1)
if len(sys.argv) > 2:
print "ERROR: Too many arguments (expected 1)."
usage()
sys.exit(-2)
def errorhandler(err, exitonerror=True):
"""Display an error message and exit gracefully on "ERROR\r\n" responses"""
print "ERROR: " + err.message
if exitonerror:
ser.close()
sys.exit(-3)
def atcommand(command, delayms=0):
"""Executes the supplied AT command and waits for a valid response"""
serio.write(unicode(command + "\n"))
if delayms:
sleep(delayms/1000)
rx = None
while rx != "OK\r\n" and rx != "ERROR\r\n":
rx = serio.readline(2000)
if verbose:
print unicode(rx.rstrip("\r\n"))
# Check the return value
if rx == "ERROR\r\n":
raise ValueError("AT Parser reported an error on '" + command.rstrip() + "'")
if __name__ == '__main__':
# Make sure we received a single argument (comPort)
checkargs()
# This will automatically open the serial port (no need for ser.open)
ser = serial.Serial(port=sys.argv[1], baudrate=9600, rtscts=True)
serio = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1),
newline='\r\n',
line_buffering=True)
© Adafruit Industries https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le Page 151 of 211