The central concept of message passing is by the use of multi-threading function. Since RPI is responsible for receiving and sending messages between devices, each receive and send function is seen as a thread. Thus, 6 threads are created: receive and send thread for each device.
def __init__(self):
threading.Thread.__init__(self)
self.pc_thread = PcAPI()
print("PC - PC thread created.")
self.bt_thread = BluetoothAPI()
print("BT - Bluetooth thread created.")
self.sr_thread = SerialAPI()
print("SR - Arduino thread created.")
def initialize_threads(self):
# PC read and write thread
rt_pc = threading.Thread(target = self.readPC, name = "pc_read_thread")
wt_pc = threading.Thread(target = self.writePC, args = ("",), name = "pc_write_thread")
# Bluetooth (BT) read and write thread
rt_bt = threading.Thread(target = self.readBT, name = "bt_read_thread")
wt_bt = threading.Thread(target = self.writeBT, args = ("",), name = "bt_write_thread")
# Serial (SR) read and write thread
rt_sr = threading.Thread(target = self.readSR, name = "sr_read_thread")
wt_sr = threading.Thread(target = self.writeSR, args = ("",), name = "sr_write_thread")
# Set threads as daemons
rt_pc.daemon = True
wt_pc.daemon = True
rt_bt.daemon = True
wt_bt.daemon = True
rt_sr.daemon = True
wt_sr.daemon = True
rt_pc.start()
wt_pc.start()
rt_bt.start()
wt_bt.start()
rt_sr.start()
wt_sr.start()
Advantages of Multi-thread processing:
1. Concurrent message sending – Each thread is focused on its own job scope: either receiving or sending. Thus, the message can be sent to two devices at the same time rather than waiting for one to be sent first.
2. Reduce the time of communication – With each thread working independently by itself, messagesĀ are sent with little or no jams as no message queue are created.