evenjc

slett

Mar 28th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. #!/user/bin/env python
  2.  
  3. import serial
  4. import time
  5.  
  6. serialPorts = []
  7.  
  8. # Serial Connection settings
  9. # Do not change
  10. baudrate = 9600
  11. parity = serial.PARITY_NONE
  12. stopbits = serial.STOPBITS_ONE
  13. bytesize = serial.EIGHTBITS
  14. timeout = 1
  15.  
  16. numberOfRespondingDevices = 0
  17. numberOfArduinoDevices = 2
  18.  
  19. def establishSerialConnections():
  20.     counter = 0
  21.     while (counter < 100):
  22.         try:
  23.             ser = serial.Serial(
  24.                 port= "/dev/ttyACM" + counter.__str__(),
  25.                 baudrate = baudrate,
  26.                 parity = parity,
  27.                 stopbits = stopbits,
  28.                 bytesize = bytesize,
  29.                 timeout = 1
  30.             )
  31.             serialPorts.append(ser)
  32.             ser.close()
  33.             ser.open()
  34.             print ("Found open device at /dev/ttyACM" + counter.__str__())
  35.            
  36.         except IOError:
  37.             print ("No Arduino at this port")
  38.        
  39.         counter += 1
  40.  
  41. sendByte1 = '1'
  42. sendByte2 = '2'
  43.  
  44.  
  45. def write(message):
  46.     print ('Skriver ' + message + ' til Arduino')
  47.     counter = 0
  48.     if not serialPorts:
  49.         print ("All out of ports")
  50.         exit()
  51.     for ports in serialPorts:
  52.         ports.write(message)
  53.         print ('Leser fra Arduino')
  54.         recievedData = ports.readline()
  55.         print recievedData
  56.         if recievedData == "TIL":
  57.             #remove this serial connection
  58.             print ("I see an arduino")
  59.             del serialPorts[counter]
  60.         elif recievedData == "PUST":
  61.             #remove this serial connection
  62.             print ("I see an arduino")
  63.             del serialPorts[counter]
  64.            
  65.         counter += 1
  66.  
  67. def read():
  68.     counter = 0
  69.     if not serialPorts:
  70.         print ("All out of ports")
  71.         exit()
  72.     for ports in serialPorts:
  73.         print ('Leser fra Arduino')
  74.         recievedData = ports.readline()
  75.         print recievedData
  76.         if recievedData == "TIL":
  77.             #remove this serial connection
  78.             print ("I see an arduino")
  79.             del serialPorts[counter]
  80.         elif recievedData == "PUST":
  81.             #remove this serial connection
  82.             print ("I see an arduino")
  83.             del serialPorts[counter]
  84.            
  85.         counter += 1
  86.  
  87. establishSerialConnections()
  88.  
  89. while True:
  90.     print ("remaining ports are: ")
  91.     for pots in serialPorts:
  92.         print pots.name
  93.        
  94.     write(sendByte2)
  95.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment