beckhoff — 6.4 Transmitting structures to the PLC

Samples: TcJavaToAds else { for (int i = 0; i { // PLC datatype int consists of two bytes. Get them. byte lowByte = dataBuff.getByteArray()[i]; byte highByte = dataBuff.getByteArray()[i+1]; // Create new byte[]. Little endian! byte[] valBytes = { lowByte, highByte }; // Integer value: byte[] to int int valInt = Convert.ByteArrToShort(valBytes); System.out.println("Value of PLCVar[" + i/2 + "]: " + valInt); } } // Close communication err = AdsCallDllFunction.adsPortClose(); if(err!=0) { System.out.println("Error: Close Communication: 0x" + Long.toHexString(err)); } try{ System.in.read(); } catch (Exception e){ System.out.println("Error:

Close program"); } } } 6.4 Transmitting structures to the PLC This sample shows the transfer of a structure to the PLC via ADS. The structure consists of elements of different data types: Unpack the sample program 'Transmitting structures to the PLC': https://infosys.beckhoff.com/content/1033/ tcjavatoads/Resources/12475094411/.zip To execute the *.jar sample, the command 'java -classpath "Sample04.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.

import de.beckhoff.jni.JNIByteBuffer; import de.beckhoff.jni.tcads.AmsAddr; import de.beckhoff.jni.tcads.AdsCallDllFunction; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { public static void main(String[] args) { long err; AmsAddr addr = new AmsAddr(); TransferObject transfer = new TransferObject(); // See additional class JNIByteBuffer dataBuff = new JNIByteBuffer(19); // 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!"); } // Use JNIByteBuffer as a backing array for ByteBuffer ByteBuffer bb = ByteBuffer.wrap(dataBuff.getByteArray()); // Write elements to buffer. Litte Endian! bb.order(ByteOrder.LITTLE_ENDIAN); TX1000 34 Version: 1.1