beckhoff — 6.9 Event driven reading

Samples: TcJavaToAds 6.9 Event driven reading If values from a PLC or NC are to be displayed continuously on a user interface, then it is very inefficient to use AdsSyncReadReq(), since this function must be called cyclically. By defining so-called notifications, a TwinCAT server can be caused to transfer values via ADS to another ADS device. A distinction is drawn between whether the TwinCAT server is to transmit the values cyclically, or only when the values change. A notification is begun with the AdsSyncAddDeviceNotificationReq() function. After this, the callback function is automatically invoked by TwinCAT. AdsSyncDelDeviceNotificationReq() is used to halt the notification again.

Since the number of notifications is limited, you should ensure the notifications no longer required by your program are deleted. You will find further information under the description of the AdsNotificationAttrib structure. Unpack the sample program 'Event driven reading': https://infosys.beckhoff.com/content/1033/tcjavatoads/ Resources/12475101451/.zip To execute the *.jar sample, the command 'java -classpath "Sample09.jar;[path to TcJavaToAds.jar] Main' must be executed in the console in the correct directory (example path: "C:TwinCAT\Ads Api\AdsToJava\*"). For this, java must be entered in the environment variables.

The following program starts a notification on flag double word 0 in the PLC. Each time the PLC variable changes, the callback function is invoked. The callback function receives a variable of type AdsNotificationHeader() as one of its parameters. This structure contains all the necessary information (value, timestamp, ...). Time intensive actions No time-intensive actions may be executed in the callback.

import de.beckhoff.jni.AdsConstants; import de.beckhoff.jni.JNILong; import de.beckhoff.jni.tcads.AdsCallDllFunction; import de.beckhoff.jni.tcads.AdsNotificationAttrib; import de.beckhoff.jni.tcads.AdsCallbackObject; import de.beckhoff.jni.tcads.AmsAddr; public class Main { public static void main(String[] args) { try { long err; AmsAddr addr = new AmsAddr(); JNILong notification = new JNILong(); // Open communication AdsCallDllFunction.adsPortOpen(); err = AdsCallDllFunction.getLocalAddress(addr); addr.setPort(AdsCallDllFunction.AMSPORT_R0_PLC_RTS1); if (err != 0) { System.out.println("Error: Open communication: 0x" + Long.toHexString(err)); } else { System.out.println("Success:

Open communication!"); } // Specify attributes of the notificationRequest AdsNotificationAttrib attr = new AdsNotificationAttrib(); attr.setCbLength(Integer.SIZE / Byte.SIZE); attr.setNTransMode(AdsConstants.ADSTRANS_SERVERONCHA); attr.setDwChangeFilter(10000000); // 1 sec attr.setNMaxDelay(20000000); // 2 sec // Create and add listener AdsListener listener = new AdsListener(); AdsCallbackObject callObject = new AdsCallbackObject(); callObject.addListenerCallbackAdsState(listener); // Create notificationHandle err = AdsCallDllFunction.adsSyncAddDeviceNotificationReq( addr, 0x4020, // IndexGroup 0x0, // IndexOffset attr, // The defined AdsNotificationAttrib object TX1000 42 Version: 1.1