Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/env python
- import serial
- import time
- 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():
- 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:
- print ("No Arduino at this port")
- counter += 1
- sendByte1 = '1'
- sendByte2 = '2'
- def write(message):
- print ('Skriver ' + message + ' til Arduino')
- counter = 0
- if not serialPorts:
- print ("All out of ports")
- exit()
- for ports in serialPorts:
- ports.write(message)
- print ('Leser fra Arduino')
- recievedData = ports.readline()
- print recievedData
- if recievedData == "TIL":
- #remove this serial connection
- print ("I see an arduino")
- del serialPorts[counter]
- elif recievedData == "PUST":
- #remove this serial connection
- print ("I see an arduino")
- del serialPorts[counter]
- counter += 1
- def read():
- counter = 0
- if not serialPorts:
- print ("All out of ports")
- exit()
- for ports in serialPorts:
- print ('Leser fra Arduino')
- recievedData = ports.readline()
- print recievedData
- if recievedData == "TIL":
- #remove this serial connection
- print ("I see an arduino")
- del serialPorts[counter]
- elif recievedData == "PUST":
- #remove this serial connection
- print ("I see an arduino")
- del serialPorts[counter]
- counter += 1
- establishSerialConnections()
- while True:
- print ("remaining ports are: ")
- for pots in serialPorts:
- print pots.name
- write(sendByte2)
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment