Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/env python
- import serial
- import time
- # Definition of UNIX shell colors
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[93m'
- TEST = '\033[92m'
- ENDC = '\033[0m'
- RED = '\033[31m'
- serialPorts = []
- # Serial Connection settings
- # Do not change
- baudrate = 9600
- parity = serial.PARITY_NONE
- stopbits = serial.STOPBITS_ONE
- bytesize = serial.EIGHTBITS
- timeout = 1
- numberOfRespondingDevices = 0
- numberOfArduinoDevices = 2
- def establishSerialConnections():
- print(bcolors.OKGREEN + "\n--- Searching for devices ---" + bcolors.ENDC)
- counter = 0
- while (counter < 100):
- try:
- ser = serial.Serial(
- port= "/dev/ttyACM" + counter.__str__(),
- baudrate = baudrate,
- parity = parity,
- stopbits = stopbits,
- bytesize = bytesize,
- timeout = 1
- )
- serialPorts.append(ser)
- ser.close()
- ser.open()
- print ("Found open device at /dev/ttyACM" + counter.__str__())
- except IOError:
- None
- counter += 1
- print(bcolors.OKGREEN + "--- Finished searching for devices ---\n\n" + bcolors.ENDC)
- if len(serialPorts) < numberOfArduinoDevices:
- raw_input(bcolors.RED + "To few devices found, are you sure they are connected? \nPress enter to quit." + bcolors.ENDC)
- exit()
- elif len(serialPorts) > numberOfArduinoDevices:
- rawinput(bcolors.RED + "To many devices found, did you forget to adjust the numberOfArduinoDevices variable?" + bcolors.ENDC)
- sendByte1 = '1'
- sendByte2 = '2'
- def write(message, foundDevices):
- if not serialPorts:
- raw_input(bcolors.HEADER + "All ports are ready, press enter to quit. \nRun main.py next" + bcolors.ENDC)
- exit()
- counter = 0
- for ports in serialPorts:
- print(bcolors.OKGREEN + "\n--- Probing " + ports.name + " ---" + bcolors.ENDC)
- ports.write(message)
- recievedData = ports.readline()
- if recievedData == "TIL":
- #remove this serial connection
- foundDevices += 1
- print ("I see an arduino")
- del serialPorts[counter]
- elif recievedData == "PUST":
- #remove this serial connection
- foundDevices += 1
- print ("I see an arduino")
- del serialPorts[counter]
- counter += 1
- print(bcolors.OKGREEN + "--- Finished probing device ---\n\n" + bcolors.ENDC)
- establishSerialConnections()
- while True:
- write(sendByte2, numberOfRespondingDevices)
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment