![]()
I removed the "getLister" method as I haven't seen any protocol or use-case where this actually made sense.
NOTE: I would suggest to not remove PlcReadRequestItem or PlcWriteResponseItem class entirely, even if we could simply make PlcRequestItem and PlcResponseItem non-abstract. For simplicity of the API I would opt for keeping it ... so we always have matching pairs of PlcXYZRequestItem and PlcXYZResponseItems.
Here comes some example code to demonstrate how a Programm using this API could look like.
public static void main(String[] args) throws Exception {
PlcDriverManager manager = new PlcDriverManager();
try (PlcConnection conn = manager.getConnection("s7://192.168.167.211/0/0")) {
Optional<PlcReader> optionalReader = conn.getReader();
if (!optionalReader.isPresent()) {
return;
}
PlcReader Reader = optionalReader.get();
PlcReadRequest request = new PlcReadRequest(
new PlcReadRequestItem("parameterX", conn.prepareQuery("%DB8.DBX3:INT")),
new PlcReadRequestItem("parameterY", conn.prepareQuery("%DB9.DBW4:DOUBLE")),
new PlcReadRequestItem("others", conn.prepareQuery("%DB2.DBW2:BYTE[10]")));
CompletableFuture<PlcReadResponse> future = reader.read(request);
PlcReadResponse response = future.get();
// Access an item by it's name.
System.out.println("Parameter X: " + response.getInteger("parameterX"));
// Iterate over all items
for(PlcReadResponseItem item: response.getItems()) {
// Conditionally handle an item by it's name.
if("parameterY".equals(item.getName()) {
// If using getInteger without an index, the index 0 is used.
System.out.println("Parameter y: " + item.getDouble());
}
else if("others".equals(item.getName()) {
System.out.println("Others:");
// Handle multi-value items.
for(int i = 0; i < item.getNumValues(); i++) {
// Intentionally using short instead of byte
// In this case it will return Byte[i] but convert that into a Short.
System.out.println(" - " + item.getShort(i));
}
}
}
}
} |