Converting custom Data Structure to String

Struggling with finding a solution for converting this custom data_type to a string:

image

I need to convert them to create a valid send string for my connected sigmatek controllers.

Any suggestions on how I could accomplish this would be greatly appreciated.

Hi Mateo,

You can take a look at the function PV_ninfo (B&R Online Help)

This function take the name of your variable as STRING pointer and give you back what is the datatype, the lenght in bytes and the dimension if it’s array or structure.

Using this you can identify all variables in your structure and convert them 1 by 1 and depending on the datatype.

By the way if your generated string is format as JSON there is existing library that does it, you can check about :

You can use “jsonReadVariableList” to convert your variable to JSON string and if you need to parse JSON string to PV " jsonWriteVariable"

Hope that can help you,

Florent

1 Like

Thanks for the reply I will look into the resources you have provided.

Im trying to see if there are any functions in B&R similar to this AB Function

Which can take any data structure and copy its data types into a string format, see screen shot of ladder code bellow:
image

Thanks.

What is the format of the string output by this function ?
Maybe you can give an example ?

1 Like

Essentially My data structure is composed of two data structures, which contain different data types; some bools, some sints, some ints, etc…
I need to be able to map these onto a String of length 472 bytes, so I can then send to my sigmatek controller.
Quickly drawn Diagram of what the AB copy function does.

I phrased the initial question incorrectly, my apologies.

Thanks,

Ok so if I’ve well understand you want to convert your structure to bytes array ?
I will create a little example of how you can do.

1 Like

Essentially Yes, but the data has to be passed through as a STRING[472]

I don’t know if this is already solved? Are you saying that you want the ASCII value of the variables to be copied as bytes to the string? I am not aware of any function (block) which would solve your use case. You would probaby have to write your own, here is a simple example:

	// Map BOOLs to a temporary USINT
	TempUsint.0 := SigControllerStruct.Bool1;
	TempUsint.1 := SigControllerStruct.Bool2;
	TempUsint.2 := SigControllerStruct.Bool3;
	TempUsint.3 := SigControllerStruct.Bool4;
	TempUsint.4 := SigControllerStruct.Bool5;
	TempUsint.5 := SigControllerStruct.Bool6;
	TempUsint.6 := SigControllerStruct.Bool7;
	TempUsint.7 := SigControllerStruct.Bool8;
	
	// Copy to resulting string (byte 0)
	brsmemcpy(ADR(ResultString), ADR(TempUsint), SIZEOF(TempUsint));
	
	// Copy to resulting string (byte 1)
	brsmemcpy(ADR(ResultString) + 1, ADR(SigControllerStruct.Sint1), SIZEOF(SigControllerStruct.Sint1));

This will make a byte out of your BOOLs, and then copy the numeric value as the corresponding ASCII sign to the string. The SINT is one byte already so you can just copy it directly.

Now, depending on if your structure ever changes, you could either have it hard coded like in my example, or you could make it more dynamic with pv_ninfo, to iterate through the whole structure and read the data types of each structure member.