Änderungen an der Symboltabelle ereignisgesteuert erkennen

ADS-Geräte, die Symbolnamen unterstützen (SPS, NC, ...), legen die Symbolnamen in eine interne Tabelle ab. Dabei wird jedem Symbol ein Handle zugeordnet. Das Symbolhandle wird benötigt, um auf die Variablen zugreifen zu können. Ändert sich die Symboltabelle, z.B. weil ein neues SPS-Programm in die Steuerung geschrieben wird, so müssen auch die Handles neu ermittelt werden. Wie Änderungen an der Symboltabelle erkannt werden können, zeigt das folgende Beispiel:


import de.beckhoff.jni.AdsConstants;
import de.beckhoff.jni.JNILong;
import de.beckhoff.jni.tcads.AdsCallDllFunction;
import de.beckhoff.jni.tcads.AdsCallbackObject;
import de.beckhoff.jni.tcads.AdsNotificationAttrib;
import de.beckhoff.jni.tcads.AmsAddr;

public class Main {

public static void main(String[] args) {
    long err = 0;
    AmsAddr addr = new AmsAddr();

    try {
     // Open communication    
     AdsCallDllFunction.adsPortOpen();
     err = AdsCallDllFunction.getLocalAddress(addr);
     addr.setPort(851);

     if (err != 0) {
        System.out.println("Error: Open communication: 0x"
            + Long.toHexString(err));
     } else {
        System.out.println("Success: Open communication!");
     }
    
     JNILong notification = new JNILong();

     AdsNotificationAttrib attr = new AdsNotificationAttrib();
     attr.setCbLength(1);
     attr.setNTransMode(AdsConstants.ADSTRANS_SERVERONCHA);
     attr.setNMaxDelay(5000000);
     attr.setNCycleTime(5000000);

     // Create and add listener
     AdsListener listener = new AdsListener();
     AdsCallbackObject callObject = new AdsCallbackObject();
     callObject.addListenerCallbackAdsState(listener);

     AdsCallDllFunction.adsSyncAddDeviceNotificationReq(addr,
         AdsCallDllFunction.ADSIGRP_SYM_VERSION, // IndexGroup
         0,     // IndexOffset
         attr,     // The defined AdsNotificationAttrib object
         0,     // Choose arbitrary number
         notification);
     if (err != 0) {
        System.out.println("Error: Add notification: 0x"
            + Long.toHexString(err));
     }

     // Read as long as user does not press return
     System.out.println("Press enter to continue..\n");
     System.in.read();

     // Delete notificationHandle
     err = AdsCallDllFunction.adsSyncDelDeviceNotificationReq(
         addr,
         notification);
    
     if (err != 0) {
        System.out.println("Error: Remove notification: 0x"
              + Long.toHexString(err));
     }

     // Delete listener
     callObject.removeListenerCallbackAdsState(listener);

     //Close communication
     err = AdsCallDllFunction.adsPortClose();
     if (err != 0) {
        System.out.println("Error: Close Communication: 0x"
            + Long.toHexString(err));
     }

    } catch (Exception ex) {
     System.out.println(ex.getMessage());
    }
}
}


Implementierung des CallbackListenerAdsState Interface:


import de.beckhoff.jni.tcads.AdsNotificationHeader;
import de.beckhoff.jni.tcads.AmsAddr;
import de.beckhoff.jni.tcads.CallbackListenerAdsState;

public class AdsListener implements CallbackListenerAdsState {
// Callback function
@Override
public void onEvent(AmsAddr addr,
         AdsNotificationHeader notification,
         long user) {

    System.out.println("Symboltabelle hat sich geaendert!");
}
}

Beispielprogramm 'Änderungen an der Symboltabelle ereignisgesteuert erkennen' entpacken.

Um das *.jar Sample ausführen zu können, muss in der Konsole im korrekten Verzeichnis der Befehl 'java -classpath "Sample11.jar;[Pfad zu TcJavaToAds.jar] Main' ausgeführt werden (Beispielpfad: "C:TwinCAT\Ads Api\AdsToJava\*"). Dazu muss Java in den Umgebungsvariablen eingetragen sein.

Voraussetzungen

Entwicklungsumgebung

Zielplattform

Einzubindende Java-Packages

TwinCAT v3.0 Build 3102, Java

PC or CX (x86)