OPC Client Documentation

Overview

The OPC Client is designed to facilitate communication with OPC servers using the OPC UA (Unified Architecture) protocol. This library provides a high-level API for reading and writing tag values to a PLC (Programmable Logic Controller).

Features

Class Definition

OPCClient

The OPCClient class manages connections and communications with an OPC server. It is designed as a singleton to ensure that only one connection per server endpoint exists within an application.

class OPCClient: (the most important methods)
    def read(self, tag: List[str] or str, db_name: str = '', namespace: str = '3'):
        # Read data from a tag
        pass

    def write(self, value, var_type: ua.VariantType, tag: List[str] or str, db_name: str = '', namespace: str = '3'):
        # Write data to a tag
        pass

    def disconnect(self, force: bool = True) -> None:
        # Disconnect from the server
        #
        # If force is True, disconnect immediately
        # When {"keepalive": true} is set in configuration it is also strongly recommended to call this after finishing using the client
        pass
    

Configuration

Configuration of the OPC Client is handled via a JSON file which specifies server URLs, timeouts, and other options. Below is an example configuration:

{
    "opc_servers": {
    "PLC1": {
      "server_url": "opc.tcp://192.168.0.1:4840",
      "timeout": 10,
      "keepalive": true
    }
    }
    }

Usage

To use the OPC Client, you must first instantiate it with the name of the server configured in your JSON file:

client = OPCClient('PLC1')
    value = client.read(['DataBlockName', 'Struct1', 'Struct2', 'Tag1'])
    client.write(123, ua.VariantType.Int32, ['DataBlockName', 'Struct1', 'Struct2', 'Tag1'])

Utility Functions

Variant Type Lookup

Use the get_variant_type function to determine the appropriate data type for OPC UA operations:

variant_type = get_variant_type('Int32')

Additional Tools

TicToc

A utility class for measuring time intervals, useful for profiling and debugging.

tic()
    # Run some operations
    toc()

Further Reading

For more information on the OPC UA protocol and the Python OPC UA library, visit the OPC Foundation website.