Apache PLC4X Documentation
repository·develop·Indexed 23 days ago
https://github.com/apache/plc4xA 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.
What's inside Apache PLC4X
- 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.
Use the Generic CAN driver
developThe 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
writerequest builder. Incoming data is transformed based on the fields you have subscribed to.What is the PLC4X Server and when to use it
developThe
PLC4X Serveris a relay server that implements the PLC4X proxy protocol. It acts as an intermediary between a client (using theplc4xdriver) 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 protocolwith TLS + auth) - PLC4X Server $\rightarrow$ PLC (via native protocol like
s7ormodbus)
- Client $\rightarrow$ PLC4X Server (via
What is Object PLC Mapping (OPM)
developObject 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.How the Mock Driver works
developThe 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 theMockDeviceinterface to control responses (e.g., returning specific values for reads) and monitor incoming requests (e.g., verifying writes) during testing.Understand the ADS (Automation Device Specification) protocol
developADS 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.
Understand SocketCAN transport characteristics
developSocketCAN 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
vcanandgs_usbdrivers.
Identify S7 Diagnostic Events
developIn the S7 driver, system-level events (type
SYS) can be identified using theS7DiagnosticEventIdenum. This enum allows you to determine which internal event of the PLC(AS) generated the event. By interpreting theINFO1andINFO2fields associated with the event, you can determine the root cause.Note: The
S7DiagnosticEventIdenum is subject to updates as new CPUs or firmware versions are released.Understand the MSpec format
developMSpec (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, andconstants.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 onetypeSwitchelement.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] ]Address individual tags
developTo 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}Subscribe to S7 System (SYS) and User (USR) Events
developThe 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
HashMapcontaining:TYPE: Fixed value (STRING).TIMESTAMP: Time assigned when receiving the event.EVENT_ID: OS generated event ID (short).PRIORITY_CLASS: Value of "method" fromS7Parameter.OB_NUMBER: Value of "function" fromS7Parameter.DAT_ID: Status value (short).INFO1: System information (WORD).INFO2: System information (DWORD).
Note on Timestamps: For
ALARMtype messages, timestamps are generated in the PLC. Ensure date/time synchronization between the PLC and your application computer.Manage lifecycle of PLC4C structures
developPLC4C 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
_createsuffix. - Destruction: All structures must be freed, cleared, or deleted using a function ending in the
_destroysuffix.
- Creation: All structures must be instantiated using a function ending in the