-1

我可以使用 libnodave 从 Siemens S7300 读取数据,但无法从 Siemens S71500 读取数据。在代码中,您可以看到我正在尝试从 DB2 读取前 4 个字节的数据。当我更改IP地址和插槽号时,代码在S7300上成功运行

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();
        }
    }

}
4

1 回答 1

0

与这篇文章中提到的相同的问题

从 Siemens s7-1200 (0x8104) 读取时出现 libnodave 错误

检查对 PUT/GET 的访问后,我能够从 PLC 读取数据

于 2017-02-14T15:59:54.123 回答