Skip to content

Instantly share code, notes, and snippets.

@splatch
Created April 19, 2021 17:30
Show Gist options
  • Save splatch/f1554f0775b2b20df072f51a98ad280b to your computer and use it in GitHub Desktop.
Save splatch/f1554f0775b2b20df072f51a98ad280b to your computer and use it in GitHub Desktop.
Profinet DCP interaction based on experimental protocol implementation for Apache PLC4X.
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketChannel;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
// Very simple example showing how to request profinet-dcp identification.
// Answers in line 23 are returned as PlcStruct (which is a map), for example:
// {"station.type": "SIMATIC-PC","station.name": "desktop-om18vq9","device.id": 514,"device.role": 0,"ip.address": "10.10.10.139","ip.mask": "255.255.255.0","ip.gateway": "10.10.10.1"}
public class Main {
public static void main(String[] args) throws Exception {
PlcConnection connection = new PlcDriverManager().getConnection("profinet-dcp:raw://eno0?sender=<my-ma-address>");
connection.connect();
// nodes which do answer will trigger subscription event with "nodeDiscovery" field containing device description
connection.subscriptionRequestBuilder().addEventField("nodeDiscovery", "IDENTIFY_RESPONSE:").build().execute().whenComplete((r, e) -> {
r.getSubscriptionHandle("nodeDiscovery").register(new Consumer<PlcSubscriptionEvent>() {
@Override
public void accept(PlcSubscriptionEvent plcSubscriptionEvent) {
System.out.println("Received subscription event " + plcSubscriptionEvent.getFieldNames() + "\n " +
"status: " + plcSubscriptionEvent.getResponseCode("nodeDiscovery") + "\n " +
"details: " + plcSubscriptionEvent.getPlcValue("nodeDiscovery") + "\n " +
"time: " + plcSubscriptionEvent.getTimestamp()
);
}
});
});
// send broadcast with response delay set to 100 ('ms' I guess).
connection.writeRequestBuilder().addItem("broadcast", "IDENTIFY_BROADCAST:/100")
.build().execute().whenComplete((r, e) -> {
if (e != null) {
e.printStackTrace();
return;
}
System.out.println("Request status " + r.getResponseCode("broadcast"));
});
Thread.sleep(TimeUnit.MINUTES.toMillis(1)); // wait 1 minute and quit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment