I would like to know how to convert a value in decimal, that comes from opc (this means for example decimal 3, in boolean 00000000 00000011) to float format cause it's really a real (or float) value.
Thanks!
I would like to know how to convert a value in decimal, that comes from opc (this means for example decimal 3, in boolean 00000000 00000011) to float format cause it's really a real (or float) value.
Thanks!
Show us the details of how this value comes in (most drivers will interpret as floating point for you if you tell them to).
In the origin (a CNC machine control) it's a REAL value (or float).
Later, this values is got by a Siemens gateway through mqtt (as picture attached) to publish it as opc ua.
In Siemens gateway, it's defined this value as float (as shown in picture, yellow marks) but gateway iterprets this value as decimal, instead of a float. (I think this is a bug of Siemens. I'll let them know)
As a result, I'm trying to change this type of value (decimal to float) in Ingnition, which is susbcribed to this Siemens gateway where the value is. And that's the reason why I need to convert from decimal to float, or maybe decimal-boolean-float, I don't know.
Thanks in advance,
Benito.
It may need to be done via scripting, using Float.intBitsToFloat
>>> from java.lang import Float
>>> Float.intBitsToFloat(1125515264)
150.0
>>>
Hi Kevin!
I'm trying it but it doesn't work. The truth is this is the first time I import resources into code ("Float"). Could you tell me what I`m doing wrong?
The tag "MemoryGH2055_maq" has a script as shown for writing its value in "MemoryGH2055" but as float.
Thanks in advance.
Looks like a missing indent on line 2. Look in the right margin of the code editor for little red marks at syntax errors.
Hi both!
Pturmel, you're right and I had already fixed the mistake, but also I had to define origin value as INTEGER and final value as FLOAT.
And now yes, it works!
Thanks so much!!
You need to create a new OPC tag for the value that you want to convert. In the new tag, select the OPC client and the OPC value that you want. That fix you problem and is more clear and robust way.
Hi Kevin,
I have a similar scenario where I have one float tag that it is split in two different OPC tags.
I was able to use your code in a tag event script to join them and it works ( on a UDT).
I have +1000 tags like this.
How this Tag Event script will impact in the performance of the gateway?
My code is:
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
from java.lang import Float
values= system.tag.readBlocking(["[.]HMI[5]","[.]HMI[4]"])
value = values[0].value <<16 | values[1].value
valuefp= Float.intBitsToFloat( value)
system.tag.writeBlocking(["[.]../SIM_AI"],[valuefp])
You could probably handle that by making SIM_AI
an expression tag, using the other 2 in its expression.
In the script library:
from java.lang import Float
def convert_to_float(int_val):
return Float.intBitsToFloat(int_val)
expression:
runScript(
'test.convert_to_float', 0,
{[.]sources/source}[0] << 16 | {[.]sources/source}[1]
)
I think it's cleaner/easier to follow, as it makes SIM_AI
responsible for computing its value, and I'm willing to bet it's also faster.
edit: Wait, it seems the tag doesn't update when the source values change. I'm thinking it's because they're in an array...
edit2: nevermind, it did change, but the formatting on the final tag was hiding it.