我想使用以下代码从带有 PLC4X 的 S7-300 中读取单个 DB 值:
import java.util.concurrent.TimeUnit;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class S7Com {
private static final Logger logger = LoggerFactory.getLogger(S7Com.class);
public static void main(String[] args) {
String connectionString = "s7://192.168.1.102?remote-rack:0&remote-slot:2&controller-type:S7_300";
try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString)) {
// Check if this connection support reading of data.
if (!plcConnection.getMetadata().canRead()) {
logger.error("This connection doesn't support reading.");
return;
}
// Create a new read request:
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
builder.addItem("value", "%DB100:16.0:REAL");
PlcReadRequest readRequest = builder.build();
//////////////////////////////////////////////////////////
// Read synchronously ...
// NOTICE: the ".get()" immediately lets this thread pause until
// the response is processed and available.
logger.info("Synchronous request ...");
PlcReadResponse syncResponse = readRequest.execute().get();
// Simply iterating over the field names returned in the response.
logger.info("Value[{}]: {}", "value", syncResponse.getObject("value"));
TimeUnit.MILLISECONDS.sleep(1000);
plcConnection.close();
System.exit(0);
} catch (Exception e) {
logger.info("ERROR: {}", e.getMessage());
}
}
}
但是我的代码在一些问题中运行。首先,我已经设置了 Remote Rack 、Remote Slot 和 Controller Type 的选项,但是当我运行代码时,控制台中的消息中会出现以下内容:
[main] INFO org.apache.plc4x.java.transport.tcp.TcpChannelFactory - Configuring Bootstrap with Configuration{local-rack=1, local-slot=1, remote-rack=0, remot-slot=0, pduSize=1024, maxAmqCaller=8, maxAmqCallee=8, controllerType='null'}
第二个问题分别。错误来了,当程序尝试读取 DB 值时:
[main] INFO org.test.plx4j.example.S7Com - ERROR: java.lang.NullPointerException
pom.xml 的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>plx4j-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-api</artifactId>
<version>0.9.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-s7</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.23</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
我已将版本从 plc4j 更改为其他一些版本,但问题仍然存在。
最后是控制台的完整输出:
[main] INFO org.apache.plc4x.java.PlcDriverManager - Instantiating new PLC Driver Manager with class loader jdk.internal.loader.ClassLoaders$AppClassLoader@2cdf8d8a
[main] INFO org.apache.plc4x.java.PlcDriverManager - Registering available drivers...
[main] INFO org.apache.plc4x.java.PlcDriverManager - Registering driver for Protocol s7 (Siemens S7 (Basic))
[main] INFO org.apache.plc4x.java.transport.tcp.TcpChannelFactory - Configuring Bootstrap with Configuration{local-rack=1, local-slot=1, remote-rack=0, remot-slot=0, pduSize=1024, maxAmqCaller=8, maxAmqCallee=8, controllerType='null'}
[nioEventLoopGroup-2-1] INFO org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic - S7 Driver running in ACTIVE mode.
[main] INFO org.test.plx4j.example.S7Com - Synchronous request ...
[main] INFO org.test.plx4j.example.S7Com - ERROR: java.lang.NullPointerException
ObjectProcessor Bye!
EventDispacher Bye!
我在 Windows 10 上使用 Netbeans 12.6。java 版本是 11.0.12。