使用Android中的LibNoDave库,始终从PLC存储器接收0

问题描述 投票:1回答:1

我正在尝试通过Android设备与S7-1200 PLC通信(当前正在使用模拟器)。我找到了示例程序,但使它无法正常工作。

这里是主程序:

public class Avvio extends Activity {
private final static String newline = "\n";
public final static String PlcIpAddress = "10.9.15.58";
Button btnConnectRetreive, btnDelete;
TextView lblPlcAddress, txtRisultato, lblStatus, txtStatus;
TestISOTCP tp;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    settaLayout();
    gestiscoEventi();

    validaIndirizzoIp();
}

private void settaLayout() {
    lblPlcAddress = (TextView) findViewById(R.id.lblPlcAddress);
    btnConnectRetreive = (Button) findViewById(R.id.btnConnectRetreive);
    btnDelete = (Button) findViewById(R.id.btnDelete);
    lblStatus = (TextView) findViewById(R.id.lblStatus);
    txtStatus = (TextView) findViewById(R.id.txtStatus);
    txtRisultato = (TextView) findViewById(R.id.txtRisultato);

}

private void validaIndirizzoIp() {
    ValidateIP validaIp = new ValidateIP();
    if (validaIp.validate(PlcIpAddress)) {
        btnConnectRetreive.setEnabled(true);
        lblPlcAddress.setText("PLC Address: " + PlcIpAddress);
    } else {
        btnConnectRetreive.setEnabled(false);
    }
}

private void gestiscoEventi() {
    // Gestico Evento per il Bottone Collega e Leggi
    // I handle event for the button Connect Retrive
    btnConnectRetreive.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            PlcState.ErrPlc = false;
            long timeOut;

            timeOut = network.testConnection("10.9.15.58", 102, 20000);

            if (timeOut > -1) {// connection is alive
                PlcState.ErrPlc = false;
                // System.out.println("time " +
                // String.valueOf(timeOut));
                txtStatus.setText("OK " + "timeout: " + String.valueOf(timeOut));
                // Now re try to read and write
                readSomeData();
            } else {// connection is not alive
                PlcState.ErrPlc = true;
                txtStatus.setText("!!! " + "timeout: " + String.valueOf(timeOut));
            }
        }
    });

    // Gestisco Evento per cancellare i dati letti
    // I handle event for the button delete result
    btnDelete.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            txtRisultato.setText("");
        }
    });

}

private void readSomeData() {
    try {
        TestISOTCP.prepara();
        txtRisultato.append("=======================" + newline);
        txtRisultato.append("DBW1.DBB0 DoubleInt " + DB1.DBB0 + newline);
        txtRisultato.append("DBW1.DBB1 DoubleInt  " + DB1.DBB1 + newline);
        txtRisultato.append("MD0 DoubleInt " + Merker.MD0 + newline);
        txtRisultato.append("MD1 DoubleInt " + Merker.MD4 + newline);
        txtRisultato.append("MD2 DoubleInt " + Merker.MD8 + newline);
        txtRisultato.append("MD3 Float " + Merker.MD12 + newline);
    } catch (Exception e) {
        PlcState.ErrPlc = true;
        Toast.makeText(this, "Errore" + e, Toast.LENGTH_SHORT).show();

    }
}
}

这里是TestISOTCP类:

