B&R Funktion BOOL_TO_STRING

Hello everyone,

I use the B&R function BOOL_TO_STRING to convert Boolean values into strings. Basically, this works perfectly. The generated string is then transferred to a JSON script and processed there.

However, BOOL_TO_STRING returns the values “TRUE” and “FALSE” in uppercase letters. The JSON script only accepts ‘true’ and “false” in lowercase letters, which results in an error message.

Since the B&R function cannot be customized and a self-created FUNCTION cannot have STRING as a return value, I am looking for an elegant solution via a function call.

Does anyone have any tips on how to solve this in AS?
Many thanks in advance.

Best regards,
Ronny

Hi,

I would propose just to build your own string, for example:

IF myBool = TRUE THEN
   myBoolString := 'true';
ELSE
   myBoolString := 'false';
END_IF

This could also be packed into a own library function to re-use it as often as needed.

Best regards!

Please have a look at this BrLib library

no words, thanks @alexander.hefner, colleagues have developed ToLower function, but totally true in this case a simple condition it’s enough.
Ciao
Valerio

Thank you for your suggested solution.

Of course, this can be implemented in Cyclic, but it’s “quite a lot of lines”
for what is actually a fairly simple function.

The question I have is why B&R can provide a function with the return value String if I can’t implement it myself.

A function has the advantage that I could solve it nicely in one line.

I will write a funktion block it …

You can create a function and pass as input a pointer to a string and then set the string value inside of the function.
Btw what’s the final goal? You want to convert a process variable to json?

Hi,

I’m not sure if I understood it right: is your question about the return type of a function?

If yes, you’re right, the return type of a user function cannot be a STRING. I’m not aware of the exact details behind that, but as I know some of those “look alike”-functions are not really functions but are handled different by the backend parser and compiler (they are handled in background more like “defines” then like functions).

But you could achieve something similar wit a user library by:

  • using a function but passing your target string by a pointer (address to the string declared outside of the function)
  • using a function block instead of a function and passing the string as VAR_IN_OUT

I’ll try to make 2 examples of what I mean, but I’m not sure if I can finish them today.

Best regards!

Yes, it’s the return value… :slight_smile:
But I’ll solve it with a function block.

Okay, thanks for clarifying, great to hear that you can solve it by a function block!

Just for the case if someone else has a similar need, he’re the 2 examples:

  • fc_BtoS as function using a pointer
  • fb_BtoS as function block using a VAR_IN_OUT

Declarations in the user library:

Function code:

Function block code:

And calling example:

And the sample library + task code:

TstBtoS.zip (1.3 KB)
myBtoSLib.zip (2.1 KB)

Best regards!