Skip to content

Commit

Permalink
Test latency for multithread
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshen Lim authored and Joshen Lim committed Oct 3, 2019
1 parent 476d427 commit 9b78e6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
42 changes: 16 additions & 26 deletions playgrounds/multithread_comm_demo.py
Original file line number Diff line number Diff line change
@@ -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,))

Expand All @@ -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:
Expand Down
35 changes: 18 additions & 17 deletions playgrounds/pc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 9b78e6e

Please sign in to comment.