parse data out of a specific TCPIP response code

57 views Asked by At

i am currently working on a TCPIP communication. Everything is working fine, i can send messages and get specific response and stuff. My problem is I want to get a feedback if the communication was good or not, for now i am getting a feedback with the command i have sendet and a response code. I want to parse the response code out of the whole string that I'm geting back ... how to do this ?

Heres what my working code looks like:

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect(("192.168.2.25", 3000))
    s.sendall(b'GetLayoutPosition:3\r\n')
    data = s.recv(1024)

print(data.decode())

after this i am receiving ->

GetLayoutPosition:1;3;10.5;11.8;0

The return values are:

  • Status, a numerical value describing the result of the GetLayoutPosition operation:
    • 1, Successful - Layout Position details returned.
    • -2, Position not found - No Job has been loaded or position is not in the Markset or Layouts Disabled.
    • -4, Out Of Range - not with in the Zero to no. of layout positions count.
    • -6, Invalid format - Incorrect number of (or badly formatted) parameters.
  • Index, Index of the layout position.
  • X, X-Position.
  • Y, Y-Position.
  • Theta, Angle of mark (degrees).

meaning layout position 3 at position 10.5 mm in X and 11.8 mm in Y with an angle 0 ...

now i want to safe the status, X and Y values out of the response.

What would be a way to do this ?

Thanks :)

i tried to search online but couldnt find a specific solution to this ...

1

There are 1 answers

0
pinky On
import re
response = "GetLayoutPosition:1;3;10.5;11.8;0"

code = {"1":"success", "2":"pnf", "4":"oor", "6":"if"}
regex = r"^GetLayoutPosition:(?P<code>\S+);(?P<index>\S+);(?P<x>\S+);(?P<y>\S+);(?P<degree>\d+)$"

match = re.match(regex, response)
result = match.groupdict() | {"code_desc": code.get(match["code"], "unknown")}

{'code': '1', 'index': '3', 'x': '10.5', 'y': '11.8', 'degree': '0', 'code_desc': 'success'}