aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandreas128 <Andreas>2016-12-02 17:55:02 +0100
committerandreas128 <Andreas>2016-12-02 17:55:02 +0100
commitf2b7d99828e9a9f9d849661a9e24280bbb78f30d (patch)
tree90b052507c1ed33fe98e3a216e7b5aae0b4b15d8
parent2dc2e90eff0f5c46c701ed6ae749520adf94798c (diff)
downloadODR-StaticPrecorrection-f2b7d99828e9a9f9d849661a9e24280bbb78f30d.tar.gz
ODR-StaticPrecorrection-f2b7d99828e9a9f9d849661a9e24280bbb78f30d.tar.bz2
ODR-StaticPrecorrection-f2b7d99828e9a9f9d849661a9e24280bbb78f30d.zip
Add tcp_sync.py to receive
-rw-r--r--dual_tone.grc2
-rw-r--r--tcp_async.py19
-rw-r--r--tcp_sync.py88
3 files changed, 102 insertions, 7 deletions
diff --git a/dual_tone.grc b/dual_tone.grc
index 8025939..e6ec560 100644
--- a/dual_tone.grc
+++ b/dual_tone.grc
@@ -66,7 +66,7 @@
</param>
<param>
<key>run_options</key>
- <value>prompt</value>
+ <value>run</value>
</param>
<param>
<key>run</key>
diff --git a/tcp_async.py b/tcp_async.py
index d09cf70..5cd4a20 100644
--- a/tcp_async.py
+++ b/tcp_async.py
@@ -5,7 +5,7 @@ import Queue
import time
import socket
-class TcpAsyncClient(threading.Thread):
+class _TcpAsyncClient(threading.Thread):
"""Thead for message polling"""
queue = Queue.Queue()
q_quit = Queue.Queue()
@@ -14,12 +14,16 @@ class TcpAsyncClient(threading.Thread):
port = None
BUFFER_SIZE = 1
- def run(self, ip_address = "127.0.0.1", port = 47010):
- """connect and poll messages to queue"""
-
+ def __init__(self, ip_address, port):
+ super(_TcpAsyncClient, self).__init__()
self.ip_address = ip_address
self.port = port
+ def __exit__(self):
+ self.stop()
+
+ def run(self):
+ """connect and poll messages to queue"""
#Establish connection
sock = None
@@ -55,11 +59,14 @@ class TcpAsyncClient(threading.Thread):
class UhdAsyncMsg(object):
"""Creates a thread to connect to the asynchronous uhd messages tcp port"""
- tcpa = TcpAsyncClient()
- def __init__(self):
+ def __init__(self, ip_address = "127.0.0.1", port = 47010):
+ self.tcpa = _TcpAsyncClient(ip_address, port)
self.tcpa.start()
+ def __exit__(self):
+ self.tcpa.stop()
+
def stop(self):
"""stop tcp thread"""
self.tcpa.stop()
diff --git a/tcp_sync.py b/tcp_sync.py
new file mode 100644
index 0000000..faffe51
--- /dev/null
+++ b/tcp_sync.py
@@ -0,0 +1,88 @@
+"""Tcp client for synchronous uhd message tcp port"""
+
+import threading
+import Queue
+import time
+import socket
+import struct
+
+class _TcpSyncClient(threading.Thread):
+ """Thead for message polling"""
+ queue = Queue.Queue()
+ q_quit = Queue.Queue()
+
+ ip_address = None
+ port = None
+
+ def __init__(self, ip_address, port):
+ super(_TcpSyncClient, self).__init__()
+ self.ip_address = ip_address
+ self.port = port
+
+ def __exit__(self):
+ self.stop()
+
+ def run(self):
+ """connect and poll messages to queue"""
+
+ #Establish connection
+ sock = None
+ print("Connecting to synchronous uhd message tcp port " + str(self.port))
+ while 1:
+ try:
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.connect((self.ip_address, self.port))
+ break
+ except socket.error:
+ print("connecting to synchronous uhd message tcp port " + str(self.port))
+ #traceback.print_exc()
+ sock.close()
+ time.sleep(0.5)
+ print("Connected to synchronous uhd message tcp port " + str(self.port))
+
+ #Read messages
+ sock.settimeout(None)
+ while self.q_quit.empty():
+ try:
+ s = sock.recv(12)
+ res_tuple = struct.unpack(
+ "fff",
+ s)
+ assert(type(res_tuple) is tuple), (type(res_list), res_tuple)
+ self.queue.put(res_tuple)
+ except socket.timeout:
+ traceback.print_exc()
+ pass
+
+ sock.close()
+
+ def stop(self):
+ """stop thread"""
+ print("stop tcp_sync uhd message tcp thread")
+ self.q_quit.put("end")
+
+
+class UhdSyncMsg(object):
+ """Creates a thread to connect to the synchronous uhd messages tcp port"""
+
+ def __init__(self, ip_address = "127.0.0.1", port = 47009):
+ self.tcpa = _TcpSyncClient(ip_address, port)
+ self.tcpa.start()
+
+ def __exit__(self):
+ self.tcpa.stop()
+
+ def stop(self):
+ """stop tcp thread"""
+ self.tcpa.stop()
+
+ def get_res(self):
+ """get received messages as string of integer"""
+ out = []
+ while not self.tcpa.queue.empty():
+ out.append(self.tcpa.queue.get())
+ return out
+
+ def has_msg(self):
+ """Checks if one or more messages were received and empties the message queue"""
+ return self.get_res() != ""