วันพุธที่ 24 มกราคม พ.ศ. 2561

moka7 example read/write logo 8




****************  Read  ************************


package com.amjoey.moka7connectlogoplc;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import moka7.*;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override    protected void onResume() {
        super.onResume();
        // we're going to simulate real time with thread that append data to the graph        new Thread(new Runnable() {

            @Override            public void run() {
                // we add 100 new entries                for (int i = 0; i < 100; i++) {
                    runOnUiThread(new Runnable() {

                        @Override                        public void run() {
                            new PlcReader().execute("");
                        }
                    });

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
            }
        }).start();
    }

    S7Client client = new S7Client();

    public void readdb_val(View v){
        new PlcReader().execute("");
    }


    private class PlcReader extends AsyncTask<String, Void, String>{

        String ret = "";

        @Override        protected String doInBackground(String... params){

            try{
                client.SetConnectionType(S7.S7_BASIC);
                int res=client.ConnectTo("192.168.0.3",0,0);

                if(res==0){//connection OK
                    byte[] data = new byte[4];
                    res = client.ReadArea(S7.S7AreaDB,1,10,4,data);
                  //  ret = "value of DB1.DBD25: "+ S7.GetFloatAt(data,0);                  //  ret = "value of DB1.DBD10: "+ S7.GetWordAt(data,0);                    ret = "Value of DB1.DBD10: "+ S7.GetWordAt(data,0)+"/"+ S7.GetWordAt(data,2);
 /*                    byte[] dataWrite = new byte[2];                   // S7.SetBitAt(dataWrite, 0, 1, true);                   // S7.SetDIntAt(dataWrite,0,5);                    S7.SetWordAt(dataWrite,0,700);
                    client.WriteArea(S7.S7AreaDB, 1, 12, 2, dataWrite);                    ret = "WriteArea of DB1.DBD12: OK ";                    */

                }else{
                    ret = "ERR: "+ S7Client.ErrorText(res);
                }
                client.Disconnect();
            }catch (Exception e) {
                ret = "EXC: "+e.toString();
                Thread.interrupted();
            }
            return "executed";
        }

        @Override        protected void onPostExecute(String result){
            TextView txout = (TextView) findViewById(R.id.textView);
            txout.setText(ret);
        }
    }
}





****************  Write  ************************

package com.amjoey.mokawriteplc;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import Moka7.*;


public class MainActivity extends AppCompatActivity {

    int iMemoryAdd;
    int bMemoryVal;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    S7Client client = new S7Client();

    public void readdb_val(View v){
        EditText mwAdd = (EditText) findViewById(R.id.txtMWAddress);
        EditText mwVal = (EditText) findViewById(R.id.txtValue);

        if (!mwAdd.getText().toString().equals("")){
            iMemoryAdd = Integer.parseInt(mwAdd.getText().toString());
        }else{
            iMemoryAdd = 0;
        }

        if (!mwVal.getText().toString().equals("")){
            bMemoryVal = Integer.parseInt(mwVal.getText().toString());
        }else{
            bMemoryVal = 0;
        }


        new PlcReader().execute("");
    }

    private class PlcReader extends AsyncTask<String, Void, String>{

        String ret = "";



        @Override        protected String doInBackground(String... params){


            try{
                client.SetConnectionType(S7.S7_BASIC);
                int res=client.ConnectTo("192.168.0.3",0,0);

                if(res==0){//connection OK                     /*                    byte[] data = new byte[4];                    res = client.ReadArea(S7.S7AreaDB,1,10,2,data);                  //  ret = "value of DB1.DBD25: "+ S7.GetFloatAt(data,0);                    ret = "value of DB1.DBD10: "+ S7.GetWordAt(data,0);                    */
                    byte[] dataWrite = new byte[2];
                    // S7.SetBitAt(dataWrite, 0, 1, true);                    // S7.SetDIntAt(dataWrite,0,5);                    S7.SetWordAt(dataWrite,0,bMemoryVal);

                    client.WriteArea(S7.S7AreaDB, 1, iMemoryAdd, 2, dataWrite);
                    ret = "WriteArea of DB1.DBD" + iMemoryAdd + " : "+bMemoryVal;


                }else{
                    ret = "ERR: "+ S7Client.ErrorText(res);
                }
                client.Disconnect();
            }catch (Exception e) {
                ret = "EXC: "+e.toString();
                Thread.interrupted();
            }
            return "executed";
        }

        @Override        protected void onPostExecute(String result){


            TextView txout = (TextView) findViewById(R.id.textView);
            txout.setText(ret);
        }
    }

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น