简体   繁体   English

从特定的 TCPIP 响应代码中解析数据

[英]parse data out of a specific TCPIP response code

i am currently working on a TCPIP communication.我目前正在研究 TCPIP 通信。 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.我的问题是,如果沟通好坏,我想得到反馈,现在我得到的反馈是我有 sendet 的命令和响应代码。 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:状态,描述 GetLayoutPosition 操作结果的数值:
    • 1, Successful - Layout Position details returned. 1、成功——布局Position详情返回。
    • -2, Position not found - No Job has been loaded or position is not in the Markset or Layouts Disabled. -2、未找到 Position - 未加载任何作业或 position 不在 Markset 或布局禁用中。
    • -4, Out Of Range - not with in the Zero to no. -4,Out Of Range——不在零到无。 of layout positions count.布局位置计数。
    • -6, Invalid format - Incorrect number of (or badly formatted) parameters. -6,格式无效 - 参数数量不正确(或格式错误)。
  • Index, Index of the layout position.索引,布局索引 position。
  • X, X-Position. X,X 位置。
  • Y, Y-Position. Y,Y 位置。
  • Theta, Angle of mark (degrees). Theta,标记角度(度)。

meaning layout position 3 at position 10.5 mm in X and 11.8 mm in Y with an angle 0...意思是布局 position 3 at position X 方向 10.5 mm,Y 方向 11.8 mm,角度 0...

now i want to safe the status, X and Y values out of the response.现在我想从响应中保护状态、X 和 Y 值。

What would be a way to do this?这样做的方法是什么?

Thanks:)谢谢:)

i tried to search online but couldnt find a specific solution to this...我试图在线搜索但找不到具体的解决方案...

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'} {'code': '1', 'index': '3', 'x': '10.5', 'y': '11.8', 'degree': '0', 'code_desc': 'success'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM