當前位置: 妍妍網 > 碼農

在工廠計畫中,我是用這個讀取PLC數據的

2024-02-18碼農

Apache PLC4X 軟體介紹

Apache PLC4X旨在建立一組庫,以統一的方式與工業級可編程邏輯控制器(PLCs)進行通訊。目前,支持以下語言:

  • Java

  • Go

  • C (尚未可用)

  • Python (尚未可用)

  • C# (.Net) (已廢棄)

  • 功能特點

    PLC4X 設計目標之一是為開發人員提供簡化的 API,隱藏底層通訊細節,以便與各種 PLC 進行通訊。該框架提供了一致的編程介面,涵蓋以下通訊協定:

  • Modbus

  • S7 Communication (Siemens S7)

  • ADS (Automation Device Specification)

  • OPC UA (Unified Architecture)

  • EtherNet/IP

  • DF1 (Data Highway Plus)

  • KNX

  • ISO on TCP

  • 這種支持多個協定的設計使得 PLC4X 具有較高的靈活性和通用性。

    套用場景

  • 工業自動化:監控和控制工業生產裝置和系統。

  • Modbus PLC 通訊:與支持 Modbus 協定的 PLC 進行數據交換。

  • 範例程式碼

    這是一個範例程式碼的簡要概述:

  • 1.在 pom.xml 中添加 PLC4X 依賴。

  • <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.plc4x</groupId>
    <artifactId>plc4j-api</artifactId>
    <version>0.8.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.plc4x</groupId>
    <artifactId>plc4j-driver-modbus</artifactId>
    <version>0.8.0</version>
    </dependency>
    </dependencies>

  • 2.在 application.properties 或 application.yml 中配置 Modbus 連線資訊。

  • plc4x.connection-string=modbus:tcp://127.0.0.1:502

  • 3.編寫服務類用於與 Modbus PLC 進行通訊,包括讀取和寫入數據的方法。可以使用 PlcReadRequest 和 PlcWriteRequest 進行通訊。

  • import org.apache.plc4x.java.api.PlcConnection;
    import org.apache.plc4x.java.api.PlcReadRequest;
    import org.apache.plc4x.java.api.PlcReadResponse;
    import org.apache.plc4x.java.api.PlcSubscriptionRequest;
    import org.apache.plc4x.java.api.PlcSubscriptionResponse;
    import org.apache.plc4x.java.api.exceptions.PlcException;
    import org.apache.plc4x.java.api.exceptions.PlcTimeoutException;
    import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
    import org.apache.plc4x.java.api.messages.PlcSubscriptionRequestBuilder;
    import org.apache.plc4x.java.base.messages.PlcSubscriptionEventAdapter;
    import org.apache.plc4x.java.api.PlcWriteRequest;
    import org.apache.plc4x.java.api.PlcWriteResponse;
    @Service
    public class ModbusPlcService {
    private final PlcConnection plcConnection;
    public ModbusPlcService(PlcConnection plcConnection) {
    this.plcConnection = plcConnection;
    }
    public void readDataFromPlc(int slaveId) {
    try {
    plcConnection.setOption(PlcDriverOption.SLAVE_ID, slaveId);
    PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
    PlcReadRequest<Integer> readRequest = builder.addItem("readInt""HR1", Integer. class).build();
    PlcReadResponse<Integer> readResponse = readRequest.execute().get();
    if (readResponse.hasValues()) {
    Integer result = readResponse.getInteger("readInt");
    System.out.println("Read value from PLC: " + result);
    else {
    System.out.println("No values received from PLC");
    }
    } catch (PlcException e) {
    e.printStackTrace();
    }
    }
    public void writeDataToPlc(int slaveId) {
    try {
    plcConnection.setOption(PlcDriverOption.SLAVE_ID, slaveId);
    PlcWriteRequest.Builder builder = plcConnection.writeRequestBuilder();
    PlcWriteRequest<Integer> writeRequest = builder.addItem("writeInt""HR1", value).build();
    PlcWriteResponse writeResponse = writeRequest.execute().get();
    if (writeResponse.getResponseCode("writeInt") == PlcWriteResponse.ResponseCode.OK) {
    System.out.println("Write to PLC successful");
    else {
    System.out.println("Failed to write to PLC");
    }
    } catch (PlcException e) {
    e.printStackTrace();
    }
    }
    }







    優勢

  • 開源框架:PLC4X 是一個開源計畫,支持多種 PLC 品牌和通訊協定。

  • 整合簡便:透過 Spring Boot 整合,使得與 PLC 的通訊更加簡單。

  • 對於更詳細的資訊和最新支持的協定列表,請檢視 PLC4X 的官方網站或文件。