Write Opc Tag Hex from concat string

Hi I read 14 opc tags in Hex that make up the article name. I get the string through
runscript - dec2str (DecVal):
s = str (hex (DecVal))
c1 = s (2: 4)
c2 = s (4: 6)
return chr (int (c2,16)) + chr (int (c1,16)),
with derived Tags, after concatenate the tags - concat conTag1 + conTag2, etc … and I get my string. Now I have to edit the string and write the article name on the opc tags by converting the string into 14 hex tags. I have 14 tags with value int
example:
opc tag1 = 19267
opc tag2 = 18012
conTag1 = CK
conTag2 = \ F
my string concat =" CK \ F"
If I modify the string how do I write the int value in opcTag?
Sorry I’m a beginner with ignition and, thanks to the forum and to the manual and the inductive university I managed to read tags and convert the hex value to chr and concatenate the values ​​in string but I can not divide and convert the string of 40 characters in integers and write on the opc tags. I tried split - ord - list - but I can not find the way to write the value int in the opc tag - could you help me? thank you

Hi Claudio, welcome to the forums! :slight_smile:

Consider using a script to read/write all your tags at once and make a string out of them. Jython has access to both python and Java libraries, so there’s a lot of built-in things that can make life easier for you. In this instance java has libraries to help with byte swapping. We’ll use the Short library, because in Java, a Short is a 16-bit integer.

#----------------------------------------------
# tags to string
#----------------------------------------------

import binascii
import java.lang.Short as Short

# list of tags containing your string
tags = ['[Client]Tag1','[Client]Tag2']

# read the tag values
tagIn = system.tag.readAll(tags)

# create hex string of the tag values
hexIn = ''
for tag in tagIn:
  # byte swapping.
  value = Short.reverseBytes(tag.value)
  # concatenate hexvalues (minus the '0x')
  hexIn += hex(value)[2:]

# print the result string
print hexIn.decode('hex')

Going the other way is essentially reversing the procedure. The tricky part, as you’ve already found out, is how to separate things into 2-byte chunks. I’ll admit to having used Google to figure that one out!

#----------------------------------------------
# string to tags
#----------------------------------------------

import binascii
import java.lang.Short as Short

# list of tags containing your string
tags = ['[Client]Tag1','[Client]Tag2']

# string to write
stringOut = 'CD\F'

# convert the string to a hex string
hexIn = binascii.hexlify(stringOut)

# break into a list of two-byte hex values. It just groups into characters of 4, 
# joined by a comma, then split again to make a list. Thanks Google!
hexOut = (','.join(hexIn[i: i+4] for i in range(0, len(hexIn), 4))).split(',')

# create list of integers to write to the tags
valuesOut = []
for i in hexOut:
  # byte swapping
  valuesOut.append(Short.reverseBytes(int(i,16)))

#write to tags
system.tag.writeAll(tags, valuesOut)

Hope this helps!

1 Like

Thanks a lot Jordan I read your many solutions, I tried to use the functions, but I have not yet solved, in “string to tags” I get an error
“output = binascii.a2b_hex (input)”
" TypeError: 0dd-Lenght string"
do you have any idea what the error is?
Thank you very much

Is there anything extra in the string, like a new line or whitespace?

Thanks Jordan is true are the special characters, if I try with normal characters extracts the string. While the “tags to string” does not write in the tags and does not show errors …

OK , the “tags to string” write in the tags … you’re really good, I have the problem of special characters
Thank

Glad you got it sorted out. :slight_smile: