Querying Master/Standby Redundancy Status in C# .NET via SIMATIC BATCH API

Querying Master/Standby Redundancy Status in C# .NET via SIMATIC BATCH API

In SIMATIC PCS 7, querying the Master/Standby redundancy status of a SIMATIC BATCH server from an external C# .NET ERP connector cannot be achieved via a polling property on the connection object. The SIMATIC BATCH API utilizes an event-driven architecture based on COM event sinks. To determine whether the local BATCH server is Master or Standby, instantiate the ISimaticBatchServer COM interface, subscribe to the server state event notification channel (SB_EVENT_REDUNDANCY_STATE_CHANGED), and implement the event callback handler. The handler receives state constants SB_SERVER_STATE_MASTER (value 1) or SB_SERVER_STATE_STANDBY (value 2). The C# connector should cache this state in memory and direct recipe creation and batch start commands strictly to the active Master server.

Table of Contents

Executive Summary

Integrating Enterprise Resource Planning (ERP) or Manufacturing Execution Systems (MES) with SIMATIC BATCH requires bi-directional data exchange for production orders, recipe definitions, and batch execution triggers. When SIMATIC BATCH operates in a high-availability redundant server configuration, custom .NET applications using the SIMATIC BATCH API must dynamically track server roles. Sending batch execution requests to a Standby server results in command rejection or API communication exceptions. This guide provides the software architecture, COM event subscription pattern, and complete C# code example required to detect Master/Standby failover transitions programmatically.

Architecture and Fundamental Mechanics

The SIMATIC BATCH API architecture governs client-server interaction in redundant topologies:
1. Master/Standby Roles in SIMATIC BATCH:
- The Master BATCH Server holds exclusive write authority over active batches, unit allocations, and recipe execution engines.
- The Standby BATCH Server continuously synchronizes batch databases (sbdata) over Microsoft SQL Server replication and internal socket links, ready to assume mastership upon heartbeat failure.
- Client applications connecting via API must route write operations (e.g., StartBatch, ReleaseRecipe) strictly to the Master.
2. SIMATIC BATCH API COM Interop:
- The BATCH API exposes COM interfaces (registered under BatchAPI.dll and BatchObjects.dll).
- In .NET C#, these libraries are consumed via Primary Interop Assemblies (PIA) or runtime COM wrappers.
3. Event Notification Architecture vs Polling:
- Early API designs lacked a simple synchronous GetRedundancyState() method.
- Instead, the API implements a Publish/Subscribe model via connection points (IConnectionPointContainer).
- When an application registers its event sink, the BATCH API fires an initial state event (SB_EVENT_REDUNDANCY_STATE_CHANGED) containing the current state, and fires subsequent events whenever a failover or role switch occurs.

Step-by-Step Implementation Procedure

  1. Reference SIMATIC BATCH Interop Assemblies in C# Project:
    a. Open Visual Studio and load your C# .NET solution (targeting .NET Framework 4.5/4.7).
    b. In Solution Explorer, right-click 'References' and select Add Reference.
    c. Browse to the SIMATIC BATCH installation directory (typically C:\Program Files (x86)\Siemens\BATCH\bin).
    d. Add references to SIMATIC BATCH API Type Library (or BatchAPI.dll and BatchObjects.dll).
    e. Set 'Embed Interop Types' to False on the referenced assemblies.

  2. Define Server State Constants and Event Interface:
    a. In your C# code, define the redundancy state enumeration:
    csharp public enum BatchServerState { Unknown = 0, Master = 1, Standby = 2, Fault = 3 }

  3. Initialize COM Interface and Subscribe to Redundancy Events:
    a. Instantiate the BATCH Server connection object:
    ```csharp
    using BATCHAPILib;

public class BatchConnector
{
private SimaticBatchServer _batchServer;
public BatchServerState CurrentState { get; private set; } = BatchServerState.Unknown;

   public void Connect(string serverName)
   {
       _batchServer = new SimaticBatchServer();
       // Register event sink for redundancy notifications
       _batchServer.OnRedundancyStateChanged += BatchServer_OnRedundancyStateChanged;
       _batchServer.Connect(serverName, "ERP_Connector", "");
   }

   private void BatchServer_OnRedundancyStateChanged(int newState)
   {
       CurrentState = (BatchServerState)newState;
       Console.WriteLine($"SIMATIC BATCH Redundancy State Changed: {CurrentState}");
       if (CurrentState == BatchServerState.Master)
       {
           // Enable write operations
       }
       else
       {
           // Throttle write operations; wait for Master failover
       }
   }

}
```

  1. Implement Failover Handling in ERP Request Pipeline:
    a. Before executing write operations (such as _batchServer.CreateBatch(...)), evaluate CurrentState == BatchServerState.Master.
    b. If CurrentState == BatchServerState.Standby, redirect the request to the partner server or queue the message until failover completes.

  2. Clean Up COM Event Subscriptions on Application Shutdown:
    a. When the connector shuts down, unbind event listeners to prevent memory leaks:
    csharp public void Disconnect() { if (_batchServer != null) { _batchServer.OnRedundancyStateChanged -= BatchServer_OnRedundancyStateChanged; _batchServer.Disconnect(); System.Runtime.InteropServices.Marshal.ReleaseComObject(_batchServer); _batchServer = null; } }

Verification

  1. Run the C# application connecting to the active Master BATCH server: verify console outputs SIMATIC BATCH Redundancy State Changed: Master.
  2. Manually trigger a failover by stopping the primary BATCH server runtime: verify the application receives the event and logs SIMATIC BATCH Redundancy State Changed: Standby on the primary and Master on the partner.
  3. Execute a batch creation call: confirm the call succeeds on the newly promoted Master server.

Limitations and Operational Precautions

Directly issuing write API commands to a Standby server will throw COM exceptions. External applications must always verify Master state prior to dispatching command telegrams.

Engineer FAQ

Can external applications write recipes or start batches on a Standby BATCH server?

No. The Standby BATCH server operates in read-only passive mode; write commands must be sent to the Master server.

How does the SIMATIC BATCH API report redundancy state changes?

Via COM connection points that fire the 'OnRedundancyStateChanged' event whenever server roles change.

What integer value corresponds to the Master server in the BATCH API?

Value 1 represents SB_SERVER_STATE_MASTER. Value 2 represents SB_SERVER_STATE_STANDBY.

What .NET Framework version should be used for BATCH API development?

.NET Framework 4.5 through 4.8 are standard for interfacing with PCS 7 V8.x and V9.x COM libraries.

Does the C# application require a SIMATIC BATCH API license?

The SIMATIC BATCH API requires the 'SIMATIC BATCH API' license key installed in Automation License Manager on the connecting station.

Technical References

Related Articles

Related Tags

  • SIMATIC BATCH
  • BATCH API
  • C# .NET
  • Redundancy Status
  • COM Interop
  • ERP Integration
20 respuestas