Codesys JSON parsing variable values

TrevorBlevins

Member
Join Date
Jan 2020
Location
Sydney
Posts
1
Hello,
I have a codesys controller which I am looking to communicate with an external. User interface via mqtt.

I subscribe to a topic and recieve a JSON Payload. It contains the variable to be updated as well as the value.

Example
Code:
{"tag":"hmiStopReq","value":1}

My applications may have 10, or 100, or maybe 1000 variables that need to be updatable from the UI.

Is there a way to use the key (which is a string) as a reference to the variable that is to be updated so i can keep to code concise and not have to explicitly reference all variables.

Currently my only method of updating the value would be using a case select statement checking if the string value matches the tag. however this would need to be done for each tag.

This is the equivalent code I am using in python for an alien Bradley controller

Code:
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    payload = json.loads(msg.payload)
    comm.Write(payload['tag'], payload['value'])

Any thoughts would be appreciated
 
I have not used it but the CoDeSys store has an IIOT library that says it encodes and decodes JSON objects.
 
Last edited:
... so i can keep to code concise and not have to explicitly reference all variables.
...
The only entities that do that are the programming IDE, HMIs, and OPC servers; there are probably others. So to make it work would require a workaround.

Off the top of my head, in no particular order:

Perhaps you could get the codesys device to act as an HMI or OPC client to itself. But that is very likely to be a serious and deep rabbit hole; I looked at something similar once and found the loopback (127.0.0.1) was not allowed as the address of an E/IP server.

Apparently B&R has such a function (PLC_ReadSymInfoByName) internally, but it is not generic codesys; cf. here.

If it's in ST, a trivial external script (eXcel?) could be written to parse the declarations from text and then compose a function or paste-able code to do the work.

If the environment allows aliasing, then putting the names as aliases to elements of an array, and having another array to map from name string to index in the array, might work. An external script could again compose the second array (assuming the alias mapping is somehow exportable), but it does not save typing the aliases (alii?). Also, if there are multiple data types (integer vs. real), then there are multiple arrays.
 
Probably the best (quickest to implement) option is what you are already doing for Allen-Bradley: a minimal intermediary (e.g. RaspberryPI) to handle the MQTT communication, and then a the equivalent of that comm.Write(...) to the Codesys device.
 
I would advice following Norman's recommendation in post number 2. I have used this library although not for MQTT. Yes, it will set you back a few hundred US$ per runtime, but it covers the basics. Unless you plan to build dozens of machines, this library may save you a lot of pain and cost.
 

Similar Topics

Hello, I am using a Hitachi Micro EHV+ for a small project, and I wanted to have a Web visu, done with Codesys V3.5 SP13 Patch 2. I test the...
Replies
6
Views
294
Hello, I have a requirement to manage the text alignment dynamically. So, for example: 1. English Texts should be displayed from Left in...
Replies
0
Views
90
Hello, I am new to Codesys, and am trying to learn about it for a project we're developing. I've got a couple questions, but first a little...
Replies
1
Views
156
Hi everyone, as this is my first experience with Rockwell Software i would like to know what's the best way to make Enumerations?
Replies
10
Views
509
I am trying to get Codesys to work with a couple of Moxa ioLogik E1200 series DIO devices (E1210 and E1211). I am able to write to the E1211 DOs...
Replies
2
Views
173
Back
Top Bottom