The easiest way to convert a TCP/IP stream into a cluster

I have some existing code that takes a stream of bytes from a TCP/IP connection and converts it into a cluster. It works fine but I think it’s going to become unmanageable as new fields are added to the network packet protocol

Original Byte Parser

While looking around for a better solution I found this thread on NI’s forums that discusses different conversion methods. There are a number of options suggested here, including using Type Cast, Join Number or Unflatten From String

I’ve chosen to use Unflatten from String because it can convert the whole network packet in a single node, and also handles big or little endian conversion

Unflatten From String help image, showing Binary String input, boolean Data Size input where T means the binary string has a size header, and Byte Order enumeration input
Unflatten From String

New and Improved

This is the improved version, which replaces my custom “4 Little Endian Bytes To …” conversion VIs with Unflatten from String to convert the byte stream into a cluster. In this case the ‘data includes array or string size’ boolean is False, as the data doesn’t have a size header. This header would only exist if the byte string was generated by Flatten To String

Improved Byte Parser
Raw Network Packet cluster typedef.  This cluster contains a numeric control of the
Raw Data Packet

I’ve added a new cluster typedef to define the format of the raw data coming in from the network, which is shown to the left

The packet format includes a number of reserved ‘Padding’ bytes which are currently unused, but still need to be parsed to get to the data further into the packet

I’ve set up the default value for each control as its offset in the packet. This lines up with the documentation for the network protocol, and will make this easier to understand next time I need to work on the cluster.


Subscribe to our newsletter

Did you find this post helpful? Sign up to the Lonely Ant newsletter to get more useful tips delivered directly to your mailbox

Unsubscribe at any time. I will not share your information with third parties.

* indicates required