From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unflatten from string or Type Cast ?

Solved!
Go to solution

Hi. 

 

I am sending an array of 8-bit unsigned integer with the size 19203 from a TCP server in a c-program to a TCP client in LabVIEW. I have managed to receive the data as shown in the picture below. The first byte, that is sent by the c-program is the header. The second and the third byte represent the size of the array, which is actually 160x120. Then the rest 19200 bytes are the actual values for the array. 

 

Untitled.png

 

 

My TCP client code looks like this: 

 

Untitled.png

 

The issue occurs when I'm replacing the Type Cast with the Unflatten to string function, and adding a dimension to both arrays. The first two values in the array (120 and 160) should inform the Unflatten to string function about the size of the array, but I don't get any data in the array indicator.  Is there any way to create a 160x120 sized 2d array from 8-bit uint data ?. 

Best regards

Oesen
0 Kudos
Message 1 of 9
(3,693 Views)
Solution
Accepted by topic author Oesen

Oesen,

 

Your client code with the 1D array gets the data, just not in the 2D format, right? If so, use Reshape Array. This also can adapt automatically to any size of 2D array where the first two elements of the 1D array contain the dimensions of the 2D array.  I put the code outside the loop just because there was space on the BD there. It can be moved inside if you get multiple messages.

 

Lynn

 

Reshape.png

Message 2 of 9
(3,680 Views)

Can this one be used for at color image btw ? 

 

Untitled.png

Best regards

Oesen
0 Kudos
Message 3 of 9
(3,652 Views)

I do not have the Vison stuff, so I cannot say.

 

Lynn

0 Kudos
Message 4 of 9
(3,647 Views)

@Oesen wrote:

Can this one be used for at color image btw ? 

 

Untitled.png


Yes. That is "IMAQ ArrayToColorImage" which writes a 2D array of U32 to an image.

 

0 Kudos
Message 5 of 9
(3,641 Views)

The problem is that I'm always getting blue images as a result 

Untitled.png

 

 

 

And I have chosen a 32-bit RGB image in the image create. I'm generating random numbers from a c-program from 0-4,294,967,295 and sending them directly to this LabVIEW Vi. I don't really understand, why it always creates an image with the same color, everytime I'm generating new pixel values. 

 

The Vi is like this: 

Untitled.png

 

 

Best regards

Oesen
0 Kudos
Message 6 of 9
(3,633 Views)

Please stop posting variations on the same code in so many places. I already explained here that it will not work to read a single byte and type cast it to a 4-byte value; you'll get the wrong result, and will then read the wrong number of bytes following. If your C code generates a continuous stream of numbers, and doesn't send a length first, then you should do a single TCP Read for the correct number of bytes. It doesn't help to copy examples blindly if you don't take the time to understand what they do. Also, your code reads and discards data until the stop button is pressed, at which point it tries to convert whatever random data was read most recently into an image. Does your C code send the array dimensions? If not, then your array manipulation won't work. In what format does your C program send data?

0 Kudos
Message 7 of 9
(3,619 Views)

I'm sending an array of 76812 bytes from my c-program.

 

The first four bytes is the header, I defined it to be "1"

 

The second four bytes is the array dimension "160" 

 

The third four bytes is the array dimension "120"

 

The last 76800 bytes is the randomly generated uint32 pixel values from 0 to 0-4,294,967,295. 

 

 

 

 

Best regards

Oesen
0 Kudos
Message 8 of 9
(3,607 Views)

Now you are giving completely different specifications.  You now have everything coming in four-byte chunks rather than the single bytes mentioned earlier.

 

This code will handle conversion of a string to U32.

 

Lynn

0 Kudos
Message 9 of 9
(3,596 Views)