Consumer in Java to read and write PLC variables

This sample explains how to create an ADS-WebService-Consumer to read and write PLC-Variables via ADS-HTTP.

Unzip the sample 'WebServiceTestConsumer (Java)'.

Copy the files "TcAdsSOAP.java" and "Base64.java" to the folder of your Java-application and compile them.
Add the following line to the Java-file in which you want to use the ADS WebService:

TcAdsSOAP tcSoap = new TcAdsSOAP("http://192.168.0.2/TcAdsWebService/TcAdsWebService.dll");

The address must be the URL of your TwinCAT ADS WebService-DLL-file.

reading PLC-variables

To read PLC-variables use the following method of the TcAdsSoap-object:

DataInputStream SOAPCall(String netId, int port, long group, long offset, int cblen)

The 3 methods

short ReadInt(String netId, int port, long group, long offset)
boolean ReadBool(String netId, int port, long group, long offset) 
String ReadString(String netId, int port, long group, long offset, int cblen)

convert the variables to their corresponding type.

Example:

boolean Bool1;
try
{
    Bool1 = tcSoap.ReadBool("192.168.0.2.1.1", 801, 16416, 0)       
}
catch (Exception e)
{
    System.out.println("Error");
}

Write PLC-variables

boolean SOAPWrite(String netId, int port, long group, long offset, byte[] data)

The 3 methods

boolean WriteBool(String netId, int port, long group, long offset, boolean data)
boolean WriteInt(String netId, int port, long group, long offset, int data)
boolean WriteString(String netId, int port, long group, long offset, String data)

convert the variables to a byte-arrays.

Example:

try
{
    tcSoap.WriteBool("192.168.0.2.1.1", 801, 16416, true)   
}
catch (Exception e)
{
    System.out.println("Error");
}

PLC-program, IEC1131 daclaration of the variables:

PROGRAM MAIN
VAR
    PlcVarBool    AT %MX0.0  : BOOL := TRUE;
    PlcVarInt     AT %MW1    : INT := 1234;
    PlcVarString  AT %MD3    : STRING := 'Hello Automation';
END_VAR

The source-code:

public class AdSWebServiceSample
{
    public static void main(String args[])
    {
        TcAdsSOAP tcSoap = new TcAdsSOAP("http://192.168.0.2/TcAdsWebService/TcAdsWebService.dll");
        try
        {
            String netId = "192.168.0.2.1.1";
            int port = 801;
            int offset = 16416;
            /* write bool */
            boolean testBool = true;
            if (tcSoap.WriteBool(netId,port,offset,0,testBool))
                System.out.println("Boolean "+testBool+" Written");
            
            /* write int */ 
            int testInt = 1234;
            if (tcSoap.WriteInt(netId,port,offset,1,testInt))
                System.out.println("Integer "+testInt+" Written");
            
            /* write string */      
            String testString = "Hello Automation";
            if (tcSoap.WriteString(netId,port,offset,3,testString))
                System.out.println("String "+testString+" Written");
            


            /* read bool */
            System.out.println(String.valueOf(tcSoap.ReadBool(netId,port,offset,0) ));              
            
            /* read int */
            System.out.println(String.valueOf(tcSoap.ReadInt(netId,port,offset,1) ));
            
            /* read string */
            System.out.println(tcSoap.ReadString(netId,port,offset,3,81));


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

Complete sourcecode