From fd0a8880c666381aeeecf11cf8bbfee22c3fe723 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sun, 14 Mar 2021 15:02:07 -0400 Subject: [PATCH 01/12] Adds tools folder and UDP implementation of network tool --- Tools/boringNetworkTool/main.py | 97 +++++++++++++++++++++ Tools/boringNetworkTool/sending_config.json | 63 +++++++++++++ Tools/boringNetworkTool/udp_server.py | 43 +++++++++ 3 files changed, 203 insertions(+) create mode 100644 Tools/boringNetworkTool/main.py create mode 100644 Tools/boringNetworkTool/sending_config.json create mode 100644 Tools/boringNetworkTool/udp_server.py diff --git a/Tools/boringNetworkTool/main.py b/Tools/boringNetworkTool/main.py new file mode 100644 index 00000000..ddc6b491 --- /dev/null +++ b/Tools/boringNetworkTool/main.py @@ -0,0 +1,97 @@ +from tkintertoy import Window +import socket +import sys +import time +import json + +# try: +# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +# except socket.error: +# print('Failed to create socket') +# sys.exit() + +# host = 'localhost' +# port = 8888 +# +# gui = Window() +# gui.setTitle('Paradigm Boring UDP Tool') +# gui.addEntry("udp_message", "UDP Message") +# gui.addButton("send_udp") +# gui.plot("udp_message", row=0) +# gui.plot("send_udp", row=1) + +# while True: +# gui.waitforUser() +# if gui.content: +# msg = bytes(gui.get("udp_message"), 'utf-8') +# try: +# # Set the whole string +# s.sendto(msg, (host, port)) +# print("message sent:" + gui.get("udp_message")) +# # receive data from client (data, addr) +# d = s.recvfrom(1024) +# reply = d[0] +# addr = d[1] +# print('Server reply : ' + reply.decode('utf-8')) +# except socket.error as msg: +# print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) +# sys.exit() +# else: +# break + + +class Message: + def __init__(self, msgID, isExtendedID, data): + self.msgID = msgID + self.isExtendedID = isExtendedID + self.data = data + def toString(self): + return "" + str(self.msgID) + "\n" + str(self.isExtendedID) + "\n" + str(self.data) + +def createMessages(raw_messages): + msgs = [] + print(raw_messages) + for message in raw_messages: + msgs.append(Message(msgID=message["msgId"], + isExtendedID=message["isExtendedId"], + data=message["data"] + )) + return msgs + + +def sendWithUDP(msg_config, host, port): + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + except socket.error: + print('Failed to create socket') + sys.exit() + msgs = createMessages(msg_config["config"]["messages"]) + msg_frequency = msg_config["config"]["messageFrequency"] + for message in msgs: + time.sleep(1.0 / msg_frequency) + try: + # Set the whole string + s.sendto(bytes(message.toString(), 'utf-8'), (host, port)) + print("message sent:" + message.toString()) + # receive data from client (data, addr) + d = s.recvfrom(1024) + reply = d[0] + addr = d[1] + print('Server reply : ' + reply.decode('utf-8')) + except socket.error as msg: + print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + sys.exit() + +def sendWithTCP(msg_config, host, port): + pass + +if __name__ == "__main__": + with open('sending_config.json', mode='r') as json_config: + msg_config = json.load(json_config) + host = msg_config["config"]["ip"] + port = msg_config["config"]["port"] + useTCP = msg_config["config"]["useTCP"] + if useTCP: + sendWithTCP(msg_config, host, port) + else: + sendWithUDP(msg_config, host, port) diff --git a/Tools/boringNetworkTool/sending_config.json b/Tools/boringNetworkTool/sending_config.json new file mode 100644 index 00000000..e828db62 --- /dev/null +++ b/Tools/boringNetworkTool/sending_config.json @@ -0,0 +1,63 @@ +{ + "config": { + "ip": "localhost", + "port": 8888, + "useTCP": false, + "messageFrequency": 5, + "messages": [ + { + "msgId": 5, + "isExtendedId": 0, + "data": [ + 5, + 6, + 7, + 8, + 9, + 10 + ] + }, + { + "msgId": 100, + "isExtendedId": 0, + "data": [ + 72, + 54, + 32, + 87 + ] + }, + { + "msgId": 200, + "isExtendedId": 0, + "data": [ + 100, + 253, + 0, + 87 + ] + }, + { + "msgId": 74, + "isExtendedId": 0, + "data": [ + 87 + ] + }, + { + "msgId": 29, + "isExtendedId": 0, + "data": [ + 98, + 56, + 182, + 78, + 28, + 37, + 16, + 82 + ] + } + ] + } +} \ No newline at end of file diff --git a/Tools/boringNetworkTool/udp_server.py b/Tools/boringNetworkTool/udp_server.py new file mode 100644 index 00000000..8450d9bd --- /dev/null +++ b/Tools/boringNetworkTool/udp_server.py @@ -0,0 +1,43 @@ +''' + Simple udp socket server +''' + +import socket +import sys + +HOST = '' # Symbolic name meaning all available interfaces +PORT = 8888 # Arbitrary non-privileged port + +# Datagram (udp) socket +try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + print('Socket created') +except socket.error as msg: + print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + sys.exit() + +# Bind socket to local host and port +try: + s.bind((HOST, PORT)) +except socket.error as msg: + print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + sys.exit() + +print('Socket bind complete') + +# now keep talking with the client +while 1: + # receive data from client (data, addr) + d = s.recvfrom(1024) + data = d[0] + addr = d[1] + + if not data: + break + + reply = 'OK...' + data.decode('utf-8') + + s.sendto(bytes(reply, 'utf-8'), addr) + print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.decode('utf-8').strip()) + +s.close() \ No newline at end of file From f8101fd4a3121369690d6aa093b0ac846760944e Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sat, 20 Mar 2021 19:22:48 -0400 Subject: [PATCH 02/12] adds simple tcp server for testing --- Tools/boringNetworkTool/tcp_server.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Tools/boringNetworkTool/tcp_server.py diff --git a/Tools/boringNetworkTool/tcp_server.py b/Tools/boringNetworkTool/tcp_server.py new file mode 100644 index 00000000..5efc66e6 --- /dev/null +++ b/Tools/boringNetworkTool/tcp_server.py @@ -0,0 +1,11 @@ +import socket +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', 8888)) +s.listen(1) +conn, addr = s.accept() +while 1: + data = conn.recv(1024) + if not data: + break + conn.sendall(data) +conn.close() \ No newline at end of file From ed455064c3235497e480fdd243c316135fcce4d2 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sat, 20 Mar 2021 19:24:03 -0400 Subject: [PATCH 03/12] adds tcp functionality to network tool --- Tools/boringNetworkTool/main.py | 62 +++++++++++++-------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/Tools/boringNetworkTool/main.py b/Tools/boringNetworkTool/main.py index ddc6b491..89fd767b 100644 --- a/Tools/boringNetworkTool/main.py +++ b/Tools/boringNetworkTool/main.py @@ -1,44 +1,8 @@ -from tkintertoy import Window import socket import sys import time import json -# try: -# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -# except socket.error: -# print('Failed to create socket') -# sys.exit() - -# host = 'localhost' -# port = 8888 -# -# gui = Window() -# gui.setTitle('Paradigm Boring UDP Tool') -# gui.addEntry("udp_message", "UDP Message") -# gui.addButton("send_udp") -# gui.plot("udp_message", row=0) -# gui.plot("send_udp", row=1) - -# while True: -# gui.waitforUser() -# if gui.content: -# msg = bytes(gui.get("udp_message"), 'utf-8') -# try: -# # Set the whole string -# s.sendto(msg, (host, port)) -# print("message sent:" + gui.get("udp_message")) -# # receive data from client (data, addr) -# d = s.recvfrom(1024) -# reply = d[0] -# addr = d[1] -# print('Server reply : ' + reply.decode('utf-8')) -# except socket.error as msg: -# print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) -# sys.exit() -# else: -# break - class Message: def __init__(self, msgID, isExtendedID, data): @@ -50,7 +14,6 @@ def toString(self): def createMessages(raw_messages): msgs = [] - print(raw_messages) for message in raw_messages: msgs.append(Message(msgID=message["msgId"], isExtendedID=message["isExtendedId"], @@ -83,7 +46,29 @@ def sendWithUDP(msg_config, host, port): sys.exit() def sendWithTCP(msg_config, host, port): - pass + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, port)) + except socket.error: + print('Failed to create socket') + sys.exit() + msgs = createMessages(msg_config["config"]["messages"]) + msg_frequency = msg_config["config"]["messageFrequency"] + for message in msgs: + time.sleep(1.0 / msg_frequency) + try: + # Set the whole string + s.sendall(bytes(message.toString(), 'utf-8')) + print("message sent:" + message.toString()) + # receive data from client (data, addr) + d = s.recv(1024) + print('Server reply : ' + d.decode('utf-8')) + except socket.error as msg: + print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + s.close() + sys.exit() + s.close() + if __name__ == "__main__": with open('sending_config.json', mode='r') as json_config: @@ -91,6 +76,7 @@ def sendWithTCP(msg_config, host, port): host = msg_config["config"]["ip"] port = msg_config["config"]["port"] useTCP = msg_config["config"]["useTCP"] + print("Using TCP: " + str(useTCP)) if useTCP: sendWithTCP(msg_config, host, port) else: From df99928c75f20644a02cbb9e043dabc5c76f9793 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sat, 20 Mar 2021 19:54:45 -0400 Subject: [PATCH 04/12] cleans up both server test scripts --- Tools/boringNetworkTool/tcp_server.py | 21 +++++++++++++++++++-- Tools/boringNetworkTool/udp_server.py | 18 +++--------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Tools/boringNetworkTool/tcp_server.py b/Tools/boringNetworkTool/tcp_server.py index 5efc66e6..cb6d49f8 100644 --- a/Tools/boringNetworkTool/tcp_server.py +++ b/Tools/boringNetworkTool/tcp_server.py @@ -1,6 +1,23 @@ import socket -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.bind(('', 8888)) +import sys + +HOST = '' +PORT = 8888 + +try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + print('Socket created') +except socket.error as msg: + print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + sys.exit() + +try: + s.bind((HOST, PORT)) +except socket.error as msg: + print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + sys.exit() + + s.listen(1) conn, addr = s.accept() while 1: diff --git a/Tools/boringNetworkTool/udp_server.py b/Tools/boringNetworkTool/udp_server.py index 8450d9bd..67852179 100644 --- a/Tools/boringNetworkTool/udp_server.py +++ b/Tools/boringNetworkTool/udp_server.py @@ -1,14 +1,9 @@ -''' - Simple udp socket server -''' - import socket import sys -HOST = '' # Symbolic name meaning all available interfaces -PORT = 8888 # Arbitrary non-privileged port +HOST = '' +PORT = 8888 -# Datagram (udp) socket try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) print('Socket created') @@ -16,28 +11,21 @@ print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) sys.exit() -# Bind socket to local host and port try: s.bind((HOST, PORT)) except socket.error as msg: print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) sys.exit() -print('Socket bind complete') - -# now keep talking with the client while 1: - # receive data from client (data, addr) d = s.recvfrom(1024) data = d[0] addr = d[1] - if not data: break reply = 'OK...' + data.decode('utf-8') - s.sendto(bytes(reply, 'utf-8'), addr) print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.decode('utf-8').strip()) -s.close() \ No newline at end of file +s.close() From 90850f3ca03c2b63aa737d19e1bdc4604a838b40 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sat, 20 Mar 2021 19:55:07 -0400 Subject: [PATCH 05/12] adds README --- Tools/boringNetworkTool/README.MD | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Tools/boringNetworkTool/README.MD diff --git a/Tools/boringNetworkTool/README.MD b/Tools/boringNetworkTool/README.MD new file mode 100644 index 00000000..93cc4417 --- /dev/null +++ b/Tools/boringNetworkTool/README.MD @@ -0,0 +1,17 @@ +Boring Network Tool + +The file main.py can be used to send either +TCP or UDP messages to a given hostname and port, specified via the config json. + +This script does not require any external packages, aside from standard python 3.7 or higher. + +Usage: + +sending_config.json +has a similar configuration to the json in Pidaq/Examples/CanSender, and can be used to specify the +messages to send, and whether to send them via TCP or UDP. The frequency with which to send the +messages can also be specified. Finally, the port and hostname are specified in the json as well. + +tcp_server.py implements a simple TCP server to verify the functionality of the script + +udp_server.py implements a simple UDP server to verify the functionality of the script \ No newline at end of file From 65770ce3d854d6d0a5fd032366a3e9585d9ba7fb Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Sat, 20 Mar 2021 20:02:39 -0400 Subject: [PATCH 06/12] servers now print out data received --- Tools/boringNetworkTool/README.MD | 7 +++++-- Tools/boringNetworkTool/tcp_server.py | 1 + Tools/boringNetworkTool/udp_server.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tools/boringNetworkTool/README.MD b/Tools/boringNetworkTool/README.MD index 93cc4417..f79dd08e 100644 --- a/Tools/boringNetworkTool/README.MD +++ b/Tools/boringNetworkTool/README.MD @@ -12,6 +12,9 @@ has a similar configuration to the json in Pidaq/Examples/CanSender, and can be messages to send, and whether to send them via TCP or UDP. The frequency with which to send the messages can also be specified. Finally, the port and hostname are specified in the json as well. -tcp_server.py implements a simple TCP server to verify the functionality of the script +tcp_server.py implements a simple TCP server to verify the functionality of the script. -udp_server.py implements a simple UDP server to verify the functionality of the script \ No newline at end of file +udp_server.py implements a simple UDP server to verify the functionality of the script. + +For both server scripts, to receive any messages and print them out, simply use localhost +and port 8888 to connect to the server after running it \ No newline at end of file diff --git a/Tools/boringNetworkTool/tcp_server.py b/Tools/boringNetworkTool/tcp_server.py index cb6d49f8..841cc05c 100644 --- a/Tools/boringNetworkTool/tcp_server.py +++ b/Tools/boringNetworkTool/tcp_server.py @@ -22,6 +22,7 @@ conn, addr = s.accept() while 1: data = conn.recv(1024) + print("Data received: " + data.decode('utf-8')) if not data: break conn.sendall(data) diff --git a/Tools/boringNetworkTool/udp_server.py b/Tools/boringNetworkTool/udp_server.py index 67852179..f5988827 100644 --- a/Tools/boringNetworkTool/udp_server.py +++ b/Tools/boringNetworkTool/udp_server.py @@ -24,6 +24,7 @@ if not data: break + print("Data received: " + data.decode('utf-8')) reply = 'OK...' + data.decode('utf-8') s.sendto(bytes(reply, 'utf-8'), addr) print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.decode('utf-8').strip()) From 3d24e86f5350236ef4d986193432434f54f56eb6 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 19:07:50 -0400 Subject: [PATCH 07/12] PEP guidelines fix --- Tools/boringNetworkTool/main.py | 43 ++++++++++++++------------- Tools/boringNetworkTool/tcp_server.py | 4 +-- Tools/boringNetworkTool/udp_server.py | 2 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Tools/boringNetworkTool/main.py b/Tools/boringNetworkTool/main.py index 89fd767b..f531a004 100644 --- a/Tools/boringNetworkTool/main.py +++ b/Tools/boringNetworkTool/main.py @@ -5,37 +5,39 @@ class Message: - def __init__(self, msgID, isExtendedID, data): - self.msgID = msgID - self.isExtendedID = isExtendedID + def __init__(self, msgid, isextendedid, data): + self.msgid = msgid + self.isextendedid = isextendedid self.data = data - def toString(self): - return "" + str(self.msgID) + "\n" + str(self.isExtendedID) + "\n" + str(self.data) -def createMessages(raw_messages): + def to_string(self) -> str: + return "" + str(self.msgid) + "\n" + str(self.isextendedid) + "\n" + str(self.data) + + +def create_messages(raw_messages): msgs = [] for message in raw_messages: - msgs.append(Message(msgID=message["msgId"], - isExtendedID=message["isExtendedId"], + msgs.append(Message(msgid=message["msgId"], + isextendedid=message["isExtendedId"], data=message["data"] )) return msgs -def sendWithUDP(msg_config, host, port): +def send_with_udp(msgconfig, host, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) except socket.error: print('Failed to create socket') sys.exit() - msgs = createMessages(msg_config["config"]["messages"]) - msg_frequency = msg_config["config"]["messageFrequency"] + msgs = create_messages(msgconfig["config"]["messages"]) + msg_frequency = msgconfig["config"]["messageFrequency"] for message in msgs: time.sleep(1.0 / msg_frequency) try: # Set the whole string - s.sendto(bytes(message.toString(), 'utf-8'), (host, port)) - print("message sent:" + message.toString()) + s.sendto(bytes(message.to_string(), 'utf-8'), (host, port)) + print("message sent:" + message.to_string()) # receive data from client (data, addr) d = s.recvfrom(1024) reply = d[0] @@ -45,21 +47,22 @@ def sendWithUDP(msg_config, host, port): print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) sys.exit() -def sendWithTCP(msg_config, host, port): + +def send_with_tcp(msgconfig, host, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) except socket.error: print('Failed to create socket') sys.exit() - msgs = createMessages(msg_config["config"]["messages"]) - msg_frequency = msg_config["config"]["messageFrequency"] + msgs = create_messages(msgconfig["config"]["messages"]) + msg_frequency = msgconfig["config"]["messageFrequency"] for message in msgs: time.sleep(1.0 / msg_frequency) try: # Set the whole string - s.sendall(bytes(message.toString(), 'utf-8')) - print("message sent:" + message.toString()) + s.sendall(bytes(message.to_string(), 'utf-8')) + print("message sent:" + message.to_string()) # receive data from client (data, addr) d = s.recv(1024) print('Server reply : ' + d.decode('utf-8')) @@ -78,6 +81,6 @@ def sendWithTCP(msg_config, host, port): useTCP = msg_config["config"]["useTCP"] print("Using TCP: " + str(useTCP)) if useTCP: - sendWithTCP(msg_config, host, port) + send_with_tcp(msg_config, host, port) else: - sendWithUDP(msg_config, host, port) + send_with_udp(msg_config, host, port) diff --git a/Tools/boringNetworkTool/tcp_server.py b/Tools/boringNetworkTool/tcp_server.py index 841cc05c..36dac34f 100644 --- a/Tools/boringNetworkTool/tcp_server.py +++ b/Tools/boringNetworkTool/tcp_server.py @@ -1,7 +1,7 @@ import socket import sys -HOST = '' +HOST = 'localhost' PORT = 8888 try: @@ -26,4 +26,4 @@ if not data: break conn.sendall(data) -conn.close() \ No newline at end of file +conn.close() diff --git a/Tools/boringNetworkTool/udp_server.py b/Tools/boringNetworkTool/udp_server.py index f5988827..580b1030 100644 --- a/Tools/boringNetworkTool/udp_server.py +++ b/Tools/boringNetworkTool/udp_server.py @@ -1,7 +1,7 @@ import socket import sys -HOST = '' +HOST = 'localhost' PORT = 8888 try: From 8f1b399fb7c84a65ccf38ff43df090b26b765b53 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 19:09:07 -0400 Subject: [PATCH 08/12] json endline --- Tools/boringNetworkTool/sending_config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/boringNetworkTool/sending_config.json b/Tools/boringNetworkTool/sending_config.json index e828db62..c935d33d 100644 --- a/Tools/boringNetworkTool/sending_config.json +++ b/Tools/boringNetworkTool/sending_config.json @@ -60,4 +60,4 @@ } ] } -} \ No newline at end of file +} From 305966aa7458e82095f75ab22536b00bd13c5aac Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 23:03:32 -0400 Subject: [PATCH 09/12] formats python strings with % encoding --- Tools/boringNetworkTool/main.py | 25 ++++++++++--------------- Tools/boringNetworkTool/tcp_server.py | 6 +++--- Tools/boringNetworkTool/udp_server.py | 10 +++++----- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Tools/boringNetworkTool/main.py b/Tools/boringNetworkTool/main.py index f531a004..eac90eb3 100644 --- a/Tools/boringNetworkTool/main.py +++ b/Tools/boringNetworkTool/main.py @@ -5,22 +5,17 @@ class Message: - def __init__(self, msgid, isextendedid, data): - self.msgid = msgid - self.isextendedid = isextendedid + def __init__(self, data): self.data = data def to_string(self) -> str: - return "" + str(self.msgid) + "\n" + str(self.isextendedid) + "\n" + str(self.data) + return "%s \n" % str(self.data) def create_messages(raw_messages): msgs = [] for message in raw_messages: - msgs.append(Message(msgid=message["msgId"], - isextendedid=message["isExtendedId"], - data=message["data"] - )) + msgs.append(Message(data=message["data"])) return msgs @@ -37,14 +32,14 @@ def send_with_udp(msgconfig, host, port): try: # Set the whole string s.sendto(bytes(message.to_string(), 'utf-8'), (host, port)) - print("message sent:" + message.to_string()) + print("message sent: %s" % message.to_string()) # receive data from client (data, addr) d = s.recvfrom(1024) reply = d[0] addr = d[1] - print('Server reply : ' + reply.decode('utf-8')) + print('Server reply : %s' % reply.decode('utf-8')) except socket.error as msg: - print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Error Code : %s Message : %s ' % (str(msg[0]), msg[1])) sys.exit() @@ -62,12 +57,12 @@ def send_with_tcp(msgconfig, host, port): try: # Set the whole string s.sendall(bytes(message.to_string(), 'utf-8')) - print("message sent:" + message.to_string()) + print("message sent: %s" % message.to_string()) # receive data from client (data, addr) d = s.recv(1024) - print('Server reply : ' + d.decode('utf-8')) + print('Server reply : %s' % d.decode('utf-8')) except socket.error as msg: - print('Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Error Code : %s Message : %s ' % (str(msg[0]), msg[1])) s.close() sys.exit() s.close() @@ -79,7 +74,7 @@ def send_with_tcp(msgconfig, host, port): host = msg_config["config"]["ip"] port = msg_config["config"]["port"] useTCP = msg_config["config"]["useTCP"] - print("Using TCP: " + str(useTCP)) + print("Using TCP: %s" % str(useTCP)) if useTCP: send_with_tcp(msg_config, host, port) else: diff --git a/Tools/boringNetworkTool/tcp_server.py b/Tools/boringNetworkTool/tcp_server.py index 36dac34f..28cf19ca 100644 --- a/Tools/boringNetworkTool/tcp_server.py +++ b/Tools/boringNetworkTool/tcp_server.py @@ -8,13 +8,13 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('Socket created') except socket.error as msg: - print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Failed to create socket. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) sys.exit() try: s.bind((HOST, PORT)) except socket.error as msg: - print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Bind failed. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) sys.exit() @@ -22,7 +22,7 @@ conn, addr = s.accept() while 1: data = conn.recv(1024) - print("Data received: " + data.decode('utf-8')) + print("Data received: %s" % data.decode('utf-8')) if not data: break conn.sendall(data) diff --git a/Tools/boringNetworkTool/udp_server.py b/Tools/boringNetworkTool/udp_server.py index 580b1030..a02fd717 100644 --- a/Tools/boringNetworkTool/udp_server.py +++ b/Tools/boringNetworkTool/udp_server.py @@ -8,13 +8,13 @@ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) print('Socket created') except socket.error as msg: - print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Failed to create socket. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) sys.exit() try: s.bind((HOST, PORT)) except socket.error as msg: - print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + print('Bind failed. Error Code : %s Message : %s' % (str(msg[0]), msg[1])) sys.exit() while 1: @@ -24,9 +24,9 @@ if not data: break - print("Data received: " + data.decode('utf-8')) - reply = 'OK...' + data.decode('utf-8') + print("Data received: %s" % data.decode('utf-8')) + reply = 'OK... %s' % data.decode('utf-8') s.sendto(bytes(reply, 'utf-8'), addr) - print('Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.decode('utf-8').strip()) + print('Message[%s:%s] - %s' % (addr[0], str(addr[1]), data.decode('utf-8').strip())) s.close() From f5cb9c8a3742a1aeff7816c64f6fa836096253fa Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 23:13:40 -0400 Subject: [PATCH 10/12] removes CAN params in json --- Tools/boringNetworkTool/sending_config.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Tools/boringNetworkTool/sending_config.json b/Tools/boringNetworkTool/sending_config.json index c935d33d..e0039d4a 100644 --- a/Tools/boringNetworkTool/sending_config.json +++ b/Tools/boringNetworkTool/sending_config.json @@ -6,8 +6,6 @@ "messageFrequency": 5, "messages": [ { - "msgId": 5, - "isExtendedId": 0, "data": [ 5, 6, @@ -18,8 +16,6 @@ ] }, { - "msgId": 100, - "isExtendedId": 0, "data": [ 72, 54, @@ -28,8 +24,6 @@ ] }, { - "msgId": 200, - "isExtendedId": 0, "data": [ 100, 253, @@ -38,15 +32,11 @@ ] }, { - "msgId": 74, - "isExtendedId": 0, "data": [ 87 ] }, { - "msgId": 29, - "isExtendedId": 0, "data": [ 98, 56, From b0f9593d12f438f67b38f0e0fcfa53bcf490521f Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 23:28:51 -0400 Subject: [PATCH 11/12] adds json flag for non blocking udp messages --- Tools/boringNetworkTool/main.py | 10 ++++++---- Tools/boringNetworkTool/sending_config.json | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Tools/boringNetworkTool/main.py b/Tools/boringNetworkTool/main.py index eac90eb3..dafc029b 100644 --- a/Tools/boringNetworkTool/main.py +++ b/Tools/boringNetworkTool/main.py @@ -34,10 +34,10 @@ def send_with_udp(msgconfig, host, port): s.sendto(bytes(message.to_string(), 'utf-8'), (host, port)) print("message sent: %s" % message.to_string()) # receive data from client (data, addr) - d = s.recvfrom(1024) - reply = d[0] - addr = d[1] - print('Server reply : %s' % reply.decode('utf-8')) + if msgconfig["config"]["sendContinuous"] is False: + d = s.recvfrom(1024) + reply = d[0] + print('Server reply : %s' % reply.decode('utf-8')) except socket.error as msg: print('Error Code : %s Message : %s ' % (str(msg[0]), msg[1])) sys.exit() @@ -74,7 +74,9 @@ def send_with_tcp(msgconfig, host, port): host = msg_config["config"]["ip"] port = msg_config["config"]["port"] useTCP = msg_config["config"]["useTCP"] + sendContinuous = msg_config["config"]["sendContinuous"] print("Using TCP: %s" % str(useTCP)) + print("Waiting for response: %s" % str(sendContinuous)) if useTCP: send_with_tcp(msg_config, host, port) else: diff --git a/Tools/boringNetworkTool/sending_config.json b/Tools/boringNetworkTool/sending_config.json index e0039d4a..c9848436 100644 --- a/Tools/boringNetworkTool/sending_config.json +++ b/Tools/boringNetworkTool/sending_config.json @@ -3,6 +3,7 @@ "ip": "localhost", "port": 8888, "useTCP": false, + "sendContinuous": true, "messageFrequency": 5, "messages": [ { From 223a8febc51e04c73e2c97c1642d654de3bf5c44 Mon Sep 17 00:00:00 2001 From: Matthew Swenson Date: Mon, 22 Mar 2021 23:30:02 -0400 Subject: [PATCH 12/12] Readme now uses markdown --- Tools/boringNetworkTool/README.MD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/boringNetworkTool/README.MD b/Tools/boringNetworkTool/README.MD index f79dd08e..eb9a5d49 100644 --- a/Tools/boringNetworkTool/README.MD +++ b/Tools/boringNetworkTool/README.MD @@ -1,11 +1,11 @@ -Boring Network Tool +##Boring Network Tool The file main.py can be used to send either TCP or UDP messages to a given hostname and port, specified via the config json. This script does not require any external packages, aside from standard python 3.7 or higher. -Usage: +###Usage sending_config.json has a similar configuration to the json in Pidaq/Examples/CanSender, and can be used to specify the