Using dpValToString with a dyn type

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
9 posts • Page 1 of 1
nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Using dpValToString with a dyn type

Post by nmnogueira »

Hi,

I cannot use dpValToString with a dyn type.

Code: Select all

main()
{
  dyn_string dpList = makeDynString("ExampleDP_Arg1.","ExampleDP_Arg2.");
  dyn_string sVal;
  dyn_float  fVal;

  dpGet(dpList, fVal);
  
  sVal = dpValToString(dpList[1],fVal,TRUE);
}
returns an error:

WCCOAui (10), 2017.12.21 16:33:56.266, IMPL, WARNING, 50, Default branch called, FloatVar, operator=, cannot assign variable of type DYNFLOAT_VAR

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Using dpValToString with a dyn type

Post by mkoller »

ExampleDP_Arg1. is a single float value, not a dyn. Therefore you can not pass fVal as dyn_float but instead pass a single float value.
You can only pass a dyn_float when the DPE is also of this type.

RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

Re: Using dpValToString with a dyn type

Post by RudiKreiner »

So you would have to do it something like this
main()
{
dyn_string dpList = makeDynString("ExampleDP_Arg1.","ExampleDP_Arg2.");
dyn_string sVal;
dyn_float fVal;

dpGet(dpList, fVal);

for (int i = 1; i

nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Re: Using dpValToString with a dyn type

Post by nmnogueira »

Thanks! I was expecting the behaviour to be similar to dpGetDescription(), but I was wrong.

That's strange. Because the format is only defined once per DPE, regardless of it being a Dyn or not, I don't see why the dp argument needs to be a dyn. Feature request :P

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Using dpValToString with a dyn type

Post by mkoller »

The dp argument (the first one) in dpValToString() must not be a dyn, it's only a single string.

nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Re: Using dpValToString with a dyn type

Post by nmnogueira »

Yes, I understand Martin. But notice that in the first example I gave, the first argument is indeed a string (name of DPE of type float = "ExampleDP_Arg1.") and it doesn't work. It needs a string which is the name of a DPE of type dyn_float.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: Using dpValToString with a dyn type

Post by mkoller »

The point is:
When you pass a dp-Name which addresses a DPE which is of a simple (non dyn) type, then you must also pass this simple (non dyn) type as second argument.
In your first example you pass "ExampleDP_Arg1." which is of type float but you did pass a dyn_float as 2nd argument, which is wrong.
So you always need to pass the 2nd argument which corresponds to the DPE datatype.

RudiKreiner
Posts:198
Joined: Mon May 16, 2011 2:10 pm

Re: Using dpValToString with a dyn type

Post by RudiKreiner »

This is an interesting case but I think that Martin and Nuno are discussing different situations here.

In the WinCC help, the dpValToString() synopsis offers the two cases:

string dpValToString(string dp,anytype val[,bool unit=false]);
and
dyn_string dpValToString(string dp,dyn_anytype val[,bool unit=false]);

It looks to me like the second case is only for datapoints of dynamic datatypes, but not for a dynamic list of datapoints like in Nuno's first example.

What I think Nuno is trying to do in the example is a bit of a trick.
He has a bunch of datapoints that all have the same format and passes their values to the dpValToString() function in a dyn_float variable together with only the first datpoint name in dp,
expecting a dyn_string back with the float values converted to strings using the format of dp.

But the dpValToString function is quite strict here and apparently expects dp to be a dyn.. datapoint type if val is a dyn.. variable and throws the error that Nuno posted.

I find that legitimate because Nuno's "trick" above can backfire if someone does change the format of one of his datapoints,
therefore violating the assumption that he made.

So the clean way to get a dyn_string containing the properly formatted values of a list of simple (not dynamic) datapoints,
is to use the for constructor as in my previous reply.

But if for some good reason you insist on using the format of the first datapoint for all of your values, Nuno,
and accept the case where it could back fire on you or some innocent soul who might modify your software later,
you can also use my example but just change dpList to dpList[1] in the call to dpValToString().

nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Re: Using dpValToString with a dyn type

Post by nmnogueira »

Thank you all for the interesting discussion and clarifications.

9 posts • Page 1 of 1