如何使用 python 将 TCP 套接字接收到的数据转换为整数?
How convert TCP socket received data in integer using python?
我正在开发一个 python 程序来从 TCP 套接字接收数据。我能够成功地接收到 ASCII 格式的数据。现在我想以用户可读格式或整数格式查看接收到的值。如何将这些值转换为整数形式并根据接收到的值绘制 3D 图形。
以下是我收到的连续数据:
输出
received "['0216', '0212', '0211', '020B', '0214', '020E', '0211', '0210', '0206', '0208', '0206', '0207', '0200', '0208', '0203', '0000', '0200', '0000', '0200', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '01FE', '0000', '0000', '0000', '0000', '0000', '0203', '01FF', '0202', '01FF', '0203', '0202', '0207', '0205', '0207', '0213', '0209', '020C', '0212', '021A', '0217\x17']"
received "['0212', '0210', '0212', '020D', '020D', '020C', '020F', '0206', '020B', '020B', '0204', '0205', '0201', '0000', '0201', '01FE', '01FE', '0000', '01FE', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0200', '0200', '0200', '01FC', '0000', '0202', '0201', '01FE', '0209', '0207', '0208', '020B', '020A', '020B', '020C', '0212']"
...................................................
请在下面的代码中帮助我显示这些接收到的值以显示为整数,并根据这些接收到的值绘制活动的 3D 图形。
代码
from __future__ import print_function
import socket
import sys
import time
from time import sleep
start = time.time()
def comm_dists(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (ip, port)
print(sys.stderr, 'connecting to %s port %s' % server_address)
sock.connect(server_address)
print("Connected")
try:
# Send data
message = b'\x02\x02\x02\x02\x00\x00\x00\x10sMI 0 3 F4724744 '
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b"\x02\x02\x02\x02\x00\x00\x00\x06sMI 5E'"
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x05sMI 2e'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x07sEI A 1\x0f'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x06sRI C5>'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
n = 52
while True:
data = sock.recv(5000)
data = data.decode("utf-8", errors='ignore')
data = data.replace('\x02', '')
data = data.replace('\x00', '')
data = data.replace('\x05', '')
data = data.replace('sRA', '')
data = data.replace('sSI', '') #comandAns
data = data.replace('000A', '') #distResolution
data = data.replace('425C0000', '') #startAngle
data = data.replace('3E800000', '')
data = data.replace('0118', '') #angularStep
data = data.split()
print('received "%s"' %data)
finally:
print(sys.stderr, 'closing socket')
return data
i=0
while i < int(1):
data = comm_dists('192.168.0.1', 2111)
i=i+1
预期结果:
将接收到的输出显示为整数。
根据接收到的插座坐标绘制 3D 图形。
由于您收到的是列表,通过列表理解,您应该得到这样的整数:
integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'big') for item in data[:-2]]
此其他解码用于对您的数据进行小端编码:
integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'little') for item in data[:-2]]
这将保留您的数据结构并将所有内容转换为整数,请记住,这不是处理此问题的非常有效的方法,因为您可以首先从另一端发送整数!
我正在开发一个 python 程序来从 TCP 套接字接收数据。我能够成功地接收到 ASCII 格式的数据。现在我想以用户可读格式或整数格式查看接收到的值。如何将这些值转换为整数形式并根据接收到的值绘制 3D 图形。 以下是我收到的连续数据:
输出
received "['0216', '0212', '0211', '020B', '0214', '020E', '0211', '0210', '0206', '0208', '0206', '0207', '0200', '0208', '0203', '0000', '0200', '0000', '0200', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '01FE', '0000', '0000', '0000', '0000', '0000', '0203', '01FF', '0202', '01FF', '0203', '0202', '0207', '0205', '0207', '0213', '0209', '020C', '0212', '021A', '0217\x17']"
received "['0212', '0210', '0212', '020D', '020D', '020C', '020F', '0206', '020B', '020B', '0204', '0205', '0201', '0000', '0201', '01FE', '01FE', '0000', '01FE', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0000', '0200', '0200', '0200', '01FC', '0000', '0202', '0201', '01FE', '0209', '0207', '0208', '020B', '020A', '020B', '020C', '0212']"
...................................................
请在下面的代码中帮助我显示这些接收到的值以显示为整数,并根据这些接收到的值绘制活动的 3D 图形。
代码
from __future__ import print_function
import socket
import sys
import time
from time import sleep
start = time.time()
def comm_dists(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (ip, port)
print(sys.stderr, 'connecting to %s port %s' % server_address)
sock.connect(server_address)
print("Connected")
try:
# Send data
message = b'\x02\x02\x02\x02\x00\x00\x00\x10sMI 0 3 F4724744 '
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b"\x02\x02\x02\x02\x00\x00\x00\x06sMI 5E'"
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x05sMI 2e'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x07sEI A 1\x0f'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
message = b'\x02\x02\x02\x02\x00\x00\x00\x06sRI C5>'
sock.sendall(message)
print(sys.stderr, 'sending "%s"' % message)
print('sent "%s"' % message)
data = sock.recv(5000)
print(sys.stderr, 'received "%s"' % data)
sleep(0.05)
n = 52
while True:
data = sock.recv(5000)
data = data.decode("utf-8", errors='ignore')
data = data.replace('\x02', '')
data = data.replace('\x00', '')
data = data.replace('\x05', '')
data = data.replace('sRA', '')
data = data.replace('sSI', '') #comandAns
data = data.replace('000A', '') #distResolution
data = data.replace('425C0000', '') #startAngle
data = data.replace('3E800000', '')
data = data.replace('0118', '') #angularStep
data = data.split()
print('received "%s"' %data)
finally:
print(sys.stderr, 'closing socket')
return data
i=0
while i < int(1):
data = comm_dists('192.168.0.1', 2111)
i=i+1
预期结果:
将接收到的输出显示为整数。
根据接收到的插座坐标绘制 3D 图形。
由于您收到的是列表,通过列表理解,您应该得到这样的整数:
integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'big') for item in data[:-2]]
此其他解码用于对您的数据进行小端编码:
integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'little') for item in data[:-2]]
这将保留您的数据结构并将所有内容转换为整数,请记住,这不是处理此问题的非常有效的方法,因为您可以首先从另一端发送整数!