apache / plc4x

PLC4X The Industrial IoT adapter

Home Page:https://plc4x.apache.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Starting subscription error using 0.10.0 package ,S7_1500

cptjackwu opened this issue · comments

What happened?

String connectionString = "s7://xx.xx.xx.xx";

    PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString);

PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
builder.addChangeOfStateField("value-1", "%DB5000:6:STRING(2)");
builder.addChangeOfStateField("value-2", "%DB5000:5.1:BOOL");
builder.addChangeOfStateField("value-3", "%DB5000:0:WORD");
PlcSubscriptionRequest subscriptionRequest = builder.build();
PlcSubscriptionResponse response = subscriptionRequest.execute().get();
for (String subscriptionName : response.getFieldNames()) {
final PlcSubscriptionHandle subscriptionHandle = response.getSubscriptionHandle(subscriptionName);
subscriptionHandle.register(plcSubscriptionEvent -> {
for (String fieldName : plcSubscriptionEvent.getFieldNames()) {
System.out.println(plcSubscriptionEvent.getPlcValue(fieldName));
}
});
}

org.apache.plc4x plc4j-api 0.10.0
    <dependency>
        <groupId>org.apache.plc4x</groupId>
        <artifactId>plc4j-driver-s7</artifactId>
        <version>0.10.0</version>
        <scope>runtime</scope>
    </dependency>

Exception in thread "main" java.lang.ClassCastException: org.apache.plc4x.java.s7.readwrite.field.S7StringField cannot be cast to org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField
at org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic.subscribe(S7ProtocolLogic.java:341)
at org.apache.plc4x.java.spi.optimizer.BaseOptimizer.lambda$optimizedSubscribe$4(BaseOptimizer.java:120)
at org.apache.plc4x.java.spi.optimizer.BaseOptimizer.send(BaseOptimizer.java:138)
at org.apache.plc4x.java.spi.optimizer.BaseOptimizer.optimizedSubscribe(BaseOptimizer.java:120)
at org.apache.plc4x.java.spi.connection.AbstractPlcConnection.subscribe(AbstractPlcConnection.java:183)
at org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionRequest.execute(DefaultPlcSubscriptionRequest.java:66)
at PlcClient.main(PlcClient.java:127)

Version

v0.10.0

Programming Languages

  • plc4j
  • plc4go
  • plc4c
  • plc4net

Protocols

  • AB-Ethernet
  • ADS /AMS
  • BACnet/IP
  • CANopen
  • DeltaV
  • DF1
  • EtherNet/IP
  • Firmata
  • KNXnet/IP
  • Modbus
  • OPC-UA
  • S7

Is it because my address was written incorrectly? But when I read it, writing like that can read the values in the PLC。
example: %DB5000:5.1:BOOL

No ... not all protocols support subscriptions ... if you had asked the connection if subscriptions are supported, it would have said "no". In the future we are planning on simulating subsciptions by polling in the background, but this feature still needs to be implemented. So I would suggest, you use the Read-API instead.

Thank you. I hope you can monitor changes in PLC addresses in the near future

When this issue is resolved, @cptjackwu, can you close it?

commented

got same problem when use plc4j 0.10.0 to subscript to s7 PLC.
the fellow check got true.
plcConnection.getMetadata().canSubscribe()

but when run the demo :

      final PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
      String[] address = new String[2];
      List<String[]> taglist = new ArrayList<>();
      address[0] = "DB145.DBD10";
      address[1] = "%DB145.DBD10:REAL";
      taglist.add(address);
      for (int i = 0; i < taglist.size(); i++) {
          builder.addChangeOfStateField(taglist.get(0)[0], taglist.get(0)[1]);
      }
      PlcSubscriptionRequest subscriptionRequest = builder.build();
      final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get();

still got error : org.apache.plc4x.java.s7.readwrite.field.S7Field cannot be cast to org.apache.plc4x.java.s7.readwrite.field.S7SubscriptionField

As we mentioned in previous responses ... a S7-1500 doesn't support subscriptions ... ony a very small subset of the S7 devices actually support it.

Well this is actually not something that PLC4X can do ... the devices simply don't support it ... we could simulate it, but that comes at operational costs of higher load on the PLC. The S7 400 models that support it, do this via a separate mechanism, that is highly efficient and doesn't put too much extra load on the PLC.