Central API Module providing core interfaces and abstractions for unified access to industrial programmable logic controllers (PLCs)
npx @tessl/cli install tessl/maven-org-apache-plc4x--plc4j-api@0.13.0Apache PLC4X Java API provides a unified, protocol-agnostic interface for communicating with industrial programmable logic controllers (PLCs). It abstracts the complexity of industrial communication protocols, enabling developers to build applications that can communicate with various PLC systems using a consistent programming interface.
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-api</artifactId>
<version>0.13.1</version>
</dependency>import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnectionManager;
import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.api.types.PlcResponseCode;import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
// Create connection manager
PlcDriverManager driverManager = new DefaultPlcDriverManager();
PlcConnectionManager connectionManager = driverManager.getConnectionManager();
// Connect to PLC
try (PlcConnection connection = connectionManager.getConnection("modbus-tcp://192.168.1.100:502")) {
connection.connect();
// Create and execute read request
PlcReadRequest readRequest = connection.readRequestBuilder()
.addTagAddress("sensor1", "holding-register:1")
.addTagAddress("sensor2", "holding-register:2")
.build();
PlcReadResponse response = readRequest.execute().get();
// Extract values
if (response.getResponseCode("sensor1") == PlcResponseCode.OK) {
int value1 = response.getInteger("sensor1");
int value2 = response.getInteger("sensor2");
System.out.println("Sensor1: " + value1 + ", Sensor2: " + value2);
}
}Apache PLC4X Java API is built around several key components:
PlcDriverManager provides discovery and factory methods for protocol-specific driversPlcConnection abstracts PLC connections with lifecycle management and capability detectionPlcValue provides universal value abstraction with automatic type conversionCompletableFuture for non-blocking executionCore connection lifecycle management, driver discovery, and connection establishment with authentication support.
public interface PlcDriverManager {
static PlcDriverManager getDefault();
Set<String> getProtocolCodes();
PlcDriver getDriver(String protocolCode) throws PlcConnectionException;
PlcDriver getDriverForUrl(String url) throws PlcConnectionException;
PlcConnectionManager getConnectionManager();
}
public interface PlcConnectionManager {
PlcConnection getConnection(String url) throws PlcConnectionException;
PlcConnection getConnection(String url, PlcAuthentication authentication) throws PlcConnectionException;
}
public interface PlcConnection extends AutoCloseable {
void connect() throws PlcConnectionException;
boolean isConnected();
void close() throws Exception;
PlcConnectionMetadata getMetadata();
CompletableFuture<? extends PlcPingResponse> ping();
}Type-safe data reading from PLC tags with comprehensive value conversion and validation.
public interface PlcReadRequest extends PlcTagRequest {
CompletableFuture<? extends PlcReadResponse> execute();
interface Builder extends PlcRequestBuilder {
Builder addTagAddress(String name, String tagAddress);
Builder addTag(String name, PlcTag tag);
PlcReadRequest build();
}
}
public interface PlcReadResponse extends PlcTagResponse {
PlcValue getPlcValue(String name);
int getInteger(String name);
boolean getBoolean(String name);
String getString(String name);
// Additional type-specific getters...
}Type-safe data writing to PLC tags with value validation and conversion.
public interface PlcWriteRequest extends PlcTagRequest {
CompletableFuture<? extends PlcWriteResponse> execute();
interface Builder extends PlcRequestBuilder {
Builder addTagAddress(String name, String tagAddress, Object... values);
Builder addTag(String name, PlcTag tag, Object... values);
PlcWriteRequest build();
}
}Event-driven communication with multiple subscription types for real-time data monitoring.
public interface PlcSubscriptionRequest extends PlcSubscriptionTagRequest {
CompletableFuture<? extends PlcSubscriptionResponse> execute();
interface Builder extends PlcRequestBuilder {
Builder setConsumer(Consumer<PlcSubscriptionEvent> consumer);
Builder addCyclicTagAddress(String name, String tagAddress, Duration duration);
Builder addChangeOfStateTagAddress(String name, String tagAddress);
Builder addEventTagAddress(String name, String tagAddress);
PlcSubscriptionRequest build();
}
}
public interface PlcSubscriptionEvent extends PlcReadResponse {
Instant getTimestamp();
}Universal value abstraction providing type-safe access to PLC data with automatic conversion.
public interface PlcValue {
PlcValueType getPlcValueType();
Object getObject();
boolean isBoolean();
boolean getBoolean();
int getInteger();
String getString();
// Comprehensive type system...
}PLC namespace exploration and tag discovery capabilities.
public interface PlcBrowseRequest extends PlcRequest {
CompletableFuture<? extends PlcBrowseResponse> execute();
interface Builder extends PlcRequestBuilder {
Builder addQuery(String name, String query);
PlcBrowseRequest build();
}
}Comprehensive exception hierarchy for robust error handling in industrial communication.
public class PlcException extends Exception { }
public class PlcConnectionException extends PlcException { }
public class PlcIoException extends PlcException { }
public class PlcProtocolException extends PlcException { }
// Additional specific exceptions...public enum PlcResponseCode {
OK, NOT_FOUND, ACCESS_DENIED, INVALID_ADDRESS, INVALID_DATATYPE,
INVALID_DATA, INTERNAL_ERROR, REMOTE_BUSY, REMOTE_ERROR,
UNSUPPORTED, RESPONSE_PENDING;
short getValue();
static PlcResponseCode enumForValue(short value);
}
public enum PlcValueType {
NULL, BOOL, BYTE, WORD, DWORD, LWORD,
USINT, UINT, UDINT, ULINT, SINT, INT, DINT, LINT,
REAL, LREAL, CHAR, WCHAR, STRING, WSTRING,
TIME, LTIME, DATE, LDATE, TIME_OF_DAY, LTIME_OF_DAY,
DATE_AND_TIME, DATE_AND_LTIME, LDATE_AND_TIME,
Struct, List, RAW_BYTE_ARRAY;
short getValue();
Class<?> getDefaultJavaType();
}
public enum PlcSubscriptionType {
CYCLIC, CHANGE_OF_STATE, EVENT
}public interface PlcAuthentication { }
public class PlcUsernamePasswordAuthentication implements PlcAuthentication {
public PlcUsernamePasswordAuthentication(String username, String password);
public String getUsername();
public String getPassword();
}public interface PlcConnectionMetadata {
boolean isReadSupported();
boolean isWriteSupported();
boolean isSubscribeSupported();
boolean isBrowseSupported();
}
public interface PlcDriverMetadata {
Optional<String> getDefaultTransportCode();
List<String> getSupportedTransportCodes();
boolean isDiscoverySupported();
}