DMC, Inc.

Improving Optix Data Integrity with Variable Change Events and Custom C# Functions

Data integrity is critical in most automation applications. Standard PLCs typically cannot detect when external software is reading or writing their tags, creating the potential for data loss or inconsistencies. FactoryTalk Optix makes it possible to mitigate this issue with a function that creates Handshaking via C# scripting. This is especially useful for ensuring data integrity when equipment data initiates the action, such as needing to write a PLC data tag change to a database. Or, conversely, when an external function is handled by or passed through an Optix C# function to write data to a PLC, ensuring the data is received.

DMC engineers Ben Clare and Otto Gottlieb presented on this topic at this year’s Rockwell Automation Fair. This blog takes a deeper look at how C# scripting and handshaking can help maintain data integrity in two scenarios: when a PLC tag change initiates an action in Optix, and when Optix initiates an action and confirms that the PLC received it.

Using a PLC Tag Change to Trigger a Handshake in Optix

In our first example, we use a model object variable called “PhaseL1Filling.StartSent” of type “Phase” as our base. This variable is bound to a PLC tag and will update its value in Optix when the corresponding PLC tag changes. A variable change event function is applied to this variable that will call a function called “StartPhase” of the “StartPhaseService” class from within the custom Optix C# Netlogic when the variable value changes.

Diagram showing a PLC tag change initiating an action Optix.

Looking inside the “StartPhase” function, we see that the Handshake function is called after the variable is extracted from the object that initiated “StartPhase.”

FactoryTalk Optix Phase object (PhaseL1Filling) with a change event configured on its StartSent property.
C# snippet defines an exported method (StartPhase) in FactoryTalk Optix (NetLogic) that retrieves a Phase node and triggers a handshake/task execution.

Because this function is labeled with the [ExportMethod] tag, it can be called directly from the Optix application as it is being used. If we take a look at the class itself, we see that we need to set the actual task that is run by the handshake function. This can be any function, from writing to a database to setting another tag, to basically anything that can be executed in a C# function. However, this class is derived from the “BaseEnumerationHandshakeService” class, so we need to investigate it further to find out what it is doing.

C# code defines StartPhasesService, which inherits from BaseEnumerationHandshakeService and overrides TaskToRun to record a phase start time in the database.

Within this base class, it’s important to note that upon receiving a handshake value of 1-9 (arbitrary range in our case), we begin to run the task that we assigned in our derived class by calling the function “RunTaskAndSetReceived”. This function also sets the received bit back to the PLC, so it knows that the function has been completed.

ProcessHandshakeAndRunTask handles handshake states to reset a status bit or launch a task.
C# RunTaskAndSetReceived method executes a task and updates the received bit.

Going back to the “ProcessHandShakeAndRunTask” function, now that we ran our task and set the received bit, the final part of the handshake gets received from the PLC (a value between 10 and 19 in this case), telling us that the PLC acknowledges that it has received the signal from Optix that the task has been run. We then reset that bit in Optix and complete the handshake procedure, ensuring all stages of the process have been accounted for and acknowledged. This, of course, is a general overview of this specific implementation and clearly does not cover all of the error handling that occurs.

C# code validates the PLC handshake and resets the received bit.

Using Optix to Integrate a Handshake with the PLC

In our second example, the trigger for our handshake to begin is not tied to a variable bound to a PLC tag, but rather starts with Optix itself. We will use the “PalletPrinter” as our example object, and we can see that the sent and received bindings are swapped from our previous example. Since Optix, in this case, starts the handshake and initiates the procedure, it writes the sent variable to the PLC to begin and executes the function in the backend C# logic.

Diagram showing Optix initiating a tag change action on a PLC.
PalletPrinter1 configuration.

Looking inside the base class the scanner C# backend code is derived from, we see that we don’t wait for a handshake value to begin the task; instead, we run it straight away and immediately send the handshake to the PLC in this case.

RunTaskAndSendHandshake runs an async task and sends a handshake to the PLC.

Then, when the PLC receives the handshake value, it sends back an acknowledgment via the “ScanReceived” variable, which initiates the “ProcessPrinter1ReceivedHandshake” function, derived from the base class’s “ProcessHandshake” function. This function simply takes in the “ScanReceived” variable and resets the Handshake sent bit, completing the Handshake process.

ProcessHandshake processes a handshake signal and resets the sent bit.

Ensuring Data Integrity with FactoryTalk Optix Handshaking

Between these two examples, you can see how custom C# functions and handshaking can help maintain data integrity between FactoryTalk Optix and a PLC, regardless of which side initiates the process. When a PLC tag change triggers an action in Optix, a handshake can verify that the requested task was completed before the process moves forward. Conversely, when Optix initiates an action, the same approach can confirm that the PLC received and acknowledged the data.

While the exact implementation will depend on the application, this handshake structure provides a flexible framework for coordinating data transfers and confirming that each step is completed as expected. Variable Change Events and custom C# functions in FactoryTalk Optix can be adapted for tasks ranging from database operations to PLC tag updates and other automation workflows.

Ready to start your next automation project? DMC can help!

Learn more about DMC’s PLC and FactoryTalk Optix programming and reach out to our Automation experts today.