public class TestISOTCP
{
static boolean doWrite = true;
static int useProtocol = Nodave.PROTOCOL_ISOTCP;
static int slot = 2;

public int i, j;
public char buf[];
public byte buf1[];
public PLCinterface di;
public TCPConnection dc;
public Socket sock;

TestISOTCP(String host) {

    buf = new char[Nodave.OrderCodeSize];
    buf1 = new byte[Nodave.PartnerListSize];
    try
        {
            System.out.println("Sono nel costruttore");

            sock = new Socket(host, 102);
        } catch (SocketException e)
        {
            System.out.println("Errore con soket in costruttore " + e);
            e.printStackTrace();

        } catch (IOException e)
        {
            System.out.println("Errore in costruttore " + e);
            e.printStackTrace();
        }

    System.out.println("Esco dal costruttore");
}

public static void prepara()
    {
        // Plc Address to connect 
        TestISOTCP tp = new TestISOTCP("10.9.15.58");
        tp.runSomething();
    }

void runSomething()
    {
        OutputStream oStream = null;
        InputStream iStream = null;
        System.out.println("sono in run2");
        byte[] by;
        if (sock != null)
            {
                try
                    {
                        oStream = sock.getOutputStream();
                    } catch (IOException e)
                    {
                        PlcState.ErrPlc = true;
                        System.out
                                .println("oStream = sock.getOutputStream() "
                                        + e);
                    }
                try
                    {
                        iStream = sock.getInputStream();
                    } catch (IOException e)
                    {
                        PlcState.ErrPlc = true;
                        System.out
                                .println("iStream = sock.getInputStream() "
                                        + e);
                    }
                di = new PLCinterface(oStream, iStream, "IF1", 0,
                        Nodave.PROTOCOL_ISOTCP);

                dc = new TCPConnection(di, 0, slot);

                int res = dc.connectPLC();
                if (0 == res)// Here we Read from PLC
                    {
                        System.out
                                .println("Read flags");
                        dc.readBytes(Nodave.FLAGS, 1, 0, 1, null);// Read
                                                                // DataBlok
                        DB1.DBB0 = dc.getBYTE();
                        System.out.println("DB1:DW0:" + DB1.DBB0);
                        //DB1.DBB2 = dc.getWORD();
                        //System.out.println("DB1:DW1: " + DB1.DBB2);

                        /*
                        System.out
                                .println("Trying to read 16 bytes from FW0.\n");
                        dc.readBytes(Nodave.FLAGS, 0, 0, 16, null);// Read
                                                                    // Flag
                        Merker.MD0 = dc.getU32();
                        Merker.MD4 = dc.getU32();
                        Merker.MD8 = dc.getU32();
                        Merker.MD12 = dc.getFloat();
                        System.out.println("4 DWORDS " + Merker.MD0 + " "
                                + Merker.MD4 + " " + Merker.MD8);
                        System.out.println("1 Float: " + Merker.MD12);

                        if (doWrite)// Here we write from PLC
                            {
                                System.out
                                        .println("Now we write back these data after incrementing the first 3 by 1,2,3 and the float by 1.1.\n");

                                by = Nodave.bswap_32(Merker.MD0 + 1);
                                dc.writeBytes(Nodave.FLAGS, 0, 0, 4, by);

                                by = Nodave.bswap_32(Merker.MD4 + 1);
                                dc.writeBytes(Nodave.FLAGS, 0, 4, 4, by);

                                by = Nodave.bswap_32(Merker.MD8 + 1);
                                dc.writeBytes(Nodave.FLAGS, 0, 8, 4, by);

                                by = Nodave.toPLCfloat(Merker.MD12 + 1.1);
                                dc.writeBytes(Nodave.FLAGS, 0, 12, 4, by);

                                dc.readBytes(Nodave.FLAGS, 0, 0, 16, null);
                                Merker.MD0 = dc.getU32();
                                Merker.MD4 = dc.getU32();
                                Merker.MD8 = dc.getU32();
                                Merker.MD12 = dc.getFloat();
                                System.out.println("FD0: " + Merker.MD0);
                                System.out.println("FD4:" + Merker.MD4);
                                System.out.println("FD8:" + Merker.MD8);
                                System.out.println("FD12: " + Merker.MD12);

                            } // doWrite
*/
                        System.out.println("Now disconnecting\n");
                        dc.disconnectPLC();
                        di.disconnectAdapter();
                    } else
                    {
                        PlcState.ErrPlc = true;
                        System.out
                                .println("Couldn't connect to PLC. Error:"
                                        + res);
                    }
            } else
            {
                PlcState.ErrPlc = true;
                System.out.println("Couldn't open connection");
            }
    }

 }

连接似乎正在工作,我可以对其进行ping操作,并且代码的第一部分显示连接状态正常。

但是,每个返回的值都等于0,我确定我在PLC中设置了它。我还启用了PLC中的Web服务器功能(据我所知这是必需的)。

android plc siemens s7-1200 libnodave
1个回答
0
投票

您的问题是s7-300中1200插槽= 1中的插槽和400插槽= 2中的插槽

© www.soinside.com 2019 - 2024. All rights reserved.