Сonverting values

Good afternoon!
From the OPC server, we get a value, but in order to display it in the correct format in Ignition we need to convert it to the following algorithm:

DEC_VALUE convert HEX_VALUE
HEX/10000
HEX_VALUE convert DEC_VALUE
DEC_VALUE - The converted value

Example:
26214400(DEC) convert 1900000(HEX)
1900000/10000
190(HEX) convert 400(DEC)
400 - Voltage

Could you suggest how to implement this algorithm in Ignition.

in a script:

int("0x"+str(int(str(hex(26214400))[2:])/10000),16)

Ok, I’ll post a full solution in a moment. It’ll take a bit to type, so stay tuned…

I’d have been quicker at getting this to you, but had a line call to take care of.

I added a script module called voltage. In the module I defined a function called convert

def convert(inputValue): return int("0x"+str(int(str(hex(inputValue))[2:])/10000),16)

Then in the Numeric label, or whatever you’re using to display the number, bind the value to an expression similar to this:

runScript(concat("app.voltage.convert(",toStr({RawValue}),")"))

The {RawValue} is replaced by whatever OPC tag you are using.

Hope this helps!

EDIT: here’s the module you may import, if you wish…
EDIT#2: replace bad code with working one from below conversation.
voltage.py (66 Bytes)

Pictures! Worth a thousand words a piece! Bid on ebay! :mrgreen:

The Module Script…


…the Numeric Label…


…and binding it’s value property.


I did everything as you said

  1. Create SQL Tag L1 Phase voltage
  2. Creat Python script - DMK
  3. Create Num Label and in the field to have an expression with the argument in the form of the required tag

    But the script fails and an error

Sorry about that. Looks like I need a little more information. :scratch:

Are these true hex values you are working with? are we really dividing by 10000 or just removing the end four zeroes?

The example you gave ended up with a nice BCD-looking number. This time, though it converted to the hex value e00000, which isn’t divisible by 10000.

Ok, I took a closer look at your original post. If I had been thinking a bit more clearly (while it is afternoon for you, it’s early morning for me, and I hadn’t had any coffee yet :laughing: ), I would have noticed earlier what you were looking to do!

try replacing the function code with this:

def convert(inputValue): return int("0x"+str((hex(inputValue))[2:-4]),16)

Edited for function input

We removing the end four zeroes. And translate back into view demyatichny

Ok, that’s what I thought.[quote=“JordanCClark”]

Try replacing the function code with this:

def convert(inputValue): return int(str((hex(inputValue))[:-4]),16)

EDIT: streamlined it from previous post

Unfortunately the code is not fulfilled

def convert(inputValue):
   return int(int(str((hex(14680064))[:-4]),16)

Error

Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 3, 1, ''))

The code from the previous post is working correctly

def convert(inputValue):
   return int("0x"+str((hex(14680064))[2:-4]),16)

Sorry, there was an extra int function that has been corrected. either will work ok now. :smiley:

My coffee is still trying to start my brain…

This function works correctly, thank you very much for your help, I will continue to improve their knowledge of Python.

def convert(inputValue):
   return int("0x"+str((hex(14680064))[2:-4]),16)

No problem. I’m just sorry I had a few missteps in there for you.

The download for the function has been updated to reflect these changes.

Now it’s time for a coffee refill! :laughing:

Function now works correctly

def convert(inputValue):
   return int(str((hex(inputValue))[:-4]),16)

I would like to ask one question.
How to use this feature to build EasyChart.
The database value is stored as the number 26214400, and we need to kept in the form of the converted values ​​to the plot of the number to 231 instead of 26214400.

I don’t think you can. You can do it in a regular chart using a query similar to

select conv(left(hex(Value1),LENGTH(hex(value1))-4),16,10) as voltage1, conv(left(hex(Value2),LENGTH(hex(value2))-4),16,10) as voltage2,...etc... from table where ... etc.

Another thing you may want to consider is to have your transaction group process the raw data and store it in a useable form into the database. That way you could easily implement the easy chart.

I should have mentioned that the above query was under MySQL.

The solution to the Transaction group because before we write the variable into the database, we will change it with a written earlier functions.

Exactly! You can use the runScript function in an expression item to call the scripted function, just like you used earlier in the numeric display. :smiley: