当前位置: 欣欣网 > 码农

在工厂项目中,我是用这个读取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 的官方网站或文档。