0

I am working on a python program to receive data from TCP socket. I am successfully able to receive the data in ASCII format. Now I want to view the received values in user readable format or in integer format. How to convert these values in integer form and plot a 3D graph from the received values. Following are the continuous data I am receiving:

Output

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']"
...................................................

Please help me in the below code to show these received values to show as an integer and plotting an active 3D graph from from these received values.

Code

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

Result expected:

  1. Display the received output as an integer.

  2. Plot a 3D graph from the received coordinate of the socket.

1 Answer 1

1

Since you're receiving a list, with a list comprehension you should get the integeres like this:

integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'big') for item in data[:-2]]

This other decoding is for little endian encoding of your data:

integer_data = [int.from_bytes(bytes.fromhex(item[0:3]), 'little') for item in data[:-2]]

This will preserve your data structure and convert everything in integer, keep in mind that this is not a very efficient way to deal with this problem, because you could send the integers in the first place from the other end!

Sign up to request clarification or add additional context in comments.

9 Comments

I am receiving the following error: ``` File "C:/Users/PycharmProjects/LaserRec.py", line 77, in comm_dists integer_data = [int(item) for item in data] File "C:/Users/PycharmProjects/LaserRec.py", line 77, in <listcomp> integer_data = [int(item) for item in data] ValueError: invalid literal for int() with base 10: '00C5' ```
Oh your information is encoded in hexadecimal, let me edit the answer, hex does the trick
Now after applying the above solution, hex_data = [hex(item) for item in data] receiving the following error: hex_data = [hex(item) for item in data] TypeError: 'str' object cannot be interpreted as an integer
Now it should work, but keep in mind that you have some broken data like for example the last entry of the list contains '0217\x17'] which seems not a valid integer.
Yes,_some broken data like '0217\x17'] is creating a serious issue. It is showing error: ValueError: non-hexadecimal number found in fromhex() arg at position 5 Is there a solution so, that I can exclude the last two entry?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.