# # hello.HC-SR04.py # # HC-SR04 sonar hello-world # hello.HC-SR04.py serial_port # # Neil Gershenfeld 11/15/15 # (c) Massachusetts Institute of Technology 2015 # # This work may be reproduced, modified, distributed, # performed, and displayed for any purpose. Copyright is # retained and must be preserved. The work is provided # as is; no warranty is provided, and users accept all # liability. # # from Tkinter import * import serial import sys import time NUM_BOARDS = 2 def find_framing(byte2): while 1: # find framing byte1 = byte2 byte2 = ord(ser.read()) if ((byte1 == 1) & (byte2 == 2)): break def main(): # idle routine for i in range(NUM_BOARDS): ser.flush() ser.write(str(i)) # find_framing(0) out = ord(ser.read()) if out == 1: print("strum", i) # ser.reset_input_buffer() # check command line arguments if (len(sys.argv) != 2): print "command line: hello.HC-SR04.py serial_port" sys.exit() port = sys.argv[1] # open serial port ser = serial.Serial(port, 9600) ser.setDTR() while 1: main()