all reading
This commit is contained in:
parent
2046758cc4
commit
7d6385c9c4
@ -16,6 +16,8 @@ import java.util.Date;
|
||||
public class DisplayMessageActivity extends AppCompatActivity {
|
||||
|
||||
private Handler handlerCoil;
|
||||
private Handler handlerInputRegister;
|
||||
|
||||
private GlobalState state;
|
||||
|
||||
@Override
|
||||
@ -23,11 +25,20 @@ public class DisplayMessageActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_display_message);
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("readCoil8192"));
|
||||
// listener
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(coilMessageReceiver, new IntentFilter("readOnlyCoil"));
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(discreteInputMessageReceiver, new IntentFilter("readOnlyDiscreteInput"));
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(inputRegisterMessageReceiver, new IntentFilter("readOnlyInputRegister"));
|
||||
|
||||
|
||||
state = (GlobalState) getApplicationContext();
|
||||
|
||||
//handler
|
||||
handlerCoil = new Handler();
|
||||
handlerCoil.post(refreshCoil);
|
||||
|
||||
handlerInputRegister = new Handler();
|
||||
handlerInputRegister.post(refreshInputRegister);
|
||||
}
|
||||
|
||||
Runnable refreshCoil = new Runnable() {
|
||||
@ -39,7 +50,7 @@ public class DisplayMessageActivity extends AppCompatActivity {
|
||||
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");
|
||||
serviceIntent.putExtra("extra.intent.name", "readOnlyCoil");
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
|
||||
handlerCoil.postDelayed(this, 2000);
|
||||
@ -68,23 +79,13 @@ public class DisplayMessageActivity extends AppCompatActivity {
|
||||
}
|
||||
};
|
||||
|
||||
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() {
|
||||
// on message receive, update the view with values
|
||||
private BroadcastReceiver coilMessageReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Bundle bundle = intent.getExtras();
|
||||
boolean[] booleanArray = bundle.getBooleanArray("values");
|
||||
boolean[] booleanArray = bundle.getBooleanArray("coilValues");
|
||||
|
||||
TextView textView1 = findViewById(R.id.textView1);
|
||||
textView1.setText(new Boolean(booleanArray[0]).toString());
|
||||
@ -111,4 +112,18 @@ public class DisplayMessageActivity extends AppCompatActivity {
|
||||
textView8.setText(new Boolean(booleanArray[7]).toString());
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver inputRegisterMessageReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
private BroadcastReceiver discreteInputMessageReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -16,6 +16,31 @@ public class GlobalState extends Application {
|
||||
private int coilRef;
|
||||
private int coilCount;
|
||||
|
||||
//
|
||||
public enum InputConfig {
|
||||
COIL (8192, 8),
|
||||
INPUT_REGISTER (1, 1),
|
||||
DISCRETE_INPUT(1, 12);
|
||||
|
||||
private int startReference;
|
||||
private int count;
|
||||
|
||||
InputConfig (int startReference, int count) {
|
||||
this.startReference = startReference;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
|
||||
// getter/setter
|
||||
public int getStartReference() {
|
||||
return startReference;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
// getter/setter
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
|
@ -18,32 +18,53 @@ public class MainActivity extends AppCompatActivity {
|
||||
/** 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();
|
||||
//String ipAddress = editText1.getText().toString();
|
||||
String ipAddress ="192.168.157.16";
|
||||
|
||||
EditText editText2 = (EditText) findViewById(R.id.port);
|
||||
int port = Integer.parseInt(editText2.getText().toString());
|
||||
// int port = Integer.parseInt(editText2.getText().toString());
|
||||
int port = 503;
|
||||
|
||||
EditText editText3 = (EditText) findViewById(R.id.ref);
|
||||
int ref = Integer.parseInt(editText3.getText().toString());
|
||||
// int ref = Integer.parseInt(editText3.getText().toString());
|
||||
|
||||
EditText editText4 = (EditText) findViewById(R.id.count);
|
||||
int count = Integer.parseInt(editText4.getText().toString());
|
||||
|
||||
// int count = Integer.parseInt(editText4.getText().toString());
|
||||
int ref = 1;
|
||||
int count = 8;
|
||||
|
||||
GlobalState state = (GlobalState) getApplicationContext();
|
||||
state.setIpAddress(ipAddress);
|
||||
state.setPort(port);
|
||||
state.setCoilRef(ref);
|
||||
state.setCoilCount(count);
|
||||
// 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 coilServiceIntent = new Intent(this, MyIntentService.class);
|
||||
coilServiceIntent.setAction("read.coil");
|
||||
coilServiceIntent.putExtra("extra.ip.address", ipAddress);
|
||||
coilServiceIntent.putExtra("extra.ip.port", port);
|
||||
coilServiceIntent.putExtra("extra.ref", GlobalState.InputConfig.COIL.getStartReference());
|
||||
coilServiceIntent.putExtra("extra.count", GlobalState.InputConfig.COIL.getCount());
|
||||
coilServiceIntent.putExtra("extra.intent.name", "readOnlyCoil");
|
||||
this.startService(coilServiceIntent);
|
||||
|
||||
Intent discretInputserviceIntent = new Intent(this, MyIntentService.class);
|
||||
discretInputserviceIntent.setAction("read.discrete.input");
|
||||
discretInputserviceIntent.putExtra("extra.ip.address", ipAddress);
|
||||
discretInputserviceIntent.putExtra("extra.ip.port", port);
|
||||
discretInputserviceIntent.putExtra("extra.ref", GlobalState.InputConfig.DISCRETE_INPUT.getStartReference());
|
||||
discretInputserviceIntent.putExtra("extra.count", GlobalState.InputConfig.DISCRETE_INPUT.getCount());
|
||||
discretInputserviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput");
|
||||
this.startService(discretInputserviceIntent);
|
||||
|
||||
Intent inputRegisterServiceIntent = new Intent(this, MyIntentService.class);
|
||||
inputRegisterServiceIntent.setAction("read.input.register");
|
||||
inputRegisterServiceIntent.putExtra("extra.ip.address", ipAddress);
|
||||
inputRegisterServiceIntent.putExtra("extra.ip.port", port);
|
||||
inputRegisterServiceIntent.putExtra("extra.ref", GlobalState.InputConfig.INPUT_REGISTER.getStartReference());
|
||||
inputRegisterServiceIntent.putExtra("extra.count", GlobalState.InputConfig.INPUT_REGISTER.getCount());
|
||||
inputRegisterServiceIntent.putExtra("extra.intent.name", "readOnlyInputRegister");
|
||||
this.startService(inputRegisterServiceIntent);
|
||||
|
||||
Intent nextIntent = new Intent(this, DisplayMessageActivity.class);
|
||||
this.startActivity(nextIntent);
|
||||
|
@ -2,18 +2,13 @@ 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.procimg.InputRegister;
|
||||
import com.ghgande.j2mod.modbus.util.BitVector;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -25,10 +20,10 @@ import java.util.List;
|
||||
* 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 static final String ACTION_READ_INPUT_REGISTER = "read.input.register";
|
||||
private static final String ACTION_READ_DISCRETE_INPUT = "read.discrete.input";
|
||||
|
||||
private ModbusTCPMaster master;
|
||||
|
||||
@ -49,7 +44,16 @@ public class MyIntentService extends IntentService {
|
||||
Intent intent = new Intent(intentName);
|
||||
Bundle bundle = new Bundle();
|
||||
|
||||
bundle.putBooleanArray("values", toPrimitiveArray(booleanList));
|
||||
bundle.putBooleanArray("coilValues", toPrimitiveArray(booleanList));
|
||||
intent.putExtras(bundle);
|
||||
sendLocationBroadcast(intent);
|
||||
}
|
||||
|
||||
private void sendIntegerListToActivity (ArrayList<Integer> integerList, String intentName) {
|
||||
Intent intent = new Intent(intentName);
|
||||
Bundle bundle = new Bundle();
|
||||
|
||||
bundle.putIntegerArrayList("irValues", integerList);
|
||||
intent.putExtras(bundle);
|
||||
sendLocationBroadcast(intent);
|
||||
}
|
||||
@ -58,7 +62,24 @@ public class MyIntentService extends IntentService {
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public void readCoilAction (Context context, String ipAddress, int port, int ref, int count, String intentReceiveName) {
|
||||
public void readDiscreteInputAction (String ipAddress, int port, int ref, int count, String intentReceiveName) {
|
||||
try {
|
||||
master = new ModbusTCPMaster(ipAddress, port);
|
||||
master.connect();
|
||||
|
||||
BitVector bv = master.readInputDiscretes(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 discrete input " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public void readCoilAction (String ipAddress, int port, int ref, int count, String intentReceiveName) {
|
||||
try {
|
||||
master = new ModbusTCPMaster(ipAddress, port);
|
||||
master.connect();
|
||||
@ -75,9 +96,24 @@ public class MyIntentService extends IntentService {
|
||||
}
|
||||
}
|
||||
|
||||
public void readInputRegisterAction (String ipAddress, int port, int ref, int count, String intentReceiveName) {
|
||||
try {
|
||||
master = new ModbusTCPMaster(ipAddress, port);
|
||||
master.connect();
|
||||
|
||||
InputRegister [] inputRegisters = master.readInputRegisters(ref, count);
|
||||
ArrayList<Integer> listIntegerInputRegister = new ArrayList<>();
|
||||
for (InputRegister ir : inputRegisters) {
|
||||
listIntegerInputRegister.add(ir.getValue());
|
||||
}
|
||||
sendIntegerListToActivity (listIntegerInputRegister, intentReceiveName);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception in reading input register " + 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);
|
||||
@ -87,23 +123,16 @@ public class MyIntentService extends IntentService {
|
||||
final String intentName = intent.getStringExtra(EXTRA_INTENT_NAME);
|
||||
|
||||
switch(action) {
|
||||
//case ACTION_FOO :
|
||||
// handleActionFoo(param1, param2);
|
||||
// break;
|
||||
case ACTION_READ_DISCRETE_INPUT:
|
||||
readDiscreteInputAction (ipAddress, port, ref, count, intentName);
|
||||
case ACTION_READ_COIL :
|
||||
handleReadCoilAction(ipAddress, port, ref, count, intentName);
|
||||
readCoilAction(ipAddress, port, ref, count, intentName);
|
||||
case ACTION_READ_INPUT_REGISTER :
|
||||
readInputRegisterAction (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;
|
||||
|
@ -1,89 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user