Delete a handle of a PLC variable

This Sample shows how to delete a handle of a PLC variable:


import de.beckhoff.jni.Convert;
import de.beckhoff.jni.JNIByteBuffer;
import de.beckhoff.jni.tcads.AdsCallDllFunction;
import de.beckhoff.jni.tcads.AmsAddr;

public class Main {
public static void main(String[] args) {
    long err;
    AmsAddr addr = new AmsAddr();
    JNIByteBuffer symBuff;
    JNIByteBuffer handleBuff = new JNIByteBuffer(Integer.SIZE / Byte.SIZE);

    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!");
     }

     // Convert Symbol name to byte buffer
     symBuff = new JNIByteBuffer(PLC_VAR.getBytes());

     // Get handle via symbol name
     err = AdsCallDllFunction.adsSyncReadWriteReq(addr,
         AdsCallDllFunction.ADSIGRP_SYM_HNDBYNAME,
         0,
         handleBuff.getUsedBytesCount(),
         handleBuff,         //buffer for getting handle
         symBuff.getUsedBytesCount(),
         symBuff);         //buffer containg symbol name
    
     if(err!=0) {
        System.out.println("Error: Get Handle: 0x" + Long.toHexString(err));
     } else {
        System.out.println("Success: Get Handle!");
     }
    
     // Release handle
     err = AdsCallDllFunction.adsSyncWriteReq(addr,
         AdsCallDllFunction.ADSIGRP_SYM_RELEASEHND,
         0,
         handleBuff.getUsedBytesCount(),
         handleBuff);
    
     if(err!=0) {
        System.out.println("Error: Release Handle: 0x"
              + Long.toHexString(err));
     } else {
        System.out.println("Success: Handle removed!");
     }

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

     System.out.println("Press enter to continue..");
     System.in.read();

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

Unpack the example program 'Delete a handle of a PLC variable'

To be able to execute the *.jar sample you need to run 'java -classpath "Sample08.jar;[Path to TcJavaToAds.jar] Main' from the console (path might be "C:TwinCAT\Ads Api\AdsToJava\*"). Make sure the current path is correct and you have added java to your environment variables.

Requirements

Development Environment

Target Platform

Java Packages to include

TwinCAT v3.0 Build 3102, Java

PC or CX (x86)