Benutzen der AdsToJava.dll
Callback-Funktionen Falls Callback-Funktionen benutzt werden, muss sichergestellt sein, dass die AdsToJava.dll in der Version 1.1.0.2 oder größer installiert ist. |
Aufgabe
SPS-Variablen mit einer JAava-Applikation lesen unter der VErwendung der Bibliothek AdsToJava.dll
Beschreibung
In diesem Beispiel werden die grundlegenden Funktionalitäten der Java Version des ADS-Stacks vorgestellt.
Um das *.jar Sample ausführen zu können, muss in der Konsole im korrekten Verzeichnis der Befehl 'java -classpath "Sample01.jar;[Pfad zu TcJavaToAds.jar] Main' ausgeführt werden (Beispielpfad: "C:TwinCAT\Ads Api\AdsToJava\*"). Dazu muss java in den Umgebungsvariablen eingetragen sein.
javac -classpath "lib/TcJavaToAds.jar;classes" -d . *.java
java -classpath "lib/TcJavaToAds.jar;classes" sample1/Application1
download sample: sample01.zip
CallAdsFuncs.java
import de.beckhoff.jni.Convert;
import de.beckhoff.jni.JNIByteBuffer;
import de.beckhoff.jni.JNILong;
import de.beckhoff.jni.tcads.AmsAddr;
import de.beckhoff.jni.tcads.AdsVersion;
import de.beckhoff.jni.tcads.AdsCallDllFunction;
public class CallAdsFuncs {
public CallAdsFuncs() {
}
long port = 0;
AmsAddr addr= new AmsAddr();
long hUser = 0;
JNILong pNotification = new JNILong();
public long openPort(boolean localNetAddr, String txtNetString, int Prt) {
if (port == 0) {
AdsVersion lAdsVersion = AdsCallDllFunction.adsGetDllVersion();
System.out.println("AdsVersion: " + new Integer(lAdsVersion.getVersion())
+ "." + new Integer(lAdsVersion.getRevision())
+ "." + lAdsVersion.getBuild());
port = AdsCallDllFunction.adsPortOpen();
//System.out.println("Port: " + port);
if (localNetAddr == true) {
addr.setNetIdString(txtNetString);
}
else {
long nErr = AdsCallDllFunction.getLocalAddress(addr); //local netid
if (nErr != 0) {
System.out.println("getLocalAddress() failed with " + nErr);
AdsCallDllFunction.adsPortClose();
return 0;
}
}
addr.mPort = Prt;
}
return port;
}
public long closePort(){
if (port != 0){
long nErr = AdsCallDllFunction.adsPortClose();
//System.out.println("Ads port closed");
port = 0;
if (nErr != 0)
System.out.println("adsPortClose() failed with " + nErr);
}
return port;
}
public long readByIGrpOffs(JNIByteBuffer databuff,long lj_idxGrp,long lj_idxOffs){
long nErr = 0;
//read by IndexGroup and IndexOffset
if (port != 0)
nErr = AdsCallDllFunction.adsSyncReadReq(addr, lj_idxGrp, lj_idxOffs,
databuff.getUsedBytesCount(), databuff);
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long writeByIGrpOffs(JNIByteBuffer databuff,long lj_idxGrp,long lj_idxOffs){
long nErr = 0;
//write by IndexGroup and IndexOffset
if (port != 0)
nErr = AdsCallDllFunction.adsSyncWriteReq(addr, lj_idxGrp, lj_idxOffs,
databuff.getUsedBytesCount(), databuff);
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long getHandleBySymbol(JNIByteBuffer hdlbuff, JNIByteBuffer symbuff){
long nErr = 0;
//get handle by symbol name (symbol like "MAIN.iCounter" name in symbuff)
if (port != 0)
nErr = AdsCallDllFunction.adsSyncReadWriteReq(addr, 0xF003, 0x0,
hdlbuff.getUsedBytesCount(), hdlbuff, //buffer for getting handle
symbuff.getUsedBytesCount(), symbuff); //buffer containg symbolpath
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long readByHandle(JNIByteBuffer databuff, long symHandle){
long nErr = 0;
if (port != 0)
{ // read variable by handle
if (symHandle != 0)
nErr = AdsCallDllFunction.adsSyncReadReq(addr, 0xF005, symHandle,
databuff.getUsedBytesCount(), databuff);
else
nErr = 1809; // error 1809 (0x711) invalid symbol handle
}
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long writeByHandle(JNIByteBuffer databuff, long symHandle){
long nErr = 0;
if (port != 0)
{ //write variable by handle
if (symHandle != 0)
nErr = AdsCallDllFunction.adsSyncWriteReq(addr, 0xF005, symHandle,
databuff.getUsedBytesCount(), databuff);
else
nErr = 1809; // error 1809 (0x711) invalid symbol handle
}
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long readBySymbol(JNIByteBuffer databuff, JNIByteBuffer symbuff){
JNIByteBuffer hdlbuff = new JNIByteBuffer(4);
long nErr = 0;
long symHandle = 0;
if (port != 0)
{
//get handle by symbol name (symbol like "MAIN.iCounter" name in symbuff)
nErr = AdsCallDllFunction.adsSyncReadWriteReq(addr, 0xF003, 0x0,
hdlbuff.getUsedBytesCount(), hdlbuff, //buffer for getting handle
symbuff.getUsedBytesCount(), symbuff); //buffer containg symbolpath
if (nErr != 0)
return nErr;
//get handle
byte[] byteArr = new byte[4];
byteArr = hdlbuff.getByteArray();
symHandle = Convert.ByteArrToInt(byteArr);
// read variable by handle
nErr = AdsCallDllFunction.adsSyncReadReq(addr, 0xF005, symHandle,
databuff.getUsedBytesCount(), databuff);
}
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
public long writeBySymbol(JNIByteBuffer databuff, JNIByteBuffer symbuff){
JNIByteBuffer hdlbuff = new JNIByteBuffer(4);
long nErr = 0;
long symHandle = 0;
if (port != 0) {
//get handle by symbol name (symbol like "MAIN.iCounter" name in symbuff)
nErr = AdsCallDllFunction.adsSyncReadWriteReq(addr, 0xF003, 0x0,
hdlbuff.getUsedBytesCount(), hdlbuff, //buffer for getting handle
symbuff.getUsedBytesCount(), symbuff); //buffer containg symbolpath
if (nErr != 0)
return nErr;
//get handle
byte[] byteArr = new byte[4];
byteArr = hdlbuff.getByteArray();
symHandle = Convert.ByteArrToInt(byteArr);
//write variable by handle
nErr = AdsCallDllFunction.adsSyncWriteReq(addr, 0xF005, symHandle,
databuff.getUsedBytesCount(), databuff);
}
else
nErr = 1864; // error 1864 (0x748) ads-port not opened
return nErr;
}
}
PLC program
PROGRAM MAIN
VAR
MdiCounter AT %MD2 : DINT := 20;
MiCounter AT %MW2 : INT := 20;
MsiCounter AT %MB2 : SINT := 20;
MfCounter AT %MD6 : REAL := 1.1;
MstrText AT %MD10 : STRING := 'qwertz';
diCounter : DINT := 40;
iCounter : INT := 40;
siCounter : SINT := 40;
fCounter : REAL := 9.3;
strText : STRING := 'asdfgh';
QdiCounter AT %QD8 : DINT := 60;
QiCounter AT %QW8 : INT := 60;
QsiCounter AT %QB8 : SINT := 60;
QfCounter AT %QD12 : REAL := 17.4;
QstrText AT %QD20 : STRING := 'yxcvbn';
Timer : TON := (PT := T#100ms);
END_VAR
Timer(IN := TRUE);
IF Timer.Q THEN
Timer(IN := FALSE);
MdiCounter := MdiCounter + 1;
MfCounter := MfCounter + 1.1;
diCounter := diCounter + 1;
iCounter := iCounter + 1;
siCounter := siCounter + 1;
fCounter := fCounter + 1.1;
QdiCounter := QdiCounter + 1;
QfCounter := QfCounter + 1.1;
END_IF