1 year ago
#162889
digga106
PLC4X: S7 Connection NullPointerException
I want to read out a single DB Value from a S7-300 with PLC4X in Java with this code:
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());
}
}
}
But my code runs in some issues. First of all i have set the options for Remote Rack ,Remote Slot and Controller Type , but when i run the code, stants following in the message in the console:
[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'}
The second issue resp. error comes, when the program try to read the DB Value:
[main] INFO org.test.plx4j.example.S7Com - ERROR: java.lang.NullPointerException
The content of the pom.xml looks like this:
<?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>
I have changed the version from plc4j to some other versions, but the issue remains the same.
At last the complete output of the console:
[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!
I use Netbeans 12.6 on Windows 10. The java version is 11.0.12.
java
apache-plc4x
plc4x
0 Answers
Your Answer