LabVIEW Web Development Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

TCP SERVER READING

I am trying to read data through TCP read I while loop but the problem is that everytime data bytes that will receive by TCP listen is different so How can I solve this problem.

0 Kudos
Message 1 of 6
(2,219 Views)

This is going to need a lot more context to be able to provide any help.

 

For a lot of TCP communications I would expect different content to be sent every read. You'll need to include information about what you're trying to communicate with, documentation about the protocol, etc.

~ Self-professed LabVIEW wizard ~
Helping pave the path to long-term living and thriving in space.
0 Kudos
Message 2 of 6
(2,208 Views)

I am tring to read data from nodered and data which I will receive in labview TCP would be in json.

It is something like this:

[{"address":"framework/metrics/system/cpu-utilisation-percent","result":"DL_OK","timestamp":"133227638906651213","type":"double","value":29.3},{"address":"framework/metrics/system/memfree-mb","result":"DL_OK","timestamp":"133227638906655987","type":"double","value":476.578125},{"address":"framework/metrics/system/memused-percent","result":"DL_OK","timestamp":"133227638906661839","type":"double","value":30.3}]

 

in this json this value stamp changes its value everytime

kuldeepM_0-1679385985226.png

kuldeepM_1-1679386020086.png

 

0 Kudos
Message 3 of 6
(2,199 Views)

You can't just read JSON with an expected number of bytes; message lengths will vary based on numeric values and other values changing in the message. You also need to make sure you aren't crossing message boundaries (You have "}][{" at the start signifying your reads aren't aligned to message boundaries)

 

Reading JSON over TCP means the read logic has to be smart about JSON formatting. It has to be able to recognize the end of a JSON message and handle the collected JSON value at that point. This often accomplished by reading 1 byte at a time and keeping track of whether you're in a string, array, object, etc and knowing how to recognize the end of the JSON item. I'm not sure if this is standard but it also looks like NodeRED is putting a line break before every message so you may be able to just look for that character to know you're between JSON elements.

 

However, NodeRED can use MQTT so I would expect an easier approach would be to use an MQTT package to do the communication.

~ Self-professed LabVIEW wizard ~
Helping pave the path to long-term living and thriving in space.
0 Kudos
Message 4 of 6
(2,180 Views)

Thank you for the replay. I will try it with MQTT but mean if you can provide more information about how can I implement read json logic in TCP then I would appreciate it.

0 Kudos
Message 5 of 6
(2,175 Views)

You don't implement the logic in TCP, that's just how the data is moved. At a very high level you can just move whatever data comes in from TCP into some buffer and then when you determine that a full message has been received (by looking at the buffer contents) you remove the message from the buffer.

Matt J | National Instruments | CLA
0 Kudos
Message 6 of 6
(2,166 Views)