Certo!
Questo è il layout dell'applicazione:
Questo è il codice XML associato:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/androi"
xmlns:app="
http://schemas.android.com/apk/res-aut"
xmlns:tools="
http://schemas.android.com/tool"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="217dp"
android:layout_marginLeft="217dp"
android:layout_marginTop="487dp"
android:text="@string/val_db1_dbw0_w"
android:textSize="24sp" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="218dp"
android:layout_marginLeft="218dp"
android:layout_marginTop="536dp"
android:text="@string/val_db1_dbw2_w"
android:textSize="24sp" />
<TextView
android:id="@+id/textView"
android:layout_width="194dp"
android:layout_height="68dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="111dp"
android:layout_marginLeft="111dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="73dp"
android:layout_marginRight="73dp"
android:text="@string/android_s7"
android:textSize="36sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginTop="152dp"
android:text="@string/read_data"
android:textSize="24sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginTop="431dp"
android:text="@string/write_data"
android:textSize="24sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="89dp"
android:layout_marginLeft="89dp"
android:layout_marginTop="208dp"
android:text="@string/db1_dbw0"
android:textSize="24sp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="218dp"
android:layout_marginLeft="218dp"
android:layout_marginTop="258dp"
android:text="@string/val_db1_dbw2_r"
android:textSize="24sp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="89dp"
android:layout_marginLeft="89dp"
android:layout_marginTop="487dp"
android:text="@string/db1_dbw0"
android:textSize="24sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="90dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="257dp"
android:text="@string/db1_dbw2"
android:textSize="24sp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="89dp"
android:layout_marginLeft="89dp"
android:layout_marginTop="535dp"
android:text="@string/db1_dbw2"
android:textSize="24sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="153dp"
android:layout_marginLeft="153dp"
android:layout_marginTop="604dp"
android:onClick="writeAll"
android:text="@string/write_all"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="italic" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="154dp"
android:layout_marginLeft="154dp"
android:layout_marginTop="330dp"
android:onClick="readAll"
android:text="@string/read_all"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="italic" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="217dp"
android:layout_marginLeft="217dp"
android:layout_marginTop="209dp"
android:text="@string/val_db1_dbw0_r"
android:textSize="24sp" />
</RelativeLayout>
Questa è la classe principale:
package com.example.client;
import android.support.v7.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.client.myapplication.client.R;
import moka7.S7;
import moka7.S7Client;
public class MainActivity extends AppCompatActivity {
protected void OnCreate(Bundle savedinstancestate) {
super.onCreate(savedinstancestate);
setContentView(R.layout.activity_main);
}
S7Client client = new S7Client();
public void readAll(View v) {
new PLC_Read().execute("");
}
public class PLC_Read extends AsyncTask<String, Void, String> {
String data1 = "null";
String data2 = "null";
@Override
protected String doInBackground(String... param) {
try {
client.SetConnectionType(S7.S7_BASIC);
int check = client.ConnectTo("192.168.10.110", 0, 1);
if (check == 0) {
byte[] data = new byte[256];
client.ReadArea(S7.S7AreaDB, 1, 0, 10, data);
data1 = "" + S7.GetWordAt(data, 0);
data1 = "" + S7.GetWordAt(data, 2);
}
else {
}
client.Disconnect();
}
catch(Exception e) {
Thread.interrupted();
}
return "execute";
}
//@Override
protected void OnPostExecute(String result){
TextView textOut1=(TextView) findViewById(R.id.textView8);
textOut1.setText(data1);
TextView textOut2=(TextView) findViewById(R.id.textView9);
textOut2.setText(data2);
}
}
public void writeAll(View v) {
new PLC_Write().execute("");
}
public class PLC_Write extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... param) {
try {
client.SetConnectionType(S7.S7_BASIC);
int check = client.ConnectTo("192.168.10.110", 0, 1);
if(check==0){
@SuppressLint("WrongViewCast") EditText writeData1 = findViewById(R.id.textView10);
String data1 = writeData1.getText().toString();
@SuppressLint("WrongViewCast") EditText writeData2 = findViewById(R.id.textView11);
String data2 = writeData2.getText().toString();
byte[] data = new byte[256];
S7.SetWordAt(data,0,Integer.parseInt(data1));
S7.SetWordAt(data,2,Integer.parseInt(data2));
client.WriteArea(S7.S7AreaDB,1,0,6,data);
}
else {
}
client.Disconnect();
}
catch(Exception e) {
Thread.interrupted();
}
return "execute";
}
}
}
Spero sia utile.