evenjc

connectionFlusher.py

Apr 5th, 2019
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. #!/user/bin/env python
  2.  
  3. import serial
  4. import time
  5.  
  6. # Definition of UNIX shell colors
  7. class bcolors:
  8.     HEADER = '\033[95m'
  9.     OKBLUE = '\033[94m'
  10.     OKGREEN = '\033[93m'
  11.     TEST = '\033[92m'
  12.     ENDC = '\033[0m'
  13.     RED = '\033[31m'
  14.    
  15. serialPorts = []
  16.  
  17. # Serial Connection settings
  18. # Do not change
  19. baudrate = 9600
  20. parity = serial.PARITY_NONE
  21. stopbits = serial.STOPBITS_ONE
  22. bytesize = serial.EIGHTBITS
  23. timeout = 1
  24.  
  25. numberOfRespondingDevices = 0
  26. numberOfArduinoDevices = 2
  27.  
  28. def establishSerialConnections():
  29.     print(bcolors.OKGREEN + "\n--- Searching for devices ---" + bcolors.ENDC)
  30.     counter = 0
  31.     while (counter < 100):
  32.         try:
  33.             ser = serial.Serial(
  34.                 port= "/dev/ttyACM" + counter.__str__(),
  35.                 baudrate = baudrate,
  36.                 parity = parity,
  37.                 stopbits = stopbits,
  38.                 bytesize = bytesize,
  39.                 timeout = 1
  40.             )
  41.             serialPorts.append(ser)
  42.             ser.close()
  43.             ser.open()
  44.             print ("Found open device at /dev/ttyACM" + counter.__str__())
  45.            
  46.         except IOError:
  47.             None
  48.        
  49.         counter += 1
  50.     print(bcolors.OKGREEN + "--- Finished searching for devices ---\n\n" + bcolors.ENDC)
  51.     if len(serialPorts) < numberOfArduinoDevices:
  52.         raw_input(bcolors.RED + "To few devices found, are you sure they are connected? \nPress enter to quit." + bcolors.ENDC)
  53.         exit()
  54.     elif len(serialPorts) > numberOfArduinoDevices:
  55.         rawinput(bcolors.RED + "To many devices found, did you forget to adjust the numberOfArduinoDevices variable?" + bcolors.ENDC)
  56. sendByte1 = '1'
  57. sendByte2 = '2'
  58.  
  59.  
  60. def write(message, foundDevices):
  61.  
  62.     if not serialPorts:
  63.         raw_input(bcolors.HEADER + "All ports are ready, press enter to quit. \nRun main.py next" + bcolors.ENDC)
  64.         exit()
  65.  
  66.     counter = 0
  67.     for ports in serialPorts:
  68.         print(bcolors.OKGREEN + "\n--- Probing " + ports.name + " ---" + bcolors.ENDC)
  69.         ports.write(message)
  70.         recievedData = ports.readline()
  71.         if recievedData == "TIL":
  72.             #remove this serial connection
  73.             foundDevices += 1
  74.             print ("I see an arduino")
  75.             del serialPorts[counter]
  76.         elif recievedData == "PUST":
  77.             #remove this serial connection
  78.             foundDevices += 1
  79.             print ("I see an arduino")
  80.             del serialPorts[counter]
  81.            
  82.         counter += 1
  83.     print(bcolors.OKGREEN + "--- Finished probing device ---\n\n" + bcolors.ENDC)
  84. establishSerialConnections()
  85.  
  86. while True:        
  87.     write(sendByte2, numberOfRespondingDevices)
  88.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment