예제 #1
0
 def __init__(self):
     os.system("sudo hciconfig hci0 piscan")
     self.arduino = ArduinoInterface(port='COM13',baud_rate=115200)
     self.android = BluetoothInterface()
     self.pc = PCInterface(host='',port='5000')
   
     pass
예제 #2
0
	def threads_create(self):
		try: 		
			to_arduino_queue = Queue()
			from_arduino_queue = Queue()
			
			to_android_queue = Queue()
			from_android_queue = Queue()

			to_pc_queue = Queue()
			
			#to_algo_queue = Queue()
			#from_algo_queue = Queue()
			
			##think theres problem with the queue passed in. I think it should also pcqueue.
			##t1 need all the queue since it is at the center of the communication.
			t1 = Thread(target=self.PC_Thread, args=(to_arduino_queue,from_arduino_queue, to_pc_queue, to_android_queue,'', 5000))

			serial_ports = ArduinoInterface.list_ports()
			for port in serial_ports:
				print(port)
				if 'ACM' in port or 'COM' in port:
					to_connect = port
			

			t2 = Thread(target=self.Arduino_Thread, args=(to_arduino_queue,from_arduino_queue,to_connect, 115200))
			
			


			read_android_thread = Thread(target=self.read_android, args=([to_pc_queue]))
			write_android_thread = Thread(target=self.write_android, args=([to_android_queue]))

			t1.start()
			t2.start()
			read_android_thread.start()
			write_android_thread.start()
			
			#t3 = Thread(target = self.Bluetooth_Thread, args = (to_android_queue, from_android_queue, '', 1))



			#t3.start()
	
		except Exception as e:
			print(str(e))
예제 #3
0
	def Arduino_Thread(self, to_arduino_queue, from_arduino_queue, port='COM13', baud_rate=115200):
		arduino = ArduinoInterface.ArduinoInterface(port = port, baud_rate = baud_rate)
		arduino.start_arduino_connection()
		while True:
			if(not to_arduino_queue.empty()):
				to_send_bytes = to_arduino_queue.get()
				print("(ARDUINO_INSTRUCTION) to Arduino (FROM QUEUE): " + str(to_send_bytes))
				to_send_bytes.insert(0, bytes("~", "ascii"))
				to_send_bytes.append(bytes("!", "ascii"))
				arduino.write_to_arduino(b''.join(to_send_bytes))
			
			temp = arduino.read_from_arduino()
			if temp is not None:
				string_to_send_tcp = ""
						
				temp[0] = (int(temp[0])).to_bytes(1, byteorder='big').decode('ascii')
				for i in range(len(temp)):
					string_to_send_tcp += temp[i]	
				from_arduino_queue.put(string_to_send_tcp)
				print("(ARDUINO_UPDATE) to PC (INTO QUEUE): " + str(string_to_send_tcp))
				
			time.sleep(0.001)
예제 #4
0
class Main(object):

    def __init__(self):
        os.system("sudo hciconfig hci0 piscan")
        self.arduino = ArduinoInterface(port='COM13',baud_rate=115200)
        self.android = BluetoothInterface()
        self.pc = PCInterface(host='',port='5000')
      
        pass

    def start_connection(self):
        try:
            self.arduino.connect()
            self.pc.connect()
            self.android.connect()
            print("All components connected!!")
            return 1
        except Exception as e:
            print("Error connecting: %", str(e))
            return 0

    #read data from ard and put to pc queue
    def read_ard(self, pc_queue):
        while True:
            temp = self.arduino.read()
            if temp is not None:
                string_to_send_tcp = ""
                for i in range(len(temp)):
                    string_to_send_tcp += temp[i].decode("ascii")
                pc_queue.put(string_to_send_tcp)

        # receives from Arduino, doesn't block
            """if (not from_arduino_queue.empty()):
                string_to_send_tcp = from_arduino_queue.get()
                print("Received from Arduino: " + str(datetime.datetime.now()))
                # Ends with a ~
                string_to_send_tcp += "~"
                print("sending to PC")
                print(string_to_send_tcp.encode("ascii"))"""


    #write to arduino from its own queue
    def write_ard(self, ard_queue):
        while True:
            if (not ard_queue.empty()):
                to_send_bytes = ard_queue.get()
                to_send_bytes.insert(0, bytes("~", "ascii"))
                to_send_bytes.append(bytes("!", "ascii"))
                self.arduino.write(b''.join(to_send_bytes))

    #read from pc and put to android queue or bluetooth queue depending on data
    def read_pc(self,to_android_queue, to_arduino_queue):
        while True:
            message_end = False
            while not message_end:
                received, message_end = self.pc.read()

                # pass to arduino queue
                # ONLY RECEIVES THESE TWO THINGS FROM PC = ARDUINO_INSTRUCTION((byte)(0x02)), ANDROID_UPDATE((byte)0x05);
                if message_end:
                    if (received[0] == (2).to_bytes(1, byteorder='big')):
                        print("Sends to Arduino: " + str(datetime.datetime.now()))
                        to_arduino_queue.put(received[1:4])
                        message_end = False
                        received = []

                # pass to bluetooth queue
                if(received[0] == (5).to_bytes(1, byteorder='big')):
                    to_android_queue.put()

    #write to pc from pc queue
    def write_pc(self,pc_queue):
        while True:
            if (not pc_queue.empty()):
                data = pc_queue.get()
                self.pc.write(data)


    def initialise(self):
        try:
            to_ard_queue = Queue()
            to_android_queue = Queue()
            to_pc_queue = Queue()

            read_ard_thread = Thread(target=self.read_ard, args=(to_pc_queue))
            write_ard_thread = Thread(target=self.write_ard, args=(to_ard_queue))

            read_android_thread = Thread(target=self.read_android, args=(to_pc_queue))
            write_android_thread = Thread(target=self.write_android, args=(to_android_queue))

            read_pc_thread = Thread(target=self.read_pc, args=(to_android_queue,to_ard_queue))
            write_pc_thread = Thread(target=self.write_pc, args=(to_pc_queue))


            read_ard_thread.start()
            write_ard_thread.start()
            read_android_thread.start()
            write_android_thread.start()
            read_pc_thread.start()
            write_pc_thread.start()

            print("All threads started!!")

        except Exception as e:
            print(str(e))
      


    def disconnect_all(self):
        try:
            self.android.disconnect()
            self.arduino.end_arduino_connection()
            self.pc.disconnect()
            return 1
        except Exception as e:
            print("Error disconnecting: %"  ,str(e))