LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Python client to unflatten received string from TCP Labview server

Solved!
Go to solution

I think I backsaved it to 2015 now.

 

I think this is how you mean I should do it. The reverse before send of the int is taken care of in the code by different endianness setting of python struct.unpack command.

labviewforum.PNG

Download All
0 Kudos
Message 31 of 35
(3,742 Views)

Well your real problem is that you use double precision float in LabVIEW but tell the Python struct.unpack function that it should use floating point aka. single precision float.

Either change the LabVIEW data to a single precision float before you do the flattening or change the Python format specifier to d instead of f. Your problem also stems from the fact that you artificially prepend the byte count to the data instead of the element count that LabVIEW would create if you had "prepend array size to data" set to true. In that case you would have sooner than later noticed that you send 8 times the amount of elements in bytes but only receive half of that with your 4 byte element size assumption.

 

But some debugging would even in your code have revealed that you send n elements on the LabVIEW side but end up with 2 * n elements in the Python array. If you do messaging between different environments, and especially when it involves flattened binary data, you really need to make sure that you have investigated every single aspect of the problem before claiming that a proven software like LabVIEW or Python (it could have bugs too, you know) is the problem.

 

Also why did you mark your post, where you say that the suggestions from others do not work, as a solution? There is nothing that could be considered a solution in that post.

 

Flatten String.png

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 32 of 35
(3,698 Views)

Yes I just noticed that I uploaded a python code with 'f' as format identifier. This of course has to be 'd' for an 64 bit IEEE 754 double precision floating point number. I couldn't get it to work reliably with the flattening via before send. So I think a more stabke solution is my approach with the fractional representation, which I am recommending. 

 

Thank you very much for your effort!

0 Kudos
Message 33 of 35
(3,693 Views)
Solution
Accepted by topic author maik.holzhey

Ok I am taking it back. You are correct I lost my self not very precise thinking and working. Flattening solution also does work now. I am correcting this now. Thanks. It is much nicer.

Download All
Message 34 of 35
(3,683 Views)

This code works for me. UDP server accept flattened dbl array x, return x+1 to port 6503. Modify LabView UDP client to your needs.

import struct
import socket
import numpy as np

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

#bind_ip = get_ip()

print("\n\n[*] Current ip is %s" % (get_ip()))
bind_ip = ''
bind_port = 6502

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind((bind_ip,bind_port))  

print("[*] Ready to receive UDP on %s:%d" % (bind_ip,bind_port))

while True:
    data, address = server.recvfrom(1024)
    print('[*] Received %s bytes from %s:' % (len(data), address))

    arrLen = struct.unpack('>i', data[:4])[0]
    print('[*] Received array of %d doubles:' % (arrLen,))
    x = []
    elt = struct.iter_unpack('>d', data[4:])
    while True:
        try:
            x.append(next(elt)[0])
            print(x[-1])
        except StopIteration:
            break
    x = np.array(x)
    y = x+1 # np.sin(x)
    msg = data[:4]
    for item in y:
        msg += struct.pack('>d', item)
    print(msg)
    A = (address[0], 6503)
    server.sendto(msg, A)
    break

server.close()
print('[*] Server closed')
print('[*] Done')

LabView UDP client
LVclient.PNG

 

 

0 Kudos
Message 35 of 35
(3,008 Views)