Unable to read data from DB in Siemens S71500 using libnodave and Java

3.8k Views Asked by At

I am able to read data from Siemens S7300 using libnodave, but I was unable to read the data from Siemens S71500. In the code you can see that I am trying to read first 4 bytes of Data from DB2. The code runs successfully with S7300, when I change the IP address and the slot number

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.libnodave.Nodave;
import org.libnodave.PLCinterface;
import org.libnodave.S7Connection;
import org.libnodave.TCPConnection;

public class ConnectPLC {

    public static void main(String[] args) {
        S7Connection dc;
        String ip = "192.168.0.61";
        Socket sock;
        OutputStream oStream = null;
        InputStream iStream = null;
        PLCinterface di;
        int slot = 1;

        try {
            System.out.println("Inside the try block");
            sock = new Socket(ip, 102);

            if(sock != null) {
                oStream = sock.getOutputStream();
                iStream = sock.getInputStream();
                di = new PLCinterface(
                        oStream, 
                        iStream, 
                        "PLCInterface1", 
                        0, 
                        Nodave.PROTOCOL_ISOTCP);
                di.initAdapter();
                dc = new TCPConnection(di, 0, slot);            
                int res = dc.connectPLC();
                int x = 0;

                if (0 == res) {
                    System.out.println("++++++++++++++++++++++++++++++++++++");
                    System.out.println("PLC is connected");
                    System.out.println("++++++++++++++++++++++++++++++++++++");
                    byte[] byteArray = new byte[4];

                    x = dc.readBytes(Nodave.DB, 2, 0, 4, byteArray);
                    System.out.println("The value of x is "+x);

                    for (int i = 0; i < byteArray.length ; i++) {
                        System.out.println("The value at index "+i+" is "+byteArray[i]);
                    }
                    System.out.println(dc.getINT(0));
                    System.out.println(dc.getINT(2));
                    if (x == 0) {
                        //byte[] byteArray = dc.msgIn;
                        for (int i = 0; i < byteArray.length ; i++) {
                            System.out.println("The value at index "+i+" is "+byteArray[i]);
                        }
                        System.out.println(dc.getINT(0));
                        System.out.println(dc.getINT(2));
                    }



                } else {
                    System.out.println("PLC is not connected");
                }

                System.out.println("Now disconnecting\n");
                dc.disconnectPLC();
                di.disconnectAdapter();

                System.out.println();

            }
        } catch (UnknownHostException e) {
            System.out.println("Unknown Host Exception is Caught during the declaration of the socket");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("IO Exception is Caught during the declaration of the socket");
            e.printStackTrace();
        }
    }

}
1

There are 1 best solutions below

0
On BEST ANSWER

The same problem as mentioned in this post

libnodave error while reading from Siemens s7-1200 (0x8104)

After checking the access to PUT/GET I am able to read the data from the PLC