use of org.apache.plc4x.java.api.messages.PlcReadRequest in project plc4x by apache.
the class EndToEndTest method helloDf1.
@Test
@Disabled("Seems to cause problems on Windows if no COM4 is available")
public void helloDf1() {
try (PlcConnection plcConnection = new PlcDriverManager().getConnection("df1:serial:///COM4")) {
PlcReadRequest request = plcConnection.readRequestBuilder().addItem("hurz", "5:INTEGER").build();
PlcReadResponse response = request.execute().get(100, TimeUnit.SECONDS);
// TODO: get the actual read bytes from the response
System.out.println(response);
System.out.println("Response code was " + response.getResponseCode("erster"));
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.plc4x.java.api.messages.PlcReadRequest in project incubator-streampipes by apache.
the class Plc4xModbusAdapter method pullData.
/**
* is called iteratively according to the polling interval defined in getPollInterval.
*/
@Override
protected void pullData() {
// create PLC read request
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
for (Map<String, String> node : this.nodes) {
switch(node.get(PLC_NODE_TYPE)) {
case "Coil":
builder.addItem(node.get(PLC_NODE_RUNTIME_NAME), "coil:" + String.valueOf(node.get(PLC_NODE_ADDRESS)));
break;
case "HoldingRegister":
builder.addItem(node.get(PLC_NODE_RUNTIME_NAME), "holding-register:" + String.valueOf(node.get(PLC_NODE_ADDRESS)));
break;
case "DiscreteInput":
builder.addItem(node.get(PLC_NODE_RUNTIME_NAME), "discrete-input:" + String.valueOf(node.get(PLC_NODE_ADDRESS)));
break;
case "InputRegister":
builder.addItem(node.get(PLC_NODE_RUNTIME_NAME), "input-register:" + String.valueOf(node.get(PLC_NODE_ADDRESS)));
break;
}
}
PlcReadRequest readRequest = builder.build();
// Execute the request
PlcReadResponse response = null;
try {
response = readRequest.execute().get();
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (ExecutionException ee) {
ee.printStackTrace();
}
// Create an event containing the value of the PLC
Map<String, Object> event = new HashMap<>();
for (Map<String, String> node : this.nodes) {
if (response.getResponseCode(node.get(PLC_NODE_RUNTIME_NAME)) == PlcResponseCode.OK) {
switch(node.get(PLC_NODE_TYPE)) {
case "Coil":
event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getBoolean(node.get(PLC_NODE_RUNTIME_NAME)));
break;
case "DiscreteInput":
event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getBoolean(node.get(PLC_NODE_RUNTIME_NAME)));
break;
case "InputRegister":
event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getInteger(node.get(PLC_NODE_RUNTIME_NAME)));
break;
case "HoldingRegister":
event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getInteger(node.get(PLC_NODE_RUNTIME_NAME)));
break;
}
} else {
logger.error("Error[" + node.get(PLC_NODE_RUNTIME_NAME) + "]: " + response.getResponseCode(node.get(PLC_NODE_RUNTIME_NAME)));
}
}
// publish the final event
adapterPipeline.process(event);
}
use of org.apache.plc4x.java.api.messages.PlcReadRequest in project incubator-streampipes by apache.
the class Plc4xS7Adapter method pullData.
/**
* pullData is called iteratively according to the polling interval defined in getPollInterval.
*/
@Override
protected void pullData() {
// Create PLC read request
try (PlcConnection plcConnection = this.driverManager.getConnection("s7://" + this.ip)) {
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
for (Map<String, String> node : this.nodes) {
builder.addItem(node.get(PLC_NODE_NAME), node.get(PLC_NODE_NAME) + ":" + node.get(PLC_NODE_TYPE).toUpperCase().replaceAll(" ", "_"));
}
PlcReadRequest readRequest = builder.build();
// Execute the request
CompletableFuture<? extends PlcReadResponse> asyncResponse = readRequest.execute();
asyncResponse.whenComplete((response, throwable) -> {
// Create an event containing the value of the PLC
if (throwable != null) {
throwable.printStackTrace();
this.LOG.error(throwable.getMessage());
} else {
Map<String, Object> event = new HashMap<>();
for (Map<String, String> node : this.nodes) {
if (response.getResponseCode(node.get(PLC_NODE_NAME)) == PlcResponseCode.OK) {
event.put(node.get(PLC_NODE_RUNTIME_NAME), response.getObject(node.get(PLC_NODE_NAME)));
} else {
this.LOG.error("Error[" + node.get(PLC_NODE_NAME) + "]: " + response.getResponseCode(node.get(PLC_NODE_NAME)).name());
}
}
// publish the final event
adapterPipeline.process(event);
}
});
} catch (InterruptedException | ExecutionException e) {
this.LOG.error(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
this.LOG.error("Could not establish connection to S7 with ip " + this.ip, e);
e.printStackTrace();
}
}
use of org.apache.plc4x.java.api.messages.PlcReadRequest in project plc4x by apache.
the class OpcuaPlcDriverTest method multipleThreads.
/*
Test added to test the syncronized Trnasactionhandler.
The test originally failed one out of every 5 or so.
*/
public void multipleThreads() {
class ReadWorker extends Thread {
private PlcConnection connection;
public ReadWorker(PlcConnection opcuaConnection) {
this.connection = opcuaConnection;
}
@Override
public void run() {
try {
PlcReadRequest.Builder read_builder = connection.readRequestBuilder();
read_builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE);
PlcReadRequest read_request = read_builder.build();
for (int i = 0; i < 100; i++) {
PlcReadResponse read_response = read_request.execute().get();
assertThat(read_response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
}
} catch (ExecutionException executionException) {
executionException.printStackTrace();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
class WriteWorker extends Thread {
private PlcConnection connection;
public WriteWorker(PlcConnection opcuaConnection) {
this.connection = opcuaConnection;
}
@Override
public void run() {
try {
PlcWriteRequest.Builder write_builder = connection.writeRequestBuilder();
write_builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE, true);
PlcWriteRequest write_request = write_builder.build();
for (int i = 0; i < 100; i++) {
PlcWriteResponse write_response = write_request.execute().get();
assertThat(write_response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
}
} catch (ExecutionException executionException) {
executionException.printStackTrace();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
try {
PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress);
Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
assertThat(opcuaConnection).is(is_connected);
ReadWorker read_worker = new ReadWorker(opcuaConnection);
WriteWorker write_worker = new WriteWorker(opcuaConnection);
read_worker.start();
write_worker.start();
read_worker.join();
write_worker.join();
opcuaConnection.close();
assert !opcuaConnection.isConnected();
} catch (Exception e) {
fail("Exception during readVariables Test EXCEPTION: " + e.getMessage());
}
}
use of org.apache.plc4x.java.api.messages.PlcReadRequest in project plc4x by apache.
the class OpcuaPlcDriverTest method readVariables.
@Test
public void readVariables() {
try {
PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress);
Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
assertThat(opcuaConnection).is(is_connected);
PlcReadRequest.Builder builder = opcuaConnection.readRequestBuilder();
builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE);
builder.addItem("Byte", BYTE_IDENTIFIER_READ_WRITE);
builder.addItem("Double", DOUBLE_IDENTIFIER_READ_WRITE);
builder.addItem("Float", FLOAT_IDENTIFIER_READ_WRITE);
builder.addItem("Int16", INT16_IDENTIFIER_READ_WRITE);
builder.addItem("Int32", INT32_IDENTIFIER_READ_WRITE);
builder.addItem("Int64", INT64_IDENTIFIER_READ_WRITE);
builder.addItem("Integer", INTEGER_IDENTIFIER_READ_WRITE);
builder.addItem("SByte", SBYTE_IDENTIFIER_READ_WRITE);
builder.addItem("String", STRING_IDENTIFIER_READ_WRITE);
builder.addItem("UInt16", UINT16_IDENTIFIER_READ_WRITE);
builder.addItem("UInt32", UINT32_IDENTIFIER_READ_WRITE);
builder.addItem("UInt64", UINT64_IDENTIFIER_READ_WRITE);
builder.addItem("UInteger", UINTEGER_IDENTIFIER_READ_WRITE);
builder.addItem("BoolArray", BOOL_ARRAY_IDENTIFIER);
// builder.addItem("ByteStringArray", BYTE_STRING_ARRAY_IDENTIFIER);
builder.addItem("ByteArray", BYTE_ARRAY_IDENTIFIER);
builder.addItem("DoubleArray", DOUBLE_ARRAY_IDENTIFIER);
builder.addItem("FloatArray", FLOAT_ARRAY_IDENTIFIER);
builder.addItem("Int16Array", INT16_ARRAY_IDENTIFIER);
builder.addItem("Int32Array", INT32_ARRAY_IDENTIFIER);
builder.addItem("Int64Array", INT64_ARRAY_IDENTIFIER);
builder.addItem("SByteArray", SBYTE_ARRAY_IDENTIFIER);
builder.addItem("StringArray", STRING_ARRAY_IDENTIFIER);
builder.addItem("UInt16Array", UINT16_ARRAY_IDENTIFIER);
builder.addItem("UInt32Array", UINT32_ARRAY_IDENTIFIER);
builder.addItem("UInt64Array", UINT64_ARRAY_IDENTIFIER);
builder.addItem("DoesNotExists", DOES_NOT_EXIST_IDENTIFIER_READ_WRITE);
PlcReadRequest request = builder.build();
PlcReadResponse response = request.execute().get();
assertThat(response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Byte")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Double")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Float")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int16")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int32")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int64")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Integer")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("SByte")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("String")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt16")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt32")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt64")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInteger")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("BoolArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("ByteArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("DoubleArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("FloatArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int16Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int32Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("Int64Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("SByteArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("StringArray")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt16Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt32Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("UInt64Array")).isEqualTo(PlcResponseCode.OK);
assertThat(response.getResponseCode("DoesNotExists")).isEqualTo(PlcResponseCode.NOT_FOUND);
opcuaConnection.close();
assert !opcuaConnection.isConnected();
} catch (Exception e) {
fail("Exception during readVariables Test EXCEPTION: " + e.getMessage());
}
}
Aggregations