Hello, I am new to ignition, and I am currently trying to get an RFID reader set up and working properly.
My RFID reader outputs its read values in a JSON format. Like this {"antennaPort":1,"epc":"E2801191A5030062744B2264","firstSeenTimestamp":1673884391510175,"peakRssi":-70,"isHeartBeat":false}
For the purposes of storing and retrieving relevant data I need it to be broken up into its respective fields. This is where I'm having a bit of trouble. I've tried fiddling with the TCP connection settings mainly the delimiter and field values and I have received some success from this, however it doesn't display my data correctly and still contains formatting characters. Is there a way for induction to interpret this correctly?
Use a script to parse the JSON string into a python dictionary like so
json = '{"antennaPort":1,"epc":"E2801191A5030062744B2264","firstSeenTimestamp":1673884391510175,"peakRssi":-70,"isHeartBeat":false}'
pyDict = system.util.jsonDecode(json)
print pyDict
print pyDict['antennaPort']
Thanks for the reply.
I've tried that and I'm running into issues using that script. I do not know how to properly run it. Also, it is not dynamic it's a fixed value.
Please elaborate. Do you mean you don't know where to put the script, or you are getting errors?
I'm not sure I understand what you mean. What's not dynamic, the json
string? That would be replaced with a tag read of the rfid tag that holds the raw json string.
I've tried running it through the tag value change event in my gateway events for my RIFD read value, but it doesn't seem to run whenever it does change.
I'm sorry I did not think it would automatically update based off the current tag being read.
I did manually execute the code through the script console and got this result,
{u'antennaPort': 1, u'firstSeenTimestamp': 1673884391510175L, u'epc': 'E2801191A5030062744B2264', u'isHeartBeat': False, u'peakRssi': -70}
1
Which seems to work since it prints out the antenna value which is 1.
Show your tag change script/setup. And format any code/scritps with the </>
button
#Tags Page
Script Name jsonParse
Change Triggers = Value, Quality, Timestamp
Tag Path(s) = [default]Message
################################
#Script Page
json = system.tag.read("Message")
pyDict = system.util.jsonDecode(json.value)
print pyDict
print pyDict['antennaPort']
paths = "[default]epc"
values = pyDict['epc']
system.tag.writeBlocking(paths, values)
I'm using the gateway events dialog on the ignition designer.
To get the current value of the tag you are watching, change the json
variable to
json = newValue.value
newValue
is a fully qualified value meaning it contains a value, quality, and timestamp items. The .value
part gets just the value from the qualified value.
Next you need to write these values to the correct tags. To write to a tag, you must use a system function, simple assignment scripts, ie [default]antennaPort.value = pyDict['antennaPort']
do not work.
I would loop over the dictionary, create the tag paths and write the values to those tags like so
json = newValue.value
pyDict = system.util.jsonDecode(json)
paths = []
values = []
for key,value in pyDict.items():
path.append('[Default]' + key)
values.append(value)
system.tag.writeBlocking(paths, values)
Getting familiar with system functions is important when using Ignition.
System Functions - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)
Note:
I mentioned reading the tag to be dynamic earlier, since you are using a tag change script you will have access to the tag value without reading. If you used the script in the console for testing, you would have to read the json tag first.
It works. Thanks for the help.
1 Like