From 2d02a9787d3bbd7320f23a3c65ee8e8ed0369c1e Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 3 Dec 2021 22:19:57 +0100 Subject: Replace DearPyGui Sink with Network Sink --- src/gr-fadingui/python/netsink.py | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/gr-fadingui/python/netsink.py (limited to 'src/gr-fadingui/python/netsink.py') diff --git a/src/gr-fadingui/python/netsink.py b/src/gr-fadingui/python/netsink.py new file mode 100644 index 0000000..9df81f5 --- /dev/null +++ b/src/gr-fadingui/python/netsink.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright 2021 Sara Cinzia Halter, Naoki Pross. + +import socket +from urllib.parse import urlparse + +import numpy as np +from gnuradio import gr + +class netsink(gr.sync_block): + """ + Sink that sends the data over the network using UDP. + Keep in mind that is quite slow. + """ + def __init__(self, address, dtype, vlen): + gr.sync_block.__init__(self, + name="Network Sink", + in_sig=[], + out_sig=None) + + # Create a socket and parse remote machine url + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.url = urlparse(sock_addr) + self.srv = (self.srv.hostname, self.srv.port) + + def send(self, data): + """ + Send the data to self.srv + + @param data Data as python bytes + @return Number of bytes that were actually sent + """ + assert type(data) == bytes + return self.socket.sendto(data, self.srv) + + def encode(self, data): + """ + Encode the data into a dead simple format + + @param data Array like type + @return Bytes of ASCII encoded comma separated string of numbers + """ + # no data (what are you doing?) + if not data: + return bytes() + + values = "[" + ",".join(map(str, data)) + "]" + return bytes(values, "ascii") + + def work(self, input_items, output_items): + inp = input_items[0] + + # TODO: Check that inp has a reasonable size + self.send(self.encode(inp)) + + return len(input_items[0]) + -- cgit v1.2.1 From 97217d7eb3d0dbd07ea7deea1ec13620b8676e2d Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 3 Dec 2021 22:57:16 +0100 Subject: Fix network sink block The block can be tested with: $ netcat -l4kuv localhost 31415 | hexdump -C --- src/gr-fadingui/python/netsink.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src/gr-fadingui/python/netsink.py') diff --git a/src/gr-fadingui/python/netsink.py b/src/gr-fadingui/python/netsink.py index 9df81f5..d2dfc92 100644 --- a/src/gr-fadingui/python/netsink.py +++ b/src/gr-fadingui/python/netsink.py @@ -15,15 +15,18 @@ class netsink(gr.sync_block): Keep in mind that is quite slow. """ def __init__(self, address, dtype, vlen): + dt = np.dtype(dtype, (vlen,)) if vlen > 1 else dtype + print(dt) + gr.sync_block.__init__(self, name="Network Sink", - in_sig=[], + in_sig=[dt], out_sig=None) # Create a socket and parse remote machine url self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.url = urlparse(sock_addr) - self.srv = (self.srv.hostname, self.srv.port) + self.url = urlparse(address) + self.srv = (self.url.hostname, self.url.port) def send(self, data): """ @@ -42,18 +45,30 @@ class netsink(gr.sync_block): @param data Array like type @return Bytes of ASCII encoded comma separated string of numbers """ - # no data (what are you doing?) - if not data: - return bytes() - + # FIXME: this could be (very) slow, is there a faster way with numpy? values = "[" + ",".join(map(str, data)) + "]" return bytes(values, "ascii") def work(self, input_items, output_items): + # FIXME: it is probably better NOT to send *every* sample inp = input_items[0] + inp_len = len(inp) + blocksize = 1024 + + # Check that the packet is not huge + if len(inp) < blocksize: + self.send(self.encode(inp)) + else: + # compute how to split inp into blocks + nblocks = inp_len // blocksize + index = blocksize * nblocks - # TODO: Check that inp has a reasonable size - self.send(self.encode(inp)) + # send blocks + blocks = np.array(inp[:index]).reshape((blocksize, nblocks)) + for block in blocks: + self.send(self.encode(block)) - return len(input_items[0]) + # sent the rest + self.send(self.encode(inp[index:])) + return len(inp) -- cgit v1.2.1 From eca32bbd53d4fc51095700621525ac479f06c06c Mon Sep 17 00:00:00 2001 From: sara Date: Sat, 4 Dec 2021 15:19:24 +0100 Subject: Fix network sink block (again) --- src/gr-fadingui/python/netsink.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/gr-fadingui/python/netsink.py') diff --git a/src/gr-fadingui/python/netsink.py b/src/gr-fadingui/python/netsink.py index d2dfc92..130e5dc 100644 --- a/src/gr-fadingui/python/netsink.py +++ b/src/gr-fadingui/python/netsink.py @@ -15,7 +15,17 @@ class netsink(gr.sync_block): Keep in mind that is quite slow. """ def __init__(self, address, dtype, vlen): - dt = np.dtype(dtype, (vlen,)) if vlen > 1 else dtype + to_numpy = { + "complex": np.complex64, + "float": np.float32, + "int": np.int32, + "short": np.short, + "byte": np.byte, + } + + dt = to_numpy[dtype] + if vlen > 1: + dt = np.dtype(dt, (vlen,)) print(dt) gr.sync_block.__init__(self, -- cgit v1.2.1