I am a bit new in PLC4X and programming in general. I want to read data from a snap7 python simulator I have set up locally (source: https://python-snap7.readthedocs.io/en/1.3/API/server.html) with a service written in Java 17 (Spring Boot 2.5.0). My goal is to read about 1000 tags per second or as fast as possible.
I am using PLC4X 0.12.0 and I have followed the instructions from the documentation and can read values normally up to a point. If I add more than 18 tagAddresses in the same connection the application hangs and I get the exception:
o.a.p.j.s.r.protocol.S7ProtocolLogic : org.apache.plc4x.java.api.exceptions.PlcProtocolException: The number of requested items doesn't match the number of returned items
I can solve it by breaking down my list of tag addresses in batches and then it works but I was wondering if I was doing something wrong or if this is intentional or maybe the simulator is at fault (I only changed some of the values I can read). Thank you
My code:
@Component
public class S7Poller {
private static final Logger LOG = LoggerFactory.getLogger(S7Poller.class);
private PlcConnection connection;
private static final LinkedHashMap<String, String> tagAddresses = new LinkedHashMap<>();
@PostConstruct
public void init() {
try {
connection = new DefaultPlcDriverManager()
.getConnection("s7://localhost:1102?local-rack=0&local-slot=1");
} catch (PlcConnectionException e) {
e.printStackTrace();
System.err.println("Error connecting to the PLC: " + e.getMessage());
}
}
@Scheduled(cron = "${cron}")
public void readPLC() {
if (connection != null) {
try {
PlcReadRequest.Builder readRequest = connection.readRequestBuilder();
readRequest.addTagAddress("boolean1", "%DB1.DBX0:0:BOOL");
readRequest.addTagAddress("boolean2", "%DB1.DBX0:1:BOOL");
readRequest.addTagAddress("boolean3", "%DB1.DBX0:2:BOOL");
readRequest.addTagAddress("boolean4", "%DB1.DBX0:3:BOOL");
readRequest.addTagAddress("boolean5", "%DB1.DBX0:4:BOOL");
readRequest.addTagAddress("boolean6", "%DB1.DBX0:5:BOOL");
readRequest.addTagAddress("boolean7", "%DB1.DBX0:6:BOOL");
readRequest.addTagAddress("boolean8", "%DB1.DBX0:7:BOOL");
readRequest.addTagAddress("sint1", "%DB1.DBB10:SINT");
readRequest.addTagAddress("sint2", "%DB1.DBB11:SINT");
readRequest.addTagAddress("sint3", "%DB1.DBB12:SINT");
readRequest.addTagAddress("sint4", "%DB1.DBB13:SINT");
readRequest.addTagAddress("usint1", "%DB1.DBB20:USINT");
readRequest.addTagAddress("usint2", "%DB1.DBB21:USINT");
readRequest.addTagAddress("int1", "%DB1.DBW30:INT");
readRequest.addTagAddress("int2", "%DB1.DBW32:INT");
readRequest.addTagAddress("int3", "%DB1.DBW34:INT");
readRequest.addTagAddress("int4", "%DB1.DBW36:INT");
readRequest.addTagAddress("int5", "%DB1.DBW38:INT");
readRequest.addTagAddress("real1", "%DB1.DBD60:REAL");
readRequest.addTagAddress("real2", "%DB1.DBD76:REAL");
readRequest.addTagAddress("real3", "%DB1.DBD92:REAL");
readRequest.addTagAddress("string1", "%DB1.DBX100:STRING");
PlcReadRequest rr = readRequest.build();
PlcReadResponse response = rr.execute().get();
logResponses(response);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
private void logResponses(PlcReadResponse response) {
LOG.info("boolean1: " + response.getBoolean("boolean1"));
LOG.info("boolean2: " + response.getBoolean("boolean2"));
LOG.info("boolean3: " + response.getBoolean("boolean3"));
LOG.info("boolean4: " + response.getBoolean("boolean4"));
LOG.info("boolean5: " + response.getBoolean("boolean5"));
LOG.info("boolean6: " + response.getBoolean("boolean6"));
LOG.info("boolean7: " + response.getBoolean("boolean7"));
LOG.info("boolean8: " + response.getBoolean("boolean8"));
LOG.info("sint1: " + response.getInteger("sint1"));
LOG.info("sint2: " + response.getInteger("sint2"));
LOG.info("sint3: " + response.getInteger("sint3"));
LOG.info("sint4: " + response.getInteger("sint4"));
LOG.info("usint1: " + response.getInteger("usint1"));
LOG.info("usint2: " + response.getInteger("usint2"));
LOG.info("int1: " + response.getInteger("int1"));
LOG.info("int2: " + response.getInteger("int2"));
LOG.info("int3: " + response.getInteger("int3"));
LOG.info("int4: " + response.getInteger("int4"));
LOG.info("int5: " + response.getInteger("int5"));
LOG.info("real1: " + response.getFloat("real1"));
LOG.info("real2: " + response.getFloat("real2"));
LOG.info("real3: " + response.getFloat("real3"));
LOG.info("string1: " + response.getString("string1"));
}
deleteJavascript is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.