plc4x-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wolfgang Huse <wolfgang.h...@nutanix.com>
Subject Re: Java Example for Subscription
Date Tue, 05 May 2020 06:52:24 GMT
Sorry... Still not getting this thing running...

I don’t get this subscription event raised ☹
Prosys OPC Simulator log shows that there is a new subscription (cyclic) and I also see regular
Read Requests but nothing happens in my code ?

public class HelloPlc4x {

    private static final Logger logger = LoggerFactory.getLogger(HelloPlc4x.class);
    public static void main(String[] args) throws Exception {
        PlcConnection plcConnection = new PlcDriverManager().getConnection("opcua:tcp://192.168.178.120:53530/opcua/SimulationServer");
        PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
        builder.addCyclicField("value-1", "ns=3;s=Counter", Duration.ofMillis(1000));
        PlcSubscriptionRequest subscriptionRequest = builder.build();
        PlcSubscriptionResponse subResp = subscriptionRequest.execute().get();
        Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your
Information"); 
        PlcConsumerRegistration registration = subResp.getSubscriptionHandle("value-1").register(consumer);
       
        while (true) {
            TimeUnit.MILLISECONDS.sleep(50);
        }
    }
}

Mit freundlichen Grüßen – With kind regards
 
Wolfgang Huse


On 5/4/20, 10:40 AM, "Strljic, Matthias Milan" <matthias.strljic@isw.uni-stuttgart.de>
wrote:

    Hi Wolfgang,


    i added a small example to the ManualPLC4XOpcua file which will be included into the up
comming OPC docu.

    It is stripped down to the functions which are required to  create a subscription and
does not include a further embedding into an application.

    I hope it helps you!


    String fieldAddress = "ns=2;s=HelloWorld/ScalarTypes/String"; // String in opc ua format
to describe target value
    String fieldName = "field1"; // local unique key for the requested variable
    String connectionString = "opcua:tcp://127.0.0.1:12686/milo?discovery=false"; // Address
of the opc ua server

    PlcConnection opcuaSubConnection =  new PlcDriverManager().getConnection(connectionString);
// build connection
    PlcSubscriptionRequest.Builder subBuilder =  opcuaSubConnection.subscriptionRequestBuilder();
// get builder
    subBuilder.addChangeOfStateField(fieldName, fieldAddress); // add the tuple of fieldName
and Address to the request
    PlcSubscriptionRequest subReq = subBuilder.build(); // build the request
    PlcSubscriptionResponse subResp = subReq.execute().get(); // execute the build up of a
subscription
    Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your
Information"); // create a consumer function for the subscription
    PlcConsumerRegistration registration = subResp.getSubscriptionHandle(fieldName).register(consumer);
// add the consumer to the created subscription  of the request and access it over the  SubscriptionHandler
    registration.unregister(); // Unsubscribe
    // You done :)



    Beste regards

    Matthias

    ________________________________
    Von: Wolfgang Huse <wolfgang.huse@nutanix.com>
    Gesendet: Sonntag, 3. Mai 2020 15:02:51
    An: dev@plc4x.apache.org
    Betreff: Java Example for Subscription

    Hi,
    could anybody provide me a small example how to use Subscription?
    I am able to subscribe as example to a opcua field but I have difficulties to create the
callback-function which is triggered when new values arrive.

    I had a look at different examples but most of them use synchronous communication or async
read but not Subscriptions.

    Mit freundlichen Grüßen – With kind regards

    Wolfgang Huse



Mime
View raw message