pythontcplabview

How to create Python code from LabView code


I try to rebuild an application which was made in LabView but using Python. The code should send a command via TCP/IP to a climate logger and the logger should respond. I got the code from a colleague, but he could not help me to "translate" it since he was unsure what exactly the code does, he received it from another guy... Here is a picture of the part of the LabView code, which I do not understand.

enter image description here

The code sends some sort of array, but before sending the array the u8 function is used, according to my research it transforms the string array to a bit array. Then another array is received and the final result is calculated from it. my problem is that I only receive an empty array. I assume this is since I do not know how to send the proper command. Here is what I have so far:

import socket
import os
import codecs

TCP_IP="172.21.23.56" #THESE ARE JUST RANDOM NUMBERS 
TCP_PORT=52015 #THESE ARE JUST RANDOM NUMBERS 
BUFFER_SIZE=1024

# command arrays for climate logger
#tmp_cmd = [1,16,0,0,0,0,4,2,35,16,100,0,3,10,173,4]; THIS WAS MY FIRST TRY; JUST SENDING THE ARRAY TO THE CLIENT
tmp_cmd = "116000042351610003101734"
byte_string = bytes.fromhex(tmp_cmd) 

# initializing socket and connecting with climate logger
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))


# sending command array for air temperature, receiving and processing answer
s.send(byte_string)
data = s.recv(BUFFER_SIZE)
print(sys.getsizeof(data))
tmp=((data[14]+data[15]*100)+data[16]*10000)+data[17]*1000000
print(tmp)

Solution

  • The two array constants are numeric arrays. Each one is being passed to a Byte Array to String node (with a dark blue wire entering it and a pink wire leaving it) and there is no dot on the input to show that a type coercion is taking place, so the type of the numeric arrays must be U8 i.e. each value is an 8-bit byte.

    Confusingly it looks as if the Temperature Command array constant is set to display its values in decimal (because there is a 3-digit value, which can't be a single byte in hex) but the Air Pressure Command array constant is set to display its values in hex (because there is a value 2C). If I had created this code I would have set these constants to show their radix, to make this clear.

    So I think the first command, converted to hex representation (in my head, please verify yourself), is the byte sequence

    01 10 00 00 00 00 04 02 23 10 64 00 03 0A AC 04
    

    and the second command is the byte sequence

    01 10 00 00 00 00 04 02 23 10 2C 01 03 66 74 04
    

    After the LabVIEW code sends these bytes, it also sends an End of Line constant which on Windows will be CR LF i.e. 0D 0A.

    It then waits for a response, reading up to 22 bytes or until CR LF is received whichever comes first, with a timeout of 5000 ms. The byte values at index 14, 15, 16 and 17 in the response, counting from 0, are extracted.

    With this information you should be able to reproduce the code in Python.