I’m currently working with Automation Studio 6.7.1.5 and trying to understand how ST OOP classes can be shared across multiple programs within the same project.
I would like to declare a class only once (for example, LogManager) and make it accessible from any program in my project, including its public methods, properties, and members.
Ideally, I would like to declare a global instance and access it from different programs:
// Program A
gLogger.Info(...);
// Program B
gLogger.Warning(...);
// Program C
gLogger.Error(...);
However, it seems that .cls files must belong to an ST OOP Program and that classes are only accessible within their containing program.
My questions are:
Is there a way to declare an ST OOP class at the project level so it can be used by multiple programs?
Can I declare a global instance of an ST OOP class and access its public methods from different programs?
If this is not currently supported, is there a recommended workaround or any plan to support it in future AS6 versions?
I’m trying to build a modular, reusable architecture without having to duplicate class definitions or create separate ST OOP programs just to expose functionality.
Hello! I’m not privy to any future features, so anyone with updated knowledge, please correct me.
OOP ST gets tran-spiled to C++. My understanding as of today is that it has the same rules as C++, which is that objects cannot cross task boundaries. There are memory constraints that make this the case in the current underlying C++ implementation where each task has its own heap, which makes memory management across tasks separate. I don’t know if there are other reason or if there are plans to change this, but you do need be careful with any work arounds, which there are many, with varying degrees of acceptability.
To communicate across tasks you can use global variables, just like any other task, but not with OOP objects. If you have used C++ it is a very similar pattern, with OOP ST having the benefit of supporting the watch window and other B&R services out of the box.
As far as sharing classes across tasks, (I assume, but have not tried) that you can reference the ST files in your task so that you have one source and the others can use the same code, but again, they are not in fact the same actual class, so they can’t directly communicate since it’s essentially a copy of the class. In your case of the logger, the reference file could work fine, but it wouldn’t be a global, each would get their own instance and it would mainly be a wrapper that talks to the global structure scope, or in the case of a logger, just a wrapper around the event logging system.
From a global perspective each object in B&R’s current ST OOP implementation is just a data structure, so no member function that can be called (only locally).
Implementation files: Containing the actual implementation of functions, classes, methods, etc.
Declaration files: Where I can declare variables and instantiate the types, function blocks, or classes defined elsewhere.
What I’m trying to achieve in Automation Studio is something similar to what I’ve used in other development environments.
Ideally, I would like to:
Define and implement a module only once, using dedicated definition and implementation files.
Instantiate and use that module from a declaration file, such as <Name>.var, without duplicating its code.
Organize related files into folders, allowing me to group modules and components into a clean, reusable exportable structure.
Ideally, package these modules into precompiled libraries that could simply be linked to other projects, without having to include or recompile the source code each time.
My goal is to build a modular, reusable, and maintainable architecture, where each component is clearly separated from the application code that uses it.
I would appreciate knowing whether Automation Studio supports this approach and, if so, what the recommended project structure would be.
Ultimately, having the ability to create and distribute compiled libraries with well-defined public interfaces would be the ideal solution, allowing us to reuse components across multiple projects while keeping their implementation encapsulated.
It seems that enumerations as class mebers are not yet supported …
Unfortunately, we are running a bit behind schedule with the development of ST OOP. This may come as a surprise to some.
However, we have integrated C++ very well into the existing runtime system over the years.
We therefore felt that we didn’t need object-oriented programming in IEC since we had already integrated the market-leading language.
We are well on our way to correcting this, and Automation Studio 6.9 will bring us another step forward in December.
with ST OOP, the current state is that not everything has been implemented at the language level yet.
This is not the case for C/ C++. The GNU C++ compiler runs in the background, so there are no real language limitations.
However, the runtime system behaves somewhat differently. As soon as you leave the C++ language level (e.g., watch window, trace, OPC UA, connection with the IEC language environment, etc.), C++ objects no longer exist.
Instead, in AR there are only IEC variables and datatypes. An ‘object’ would be currently implemented as plain structure that does not support method calls.
If you can accept breakpoints in software development then everthing is there since GDB works in background.
Possible approaches include:
Writing the entire project in C++. If debugging via breakpoints is not possible, copy or reference the data to IEC variables.
Writing individual tasks in C++ and connecting them to other tasks via a standard IEC variable interface. Two separate C++ tasks can communicate using pointers (IEC variables) and thus reference actual C++ objects.
Writing function blocks in C++ and using them in IEC languages through the standard IEC function block interface.
I think the online help is a good place to start: ANSI C/C++