LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Structure through tcp comm

I have a structure having char  and int vaiable and i want to send through tcp comm . How to do this??

0 Kudos
Message 1 of 8
(3,494 Views)

For such a simple structure, the simplest way to solve your problem is to format a string with individual struct field values (sprintf) and send that through TCP, scanning out values in the target app (sscanf).

 

But if you have a more complex struct or if you want to abstract from actual struct definition you could do something as follows:

myStruct	str;

// Create a string with struct content
Fmt (msg, "%*i[zb1w2p0r16]", sizeof (myStruct), (char *)&str);

this will create a string of alphanumeric characters that can be sent through TCP.

 

The target application will rebuild the struct content with:

int		i, ch;
myStruct	str;

// Rebuild original structure
if (strlen (buffer)) {
	for (i = 0; i < strlen (buffer) / 2 - 2; i++) {
		Scan (buffer, "%s[i*w2]>%i[r16]", i * 2, &ch);
		sprintf ((char *)&str + i, "%c", ch);
	}
}

Just as an example, this struct:

typedef struct {
	char	name[5];
	char	channel[10];
	int		max;
} test;

with name = "ABC", channel = "Channel1" and max = 123 gives the following string:

"41424300eb4368616e6e656c310014007b000000"

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 8
(3,461 Views)

Why alphanumeric?  Your fixed-length flat structure can go through tcp as binary.

0 Kudos
Message 3 of 8
(3,449 Views)

It's been a while since I used tcp for interprocess communication  I remember I had problems sending raw bytes sequences that included null bytes so I resolved to go that way. ClientTcpWrite accepts strings and that one is a plain string. 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 8
(3,438 Views)

Woah! Seems it's been very long time since I used those functions, and my memory failed! Here is a message I posted a while ago that says exactly the opposite: you can actually send by nary data with null bytes embedded. 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 8
(3,437 Views)

I still think that, for small pieces of data, sending it via text is not bad advice.

 

You can also send binary data, of course, but you should exercise some caution with issues such as the byte of order of integers, or structure padding, especially if there are different computer platforms involved.

Message 6 of 8
(3,404 Views)

If you're going with text, get a JSON library and send standardized JSON, rather than an ad hoc custom format.

Message 7 of 8
(3,387 Views)

As Luis has pointed out already, endianess is a concern if you use binary formats. Other than that TCP is fully 8 bit transparent so there is nothing in TCP, IP, UDP that would make trouble with binary data.

 

What you obviously have to watch out is to not use any C string formatting and processing routines on that data. That is where the issue with NULL bytes in a data stream comes from.

 

Another issue you have to consider is compatibility. Changing a binary data stream once you distributed your app, is a non-trivial exercise for sure. It doesn't scale well in that respect and can create a maintenance nightmare. Depending on the used JSON or similar library this can be much much easier there, as the better ones do not require the data of named tuples to be in the same order as in the binary structure and can be instructed to drop unsupported message elements.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 8
(3,344 Views)