I have an input composed of 16 digits numbers from a barcode scanner, which will be entered into a Numeric Text Field as one large number. For example, “1234567009000005”.
I need to
divide this number into four groups, such as 1234567, 009, 00, 0005.
store the groups into four variables
I was wondering if I can do this in Ignition? If yes, is it better to split that large number in Ignition or instead send it to a PLC, split it there, and send 4 tags to Ignition?
What do you mean when you say “store the groups into four variables?” Are you trying to save these values to tags? Are you trying to use these values in a script and want to attach them to variable names to use later in your script? Are you trying to push the four number groups into different displays on the same screen?
Depending on which of these you’re wanting you can use expressions or scripting to accomplish this goal.
Expression: toInt(substring(toStr({source}),0,2)) //Adjust 0,2 to the appropriate values.
Script:
source = 1234567009000005
strSource = str(source)
indes = [
[0,7],
[7,10],
[10,12],
[12,16]
]
values = []
for i in indes:
print strSource[i[0]:i[1]]
You may not want to use a numeric text field for this purpose - even if the input is all digits, splitting it into 4 pieces implies it’s not actually a number - but treating it as a number in Ignition may bite you down the line. For instance, if your leading set of 7 digits starts with a 0, Ignition will discard it (as not numerically significant) which will either cause an error, or worse, lead to incorrect values for one or more of these substrings.
Thank you very much for your response @zacht and @bpreston and @JordanCClark. I really appreciate it.
The application I am trying to do is getting a 16 digits number from a barcode scanner, split it into 4 values, and query an SQLserver 2017 database base on those 4 digits.
So basically I have to selected a row/rows from that SQL table based on those 4 values (ord no|qty|position|model).
Is there any way to select the fourth list highlighted in the picture below [1222753,1,3,1] and select its elements?
For example:
val1=1222753
val2=1
val3=3
val4=1
I am new to Jython but I tried so many things and non of them worked.
Thank you for your response!
I tried this myself but it did not work. I do not understand why this command is not getting the first element of values, even if I change the format to str I still get the error.
You would need to do it after you have built the list, so outside of the scope of the for loop.
numIn = 1222753001030001
textIn = '{}'.format(numIn)
slices = [0,7,10,12,len(textIn)]
value = []
for i, j in zip(slices,slices[1:]):
val = int(textIn[i:j])
values.append(val)
print values[0],values[1],values[2],values[3]
Just in case someone using Ignition 7.9 that uses an older version of Jython, the .format() method will not work. Below is an alternative that will work on both 7.9 and >v8
numIn = 1222753001030001
# wrong -> textIn = str(numIn)
textIn = '%016d' % numIn
slices = [0,7,10,12,len(textIn)]
values = []
for i, j in zip(slices,slices[1:]):
print i,j
val = int(textIn[i:j])
values.append(val)
print values[0],values[1],values[2],values[3]