initial commit
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Handler;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class DisplayMessageActivity extends AppCompatActivity {
|
||||
|
||||
private Handler handlerCoil;
|
||||
private GlobalState state;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_display_message);
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("readCoil8192"));
|
||||
state = (GlobalState) getApplicationContext();
|
||||
|
||||
handlerCoil = new Handler();
|
||||
handlerCoil.post(refreshCoil);
|
||||
}
|
||||
|
||||
Runnable refreshCoil = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
|
||||
serviceIntent.setAction("read.coil");
|
||||
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
|
||||
serviceIntent.putExtra("extra.ip.port", state.getPort());
|
||||
serviceIntent.putExtra("extra.ref", state.getCoilRef());
|
||||
serviceIntent.putExtra("extra.count", state.getCoilCount());
|
||||
serviceIntent.putExtra("extra.intent.name", "readCoil8192");
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
|
||||
handlerCoil.postDelayed(this, 2000);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable refreshDiscretInput = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO Auto-generated method stub
|
||||
TextView textView = findViewById(R.id.textView1);
|
||||
Date currentTime = Calendar.getInstance().getTime();
|
||||
textView.setText(new Double(Math.random()).toString());
|
||||
handlerCoil.postDelayed(this, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable refreshInputRegister = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO Auto-generated method stub
|
||||
TextView textView = findViewById(R.id.textView1);
|
||||
Date currentTime = Calendar.getInstance().getTime();
|
||||
textView.setText(new Double(Math.random()).toString());
|
||||
handlerCoil.postDelayed(this, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable refreshHoldingRegister = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO Auto-generated method stub
|
||||
TextView textView = findViewById(R.id.textView1);
|
||||
Date currentTime = Calendar.getInstance().getTime();
|
||||
textView.setText(new Double(Math.random()).toString());
|
||||
handlerCoil.postDelayed(this, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Bundle bundle = intent.getExtras();
|
||||
boolean[] booleanArray = bundle.getBooleanArray("values");
|
||||
|
||||
TextView textView1 = findViewById(R.id.textView1);
|
||||
textView1.setText(new Boolean(booleanArray[0]).toString());
|
||||
|
||||
TextView textView2 = findViewById(R.id.textView2);
|
||||
textView2.setText(new Boolean(booleanArray[1]).toString());
|
||||
|
||||
TextView textView3 = findViewById(R.id.textView3);
|
||||
textView3.setText(new Boolean(booleanArray[2]).toString());
|
||||
|
||||
TextView textView4 = findViewById(R.id.textView4);
|
||||
textView4.setText(new Boolean(booleanArray[3]).toString());
|
||||
|
||||
TextView textView5 = findViewById(R.id.textView5);
|
||||
textView5.setText(new Boolean(booleanArray[4]).toString());
|
||||
|
||||
TextView textView6 = findViewById(R.id.textView6);
|
||||
textView6.setText(new Boolean(booleanArray[5]).toString());
|
||||
|
||||
TextView textView7 = findViewById(R.id.textView7);
|
||||
textView7.setText(new Boolean(booleanArray[6]).toString());
|
||||
|
||||
TextView textView8 = findViewById(R.id.textView8);
|
||||
textView8.setText(new Boolean(booleanArray[7]).toString());
|
||||
}
|
||||
};
|
||||
}
|
51
app/src/main/java/com/example/user/myapp/GlobalState.java
Normal file
51
app/src/main/java/com/example/user/myapp/GlobalState.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class GlobalState extends Application {
|
||||
|
||||
// coil
|
||||
private String ipAddress;
|
||||
private int port = 502;
|
||||
private int coilRef;
|
||||
private int coilCount;
|
||||
|
||||
// getter/setter
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public void setIpAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getCoilCount() {
|
||||
return coilCount;
|
||||
}
|
||||
|
||||
public void setCoilCount(int coilCount) {
|
||||
this.coilCount = coilCount;
|
||||
}
|
||||
|
||||
public void setCoilRef(int coilRef) {
|
||||
this.coilRef = coilRef;
|
||||
}
|
||||
|
||||
public int getCoilRef() {
|
||||
return coilRef;
|
||||
}
|
||||
}
|
52
app/src/main/java/com/example/user/myapp/MainActivity.java
Normal file
52
app/src/main/java/com/example/user/myapp/MainActivity.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
/** Called when the user taps the Send button */
|
||||
public void sendMessage(View view) {
|
||||
EditText editText1 = (EditText) findViewById(R.id.ipAddress);
|
||||
String ipAddress = editText1.getText().toString();
|
||||
|
||||
EditText editText2 = (EditText) findViewById(R.id.port);
|
||||
int port = Integer.parseInt(editText2.getText().toString());
|
||||
|
||||
EditText editText3 = (EditText) findViewById(R.id.ref);
|
||||
int ref = Integer.parseInt(editText3.getText().toString());
|
||||
|
||||
EditText editText4 = (EditText) findViewById(R.id.count);
|
||||
int count = Integer.parseInt(editText4.getText().toString());
|
||||
|
||||
|
||||
GlobalState state = (GlobalState) getApplicationContext();
|
||||
state.setIpAddress(ipAddress);
|
||||
state.setPort(port);
|
||||
state.setCoilRef(ref);
|
||||
state.setCoilCount(count);
|
||||
|
||||
Intent serviceIntent = new Intent(this, MyIntentService.class);
|
||||
serviceIntent.setAction("read.coil");
|
||||
serviceIntent.putExtra("extra.ip.address", ipAddress);
|
||||
serviceIntent.putExtra("extra.ip.port", port);
|
||||
serviceIntent.putExtra("extra.ref", ref);
|
||||
serviceIntent.putExtra("extra.count", count);
|
||||
serviceIntent.putExtra("extra.intent.name", "readCoil8192");
|
||||
this.startService(serviceIntent);
|
||||
|
||||
Intent nextIntent = new Intent(this, DisplayMessageActivity.class);
|
||||
this.startActivity(nextIntent);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,13 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class ManualDriveActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_manual_drive);
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class MeasurementActivity extends AppCompatActivity {
|
||||
|
||||
Boolean myBoolean = false;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_measurement);
|
||||
}
|
||||
|
||||
|
||||
public boolean toggle () {
|
||||
return myBoolean ? false: true;
|
||||
}
|
||||
}
|
115
app/src/main/java/com/example/user/myapp/MyIntentService.java
Normal file
115
app/src/main/java/com/example/user/myapp/MyIntentService.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
|
||||
import com.ghgande.j2mod.modbus.ModbusException;
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.io.ModbusTransaction;
|
||||
import com.ghgande.j2mod.modbus.msg.ModbusResponse;
|
||||
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
||||
import com.ghgande.j2mod.modbus.util.BitVector;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An {@link IntentService} subclass for handling asynchronous task requests in
|
||||
* a service on a separate handler thread.
|
||||
* <p>
|
||||
* TODO: Customize class - update intent actions, extra parameters and static
|
||||
* helper methods.
|
||||
*/
|
||||
public class MyIntentService extends IntentService {
|
||||
// TODO: Rename actions, choose action names that describe tasks that this
|
||||
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
|
||||
// private static final String ACTION_FOO = "com.example.user.myapp.action.FOO";
|
||||
private static final String ACTION_READ_COIL = "read.coil";
|
||||
|
||||
private ModbusTCPMaster master;
|
||||
|
||||
// TODO: Rename parameters
|
||||
private static final String EXTRA_IP_ADDRESS = "extra.ip.address";
|
||||
private static final String EXTRA_PORT = "extra.ip.port";
|
||||
private static final String EXTRA_REF = "extra.ref";
|
||||
private static final String EXTRA_COUNT = "extra.count";
|
||||
private static final String EXTRA_INTENT_NAME = "extra.intent.name";
|
||||
|
||||
// private static final String EXTRA_PARAM2 = "com.example.user.myapp.extra.PARAM2";
|
||||
|
||||
public MyIntentService() {
|
||||
super("MyIntentService");
|
||||
}
|
||||
|
||||
private void sendBooleanListToActivity(List<Boolean> booleanList, String intentName) {
|
||||
Intent intent = new Intent(intentName);
|
||||
Bundle bundle = new Bundle();
|
||||
|
||||
bundle.putBooleanArray("values", toPrimitiveArray(booleanList));
|
||||
intent.putExtras(bundle);
|
||||
sendLocationBroadcast(intent);
|
||||
}
|
||||
|
||||
private void sendLocationBroadcast(Intent intent) {
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public void readCoilAction (Context context, String ipAddress, int port, int ref, int count, String intentReceiveName) {
|
||||
try {
|
||||
master = new ModbusTCPMaster(ipAddress, port);
|
||||
master.connect();
|
||||
|
||||
BitVector bv = master.readCoils(ref,count);
|
||||
List<Boolean> listBooleanBit = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
listBooleanBit.add(bv.getBit(i));
|
||||
}
|
||||
sendBooleanListToActivity(listBooleanBit, intentReceiveName);
|
||||
master.disconnect();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception in reading coil " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
GlobalState state = (GlobalState) getApplicationContext();
|
||||
if (intent != null) {
|
||||
final String action = intent.getAction();
|
||||
final String ipAddress = intent.getStringExtra(EXTRA_IP_ADDRESS);
|
||||
final int port = intent.getIntExtra(EXTRA_PORT, 502);
|
||||
final int ref = intent.getIntExtra(EXTRA_REF, 8192);
|
||||
final int count = intent.getIntExtra(EXTRA_COUNT, 8);
|
||||
final String intentName = intent.getStringExtra(EXTRA_INTENT_NAME);
|
||||
|
||||
switch(action) {
|
||||
//case ACTION_FOO :
|
||||
// handleActionFoo(param1, param2);
|
||||
// break;
|
||||
case ACTION_READ_COIL :
|
||||
handleReadCoilAction(ipAddress, port, ref, count, intentName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle action Baz in the provided background thread with the provided
|
||||
* parameters.
|
||||
*/
|
||||
private void handleReadCoilAction(String ipAddress, int port, int ref, int count, String intentName) {
|
||||
readCoilAction(this, ipAddress, port, ref, count, intentName);
|
||||
}
|
||||
|
||||
private boolean[] toPrimitiveArray(final List<Boolean> booleanList) {
|
||||
final boolean[] primitives = new boolean[booleanList.size()];
|
||||
int index = 0;
|
||||
for (Boolean object : booleanList) {
|
||||
primitives[index++] = object;
|
||||
}
|
||||
return primitives;
|
||||
}
|
||||
}
|
89
app/src/main/java/com/example/user/myapp/MyTest.java
Normal file
89
app/src/main/java/com/example/user/myapp/MyTest.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package com.example.user.myapp;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import com.ghgande.j2mod.modbus.ModbusException;
|
||||
import com.ghgande.j2mod.modbus.facade.ModbusTCPMaster;
|
||||
import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction;
|
||||
import com.ghgande.j2mod.modbus.io.ModbusTransaction;
|
||||
import com.ghgande.j2mod.modbus.msg.ModbusResponse;
|
||||
import com.ghgande.j2mod.modbus.msg.ReadCoilsRequest;
|
||||
import com.ghgande.j2mod.modbus.msg.ReadCoilsResponse;
|
||||
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
||||
import com.ghgande.j2mod.modbus.util.BitVector;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class MyTest extends AsyncTask<String, Void, String> {
|
||||
|
||||
// private ModbusTCPMaster master = null;
|
||||
TCPMasterConnection connection = null;
|
||||
Activity prevActivityContext = null;
|
||||
|
||||
public MyTest (Activity context, TCPMasterConnection connection) {
|
||||
this.connection = connection;
|
||||
this.prevActivityContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(String...strings) {
|
||||
System.out.println("Pass !! ");
|
||||
boolean open = false;
|
||||
try {
|
||||
// master = new ModbusTCPMaster(strings[0], 503);
|
||||
|
||||
// InetAddress inetAddress = InetAddress.getByName("192.168.157.16");
|
||||
// TCPMasterConnection connection = new TCPMasterConnection(inetAddress);
|
||||
// connection.setPort(503);
|
||||
connection.connect();
|
||||
open = connection.isConnected();
|
||||
//
|
||||
System.out.print("am i open : "+open);
|
||||
|
||||
|
||||
|
||||
ReadCoilsRequest request = new ReadCoilsRequest(8192, 8);
|
||||
|
||||
ModbusTransaction transaction = new ModbusTCPTransaction(connection);
|
||||
transaction.setRequest(request);
|
||||
transaction.execute();
|
||||
|
||||
BitVector bv = ((ReadCoilsResponse) getAndCheckResponse(transaction)).getCoils();
|
||||
// bv.forceSize(count);
|
||||
System.out.println("my bit :"+bv.getBit(0));
|
||||
connection.close();
|
||||
|
||||
// this.master.connect();
|
||||
// master.connect();
|
||||
// return null;
|
||||
return "i'm connected";
|
||||
} catch (Exception e) {
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
protected void onPostExecute(String string) {
|
||||
// // try {
|
||||
System.out.println("finished");
|
||||
System.out.println(string);
|
||||
|
||||
Intent intent = new Intent(this.prevActivityContext, DisplayMessageActivity.class);
|
||||
// intent.putExtra("connection", (Object) this.master);
|
||||
// System.out.println (master);
|
||||
|
||||
prevActivityContext.startActivity(intent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private ModbusResponse getAndCheckResponse(ModbusTransaction transaction) throws ModbusException {
|
||||
ModbusResponse res = transaction.getResponse();
|
||||
if (res == null) {
|
||||
throw new ModbusException("No response");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user