From ed42c1a47bc96f6453bb50008481d3467e9254e6 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sun, 5 Jun 2022 16:45:32 +0200 Subject: Refactored SDS1000X-E unit tests --- lab_control/connection/direct_connection.py | 2 +- lab_control/connection/tcp_connection.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 lab_control/connection/tcp_connection.py (limited to 'lab_control/connection') diff --git a/lab_control/connection/direct_connection.py b/lab_control/connection/direct_connection.py index 56a5cf3..8df1e42 100644 --- a/lab_control/connection/direct_connection.py +++ b/lab_control/connection/direct_connection.py @@ -10,7 +10,7 @@ class DirectConnection: def close(self) -> None: self.open = False - def send(self, request: str) -> str: + def send(self, request: str, responseExpected=True) -> str: return self.requestHandler(request) def checkConfiguration(self) -> None: diff --git a/lab_control/connection/tcp_connection.py b/lab_control/connection/tcp_connection.py new file mode 100644 index 0000000..6af710c --- /dev/null +++ b/lab_control/connection/tcp_connection.py @@ -0,0 +1,22 @@ +import socket + +class TCPConnection: + def __init__(self, ip: str, port: int): + self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self._socket.connect((address, port)) + + def configure(self, config: dict) -> None: + self._socket.settimeout(parameters["timeout"]) + + def send(self, request, responseExpected=True): + self._socket.sendall(request.encode()) + + if responseExpected: + try: + response = self._socket.recv(4096).decode() + except TimeoutError: + response = None + return response + + def checkConfiguration(self) -> None: + pass -- cgit v1.2.3