Apache PLC4X Documentation

repository·develop·Indexed 23 days ago

https://github.com/apache/plc4x

A framework for communicating with Programmable Logic Controllers (PLCs). Includes multi-language support with plc4go and plc4c, a Profinet driver featuring GSD profile discovery and auto-configuration, and Java tools such as the Event Pump for event-driven tag data fetching and a Connection Cache for managing PLC connection resources.

Tokens
87.9K
Snippets
183
Records
424
Agent score
78%

What's inside Apache PLC4X

  1. What is Apache PLC4X

    develop
    Apache PLC4X is a set of libraries designed to provide a uniform way to communicate with industrial-grade Programmable Logic Controllers (PLCs). Instead of implementing full, certifiable protocol stacks for every device, PLC4X focuses on providing a consistent core set of operations across all supported industrial communication protocols. This abstraction allows developers to write software that is largely independent of the specific hardware being used.
  2. Use the Generic CAN driver

    develop

    The Generic CAN driver is a general-purpose driver for implementing basic CAN bus listening or writing scenarios. It allows you to model incoming and outgoing communication using the standard Apache PLC4X API.

    Data written to the CAN bus is constructed from fields submitted via a write request builder. Incoming data is transformed based on the fields you have subscribed to.

  3. What is the PLC4X Server and when to use it

    develop

    The PLC4X Server is a relay server that implements the PLC4X proxy protocol. It acts as an intermediary between a client (using the plc4x driver) and a PLC.

    Use Case: Use the server when your clients are on a different network than the PLCs (e.g., separated by a firewall or on an isolated OT network). The server sits on the reachable network, opens the native connection to the PLC (using protocols like s7, modbus, ads, opcua, etc.), and forwards reads/writes from the client.

    Architecture:

    • Client $\rightarrow$ PLC4X Server (via plc4x proxy protocol with TLS + auth)
    • PLC4X Server $\rightarrow$ PLC (via native protocol like s7 or modbus)
  4. What is Object PLC Mapping (OPM)

    develop
    Object PLC Mapping (OPM) is a module inspired by the Java Persistence API (JPA) designed to simplify PLC communication. It allows developers to interact with PLC devices by using Plain Old Java Objects (POJOs) instead of dealing with low-level protocols and domain-specific communication logic. By decorating classes with specific annotations, you can map object properties directly to PLC tags.
  5. How the Mock Driver works

    develop

    The Mock Driver uses a connection string syntax of mock:{name-of-the-connection}. This allows you to define and use multiple independent mock devices simultaneously.

    Instead of communicating with hardware, the driver forwards all requests to a MockDevice. You provide an implementation of the MockDevice interface to control responses (e.g., returning specific values for reads) and monitor incoming requests (e.g., verifying writes) during testing.

  6. Understand the ADS (Automation Device Specification) protocol

    develop

    ADS is a device-independent and fieldbus-independent interface used for communication between Beckhoff automation devices running TwinCAT and other devices. It allows for interaction between two ADS devices by defining executable operations, necessary parameters, and return values.

    Key components include:

    • AMS (Automation Message Specification): Specifies the exchange of ADS data.
    • AmsNetId: A major component of the AMS specification used to explicitly address ADS devices in the AMS/ADS package for both source and target devices.
  7. Understand SocketCAN transport characteristics

    develop

    SocketCAN is a Linux-specific method for accessing the CAN bus. When using this transport, Apache PLC4X delegates low-level bus access, arbitration, and coordination to the SocketCAN layer and the underlying OS drivers.

    Key technical details:

    • Frame Representation: You must program your application against the SocketCAN frame representation rather than raw CAN interfaces.
    • Frame Length: SocketCAN frames have a fixed length (e.g., CAN 2.0A always takes 16 bytes).
    • Identifiers: Standard CAN frames use 11-bit identifiers, whereas SocketCAN uses 29-bit identifiers and appends flags to the remaining 3 bits.
    • Compatibility: This transport has been tested with vcan and gs_usb drivers.
  8. Identify S7 Diagnostic Events

    develop

    In the S7 driver, system-level events (type SYS) can be identified using the S7DiagnosticEventId enum. This enum allows you to determine which internal event of the PLC(AS) generated the event. By interpreting the INFO1 and INFO2 fields associated with the event, you can determine the root cause.

    Note: The S7DiagnosticEventId enum is subject to updates as new CPUs or firmware versions are released.

  9. Understand the MSpec format

    develop

    MSpec (Message Specification) is a text-based format used to define protocol structures for code generation in PLC4X. It allows developers to describe how messages are structured, parsed, and serialized.

    At the root level, a specification consists of blocks like type, discriminatedType, dataIo, enum, and constants.

    • type: An object whose structure is independent of the input (e.g., a fixed packet structure).
    • discriminatedType: An object whose structure is influenced by the input (e.g., a message where a header field determines the rest of the payload). It must contain exactly one typeSwitch element.
    • constants: A block used to define protocol-wide constants.
    [type TPKTPacket
        [const    uint 8                 protocolId 0x03]
        [reserved uint 8                 '0x00']
        [implicit uint 16                len       'payload.lengthInBytes + 4']
        [simple   COTPPacket('len - 4') payload]
    ]
  10. Address individual tags

    develop

    To access specific data points (tags) on a PLC, you use a tag address string. The syntax is generally type:address.

    Some protocols support additional tag attributes specified as key-value pairs in braces following the primary address. For example: coil:1{unit-id: 10}

    Because address formats are highly dependent on the specific protocol, you must consult the relevant protocol documentation for the correct syntax.

    type:address
    // Example with attributes:
    coil:1{unit-id: 10}
  11. Subscribe to S7 System (SYS) and User (USR) Events

    develop

    The S7 driver supports asynchronous subscriptions to system and user-defined events.

    Event Types

    • SYS (System Events): Events affecting the controller or peripheral equipment (e.g., CP/IM state changes). These are generated by the OS.
    • USR (User Events): Events generated by user applications using alarm blocks (e.g., ALARM_S, NOTIFY).

    Common Fields for SYS and USR

    Both event types return a HashMap containing:

    • TYPE: Fixed value (STRING).
    • TIMESTAMP: Time assigned when receiving the event.
    • EVENT_ID: OS generated event ID (short).
    • PRIORITY_CLASS: Value of "method" from S7Parameter.
    • OB_NUMBER: Value of "function" from S7Parameter.
    • DAT_ID: Status value (short).
    • INFO1: System information (WORD).
    • INFO2: System information (DWORD).

    Note on Timestamps: For ALARM type messages, timestamps are generated in the PLC. Ensure date/time synchronization between the PLC and your application computer.

  12. Manage lifecycle of PLC4C structures

    develop

    PLC4C uses specific naming suffixes to manage the lifecycle (allocation and deallocation) of structures.

    • Creation: All structures must be instantiated using a function ending in the _create suffix.
    • Destruction: All structures must be freed, cleared, or deleted using a function ending in the _destroy suffix.