diff --git a/examples/serialclient.py b/examples/serialclient.py index be9bbf1c4a93ad7a97a6ad5d2ddb49957dde4757..5a976a495155e7d80777c0fd6c902d76f818e5c4 100644 --- a/examples/serialclient.py +++ b/examples/serialclient.py @@ -1,12 +1,15 @@ from pyrsvp.rsvpclient import RsvpClientSerial from pyrsvp.rsvptypes import * +# To emulate serial ports run the following command: +# socat -d -d pty,raw,echo=0 pty,raw,echo=0 + def test_cb(args : dict): print(args) if __name__ == "__main__": - client = RsvpClientSerial("/dev/pts/6", baud=9600) + client = RsvpClientSerial("/dev/pts/9", baud=9600) args = [ RsvpInteger("ARG1", 1), @@ -18,6 +21,6 @@ if __name__ == "__main__": ] try: - client.send_command("TEST", args, callback=test_cb) + print(client.send_command("TEST", args)) except Exception as ex: print(f"[{type(ex).__name__}] Error: {ex}") \ No newline at end of file diff --git a/examples/serialserver.py b/examples/serialserver.py index b222c4e0fe3c2c9c0bffb5cce7c49e24667de3a8..bb81f95f987ccf4f70ef640507c87dd4b18d19a1 100644 --- a/examples/serialserver.py +++ b/examples/serialserver.py @@ -1,6 +1,9 @@ from pyrsvp.rsvpserver import RsvpServer, RsvpServerSerial from pyrsvp.rsvptypes import * +# To emulate serial ports run the following command: +# socat -d -d pty,raw,echo=0 pty,raw,echo=0 + @RsvpServer.Command("TEST") def test(args : dict): print(args) @@ -9,5 +12,5 @@ def test(args : dict): if __name__ == "__main__": - srv = RsvpServerSerial("/dev/pts/5", baud=9600) - srv.run() \ No newline at end of file + srv = RsvpServerSerial("/dev/pts/8", baud=9600) + srv.run(True) \ No newline at end of file diff --git a/examples/tcpclient.py b/examples/tcpclient.py index 3d4e0191fcec145230382036a859a2bfa83026de..bcba08a80ef7c3490465fc5b175238ed679b5472 100644 --- a/examples/tcpclient.py +++ b/examples/tcpclient.py @@ -1,5 +1,5 @@ -from pyrsvp.rsvpclient import RsvpClientTcp, RsvpClientNotAck +from pyrsvp.rsvpclient import RsvpClientTcp from pyrsvp.rsvptypes import * @@ -20,7 +20,7 @@ if __name__ == "__main__": ] try: - print(client.send_command("START", args)) + print(client.send_command("")) except Exception as ex: print(f"[{type(ex).__name__}] Error: {ex}") \ No newline at end of file diff --git a/examples/tcpserver.py b/examples/tcpserver.py index 4991cf82cd0038f49a2b9f2f4d071d6e5c11da86..56203f43b407034f18ca0447a7bf22f03d2f86f6 100644 --- a/examples/tcpserver.py +++ b/examples/tcpserver.py @@ -21,7 +21,7 @@ def start(values : dict) -> str: print(values) - return RsvpServer.reply(True, [RsvpInteger("CODE", 1)]) + return RsvpServer.reply(True) if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index 3dfc03093940c85cadb1371cb94b3a3aa3f07635..1ae6f748c345b77a9b9a4dd704f859e3457e99a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyrsvp" -version = "0.2.3" +version = "0.2.4" description = "Implements RSVP communication protocol in Python" authors = ["Mattia Gallacchi "] readme = "README.md" diff --git a/pyrsvp/rsvpserver.py b/pyrsvp/rsvpserver.py index 05f1995d2518b8c9b23f741d27aed2545e6dfd4e..c928cc2349ea6439a045ea91fcddd142a752fa1f 100644 --- a/pyrsvp/rsvpserver.py +++ b/pyrsvp/rsvpserver.py @@ -239,7 +239,7 @@ class RsvpServerSerial(RsvpServer): self._running = False self.ser.cancel_read() - def run(self): + def run(self, debug=False): """Run the Serial server""" try: @@ -255,7 +255,7 @@ class RsvpServerSerial(RsvpServer): while self._running: try: - data = self.ser.readline().strip() + data = self.ser.readline() except serial.SerialException as ex: # Something went wrong probably an empty line was received print(ex) @@ -264,7 +264,15 @@ class RsvpServerSerial(RsvpServer): if len(data) == 0: continue + if debug: + print(f"[DEBUG] Data: {data}") + + data = data.strip() reply = super()._parse_data(data.decode()) + + if debug: + print(f"[DEBUG] Reply: {reply.encode()}") + self.ser.write(reply.encode()) self.ser.close() diff --git a/pyrsvp/rsvptypes.py b/pyrsvp/rsvptypes.py index 0cf1ba6a7ffb505fb92111300d308a25a5615084..cbd69bc49f05dcca310ed64478c48f51831c0825 100644 --- a/pyrsvp/rsvptypes.py +++ b/pyrsvp/rsvptypes.py @@ -263,7 +263,15 @@ def process_data_types(tokens: list[str]) -> dict: values = {} for token in tokens: - key, value = token.split("=", 1) + # Ignore tokens with not enough characters + if len(token) < 3: + continue + + try: + key, value = token.split("=", 1) + except ValueError: + # Ignore failed splits + continue # Find type try: diff --git a/tests/test_tcp_client.py b/tests/test_tcp_client.py index 06d3ed1c3eb33cad77ed2a4a996d38b1cb886f77..002ee06ecd6c0568cf58f0f8a27b306c55cd99f9 100644 --- a/tests/test_tcp_client.py +++ b/tests/test_tcp_client.py @@ -109,4 +109,9 @@ class TestTcpClient: def test_wrong_reply(self, client: RsvpClientTcp): with pytest.raises(RsvpNotAck): - client.send_command("STOP") \ No newline at end of file + client.send_command("STOP") + + def test_reply_string(self, client: RsvpClientTcp): + + reply = client.send_command("START") + assert reply["STATUS"] == "PASS" \ No newline at end of file