From 9b78e6efbd54a5a4978616a38e2359309c3cbf12 Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Thu, 3 Oct 2019 22:52:17 +0800 Subject: [PATCH] Test latency for multithread --- playgrounds/multithread_comm_demo.py | 42 +++++++++++----------------- playgrounds/pc_client.py | 35 ++++++++++++----------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/playgrounds/multithread_comm_demo.py b/playgrounds/multithread_comm_demo.py index a14b41d..bd048a1 100644 --- a/playgrounds/multithread_comm_demo.py +++ b/playgrounds/multithread_comm_demo.py @@ -1,42 +1,32 @@ import _thread +import datetime import queue import os from src.Logger import Logger -from src.communicator.Android import Android from src.communicator.Arduino import Arduino from src.communicator.PC import PC log = Logger() -# python3 -m playgrounds.multithread_comm_demo - -# Demo connects Rpi with Arduino and PC -# Connections established in a sequential fashion -# PC and Arduino will pass alphabets back and forth -# Incrementing the character whenever either device receives a message - class MultiThread: def __init__(self): log.info('Initializing Multithread Communication') - self.android = Android() self.arduino = Arduino() self.pc = PC() - self.android.connect() self.arduino.connect() self.pc.connect() - self.android_queue = queue.Queue(maxsize=0) self.arduino_queue = queue.Queue(maxsize=0) self.pc_queue = queue.Queue(maxsize=0) def start(self): - _thread.start_new_thread(self.read_android, (self.android_queue,)) + # _thread.start_new_thread(self.read_android, (self.android_queue,)) _thread.start_new_thread(self.read_arduino, (self.pc_queue,)) _thread.start_new_thread(self.read_pc,(self.arduino_queue,)) - _thread.start_new_thread(self.write_android, (self.android_queue,)) + # _thread.start_new_thread(self.write_android, (self.android_queue,)) _thread.start_new_thread(self.write_arduino, (self.arduino_queue,)) _thread.start_new_thread(self.write_pc, (self.pc_queue,)) @@ -48,19 +38,19 @@ def start(self): def end(self): log.info('Multithread Communication Session Ended') - def read_android(self, android_queue): - while True: - msg = self.android.read() - if msg is not None: - log.info('Read Android:' + str(msg)) - android_queue.put_nowait('Hello from PC: ' + str(msg)) - - def write_android(self, android_queue): - while True: - if not android_queue.empty(): - msg = android_queue.get_nowait() - self.android.write(msg) - log.info('Write Android: ' + str(msg)) + # def read_android(self, android_queue): + # while True: + # msg = self.android.read() + # if msg is not None: + # log.info('Read Android:' + str(msg)) + # android_queue.put_nowait('Hello from PC: ' + str(msg)) + + # def write_android(self, android_queue): + # while True: + # if not android_queue.empty(): + # msg = android_queue.get_nowait() + # self.android.write(msg) + # log.info('Write Android: ' + str(msg)) def read_arduino(self, pc_queue): while True: diff --git a/playgrounds/pc_client.py b/playgrounds/pc_client.py index f4d3af7..75cee45 100644 --- a/playgrounds/pc_client.py +++ b/playgrounds/pc_client.py @@ -30,23 +30,24 @@ s.connect((remote_ip , port)) # Send initial message -print('# Sending "A" to remote server') -msg = "A\n" -try: - s.sendall(msg.encode('utf-8')) -except socket.error: - print ('Send failed') - sys.exit() +# print('# Sending "A" to remote server') +# msg = "A\n" +# try: +# s.sendall(msg.encode('utf-8')) +# except socket.error: +# print ('Send failed') +# sys.exit() while True: + pass # Check if there's any incoming messages - time.sleep(1) - msg = s.recv(2014).strip().decode("UTF-8") - if msg is not None: - print('# Received message: ' + str(msg)) - reply = chr(ord(msg) + 1) - try: - s.sendall(msg.encode('utf-8')) - except socket.error: - print ('Send failed') - sys.exit() + # time.sleep(1) + # msg = s.recv(2014).strip().decode("UTF-8") + # if msg is not None: + # print('# Received message: ' + str(msg)) + # reply = chr(ord(msg) + 1) + # try: + # s.sendall(msg.encode('utf-8')) + # except socket.error: + # print ('Send failed') + # sys.exit()