first writing

This commit is contained in:
misty 2018-06-17 01:46:42 +02:00
parent c75fee9bfd
commit 07e7445672
6 changed files with 175 additions and 51 deletions

View File

@ -12,7 +12,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".ManualDriveActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -34,7 +34,7 @@
android:exported="false" />
<activity android:name=".MeasurementActivity" />
<activity android:name=".ManualDriveActivity"></activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>

View File

@ -8,6 +8,7 @@ import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

View File

@ -11,17 +11,10 @@ import java.net.UnknownHostException;
public class GlobalState extends Application {
// coil
private String ipAddress;
private int port = 502;
private int coilRef;
private int coilCount;
private String ipAddress = "192.168.157.16";
private int port = 503;
//
public enum InputConfig {
/* Q_COIL (8192, 8),
AI_IR (1, 1),
I_DI(1, 12);
*/
Q_COIL(8192, 8),
VM_COIL2 (1, 8),
VM_COIL3 (65, 8),
@ -63,20 +56,4 @@ public class GlobalState extends Application {
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;
}
}

View File

@ -7,6 +7,19 @@ import android.view.View;
public class MainActivity extends AppCompatActivity {
// constant for extra in intent
private static final String EXTRA_IP_ADDRESS = "extra.ip.address";
private static final String EXTRA_IP_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";
// constant for action name
private static final String ACTION_READ_COIL = "read.coil";
private static final String ACTION_READ_DISCRETE_INPUT = "read.discrete.input";
private static final String ACTION_READ_INPUT_REGISTER = "read.input.register";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -20,8 +33,8 @@ public class MainActivity extends AppCompatActivity {
//String ipAddress = editText1.getText().toString();
//String ipAddress = "172.16.202.14";
String ipAddress = "10.0.0.60";
int port = 502;
String ipAddress = "192.168.157.16";
int port = 503;
//EditText editText2 = (EditText) findViewById(R.id.port);
//int port = Integer.parseInt(editText2.getText().toString());
@ -30,33 +43,53 @@ public class MainActivity extends AppCompatActivity {
state.setIpAddress(ipAddress);
state.setPort(port);
// read only
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.Q_COIL.getStartReference());
coilServiceIntent.putExtra("extra.count", GlobalState.InputConfig.Q_COIL.getCount());
coilServiceIntent.putExtra("extra.intent.name", "readOnlyCoil");
coilServiceIntent.setAction(ACTION_READ_COIL);
coilServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
coilServiceIntent.putExtra(EXTRA_IP_PORT, port);
coilServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.Q_COIL.getStartReference());
coilServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.Q_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.I_DI.getStartReference());
discretInputserviceIntent.putExtra("extra.count", GlobalState.InputConfig.I_DI.getCount());
discretInputserviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput");
discretInputserviceIntent.setAction(ACTION_READ_DISCRETE_INPUT);
discretInputserviceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
discretInputserviceIntent.putExtra(EXTRA_IP_PORT, port);
discretInputserviceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.I_DI.getStartReference());
discretInputserviceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.I_DI.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.AI_IR.getStartReference());
inputRegisterServiceIntent.putExtra("extra.count", GlobalState.InputConfig.AI_IR.getCount());
inputRegisterServiceIntent.putExtra("extra.intent.name", "readOnlyInputRegister");
inputRegisterServiceIntent.setAction(ACTION_READ_INPUT_REGISTER);
inputRegisterServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
inputRegisterServiceIntent.putExtra(EXTRA_IP_PORT, port);
inputRegisterServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.AI_IR.getStartReference());
inputRegisterServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.AI_IR.getCount());
inputRegisterServiceIntent.putExtra(EXTRA_INTENT_NAME, "readOnlyInputRegister");
this.startService(inputRegisterServiceIntent);
Intent vmCoil2ServiceIntent = new Intent(this, MyIntentService.class);
vmCoil2ServiceIntent.setAction(ACTION_READ_COIL);
vmCoil2ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
vmCoil2ServiceIntent.putExtra(EXTRA_IP_PORT, port);
vmCoil2ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL2.getStartReference());
vmCoil2ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL2.getCount());
vmCoil2ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil2");
this.startService(vmCoil2ServiceIntent);
Intent vmCoil3ServiceIntent = new Intent(this, MyIntentService.class);
vmCoil3ServiceIntent.setAction(ACTION_READ_COIL);
vmCoil3ServiceIntent.putExtra(EXTRA_IP_ADDRESS, ipAddress);
vmCoil3ServiceIntent.putExtra(EXTRA_IP_PORT, port);
vmCoil3ServiceIntent.putExtra(EXTRA_REF, GlobalState.InputConfig.VM_COIL3.getStartReference());
vmCoil3ServiceIntent.putExtra(EXTRA_COUNT, GlobalState.InputConfig.VM_COIL3.getCount());
vmCoil3ServiceIntent.putExtra(EXTRA_INTENT_NAME, "readVmCoil3");
this.startService(vmCoil3ServiceIntent);
Intent nextIntent = new Intent(this, DisplayMessageActivity.class);
this.startActivity(nextIntent);
}

View File

@ -1,13 +1,83 @@
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.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class ManualDriveActivity extends AppCompatActivity {
private GlobalState state;
private Handler coilHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2"));
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
state = (GlobalState) getApplicationContext();
coilHandler = new Handler();
// coilHandler.post(refreshCoil);
setContentView(R.layout.activity_manual_drive);
ToggleButton button = (ToggleButton) findViewById(R.id.h1_control_button);
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// writing the value true
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.VM_COIL2.getStartReference());
serviceIntent.putExtra("extra.bit", true);
serviceIntent.putExtra("extra.intent.name", "readVmCoil2");
getApplicationContext().startService(serviceIntent);
} else {
// writing the value false
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.VM_COIL2.getStartReference());
serviceIntent.putExtra("extra.bit", false);
serviceIntent.putExtra("extra.intent.name", "readVmCoil2");
getApplicationContext().startService(serviceIntent);
}
}
});
}
private BroadcastReceiver coilWriterMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean supposedToBeValue = intent.getBooleanExtra("coil", false);
ImageView lightOn = (ImageView) findViewById(R.id.h1_status_light_on);
ImageView lightOff = (ImageView) findViewById(R.id.h1_status_light_off);
if (supposedToBeValue) {
lightOn.setVisibility(View.VISIBLE);
lightOff.setVisibility(View.INVISIBLE);
} else {
lightOn.setVisibility(View.INVISIBLE);
lightOff.setVisibility(View.VISIBLE);
}
}
};
}

View File

@ -21,10 +21,16 @@ import java.util.List;
*/
public class MyIntentService extends IntentService {
// read action
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";
// write action
private static final String ACTION_WRITE_COIL = "write.coil";
private static final String ACTION_CHECK_COIL = "check.coil";
private ModbusTCPMaster master;
// TODO: Rename parameters
@ -33,8 +39,10 @@ public class MyIntentService extends IntentService {
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_BIT_TO_SET = "extra.bit";
//turns out modbus on siemens logo is crap and isnt responding for at least 50ms
private static final Integer CAPWAITTIME = 100;
private static final Integer CAP_WAIT_TIME = 100;
//this is not a type, idiot.
public MyIntentService() {
@ -74,7 +82,7 @@ public class MyIntentService extends IntentService {
listBooleanBit.add(bv.getBit(i));
}
sendBooleanListToActivity(listBooleanBit, intentReceiveName, "discreteInputValues");
Thread.sleep(CAPWAITTIME);
Thread.sleep(CAP_WAIT_TIME);
master.disconnect();
} catch (Exception e) {
System.out.println("Exception in reading discrete input " + e);
@ -93,7 +101,7 @@ public class MyIntentService extends IntentService {
listBooleanBit.add(bv.getBit(i));
}
sendBooleanListToActivity(listBooleanBit, intentReceiveName, "coilValues");
Thread.sleep(CAPWAITTIME);
Thread.sleep(CAP_WAIT_TIME);
master.disconnect();
} catch (Exception e) {
System.out.println("Exception in reading coil " + e);
@ -112,13 +120,39 @@ public class MyIntentService extends IntentService {
System.out.println(ir.getValue()+" input register "+Math.random());
}
sendIntegerListToActivity (listIntegerInputRegister, intentReceiveName);
Thread.sleep(CAPWAITTIME);
Thread.sleep(CAP_WAIT_TIME);
master.disconnect();
} catch (Exception e) {
System.out.println("Exception in reading input register " + e);
}
}
public void writeCoilAction (String ipAddress, int port, int ref, boolean value, String intentReceiveName) {
try {
master = new ModbusTCPMaster(ipAddress, port);
master.connect();
// writing the value
boolean expectedValue = master.writeCoil(ref, value);
System.out.println("I wrote "+expectedValue+" to coil "+ref);
Thread.sleep(CAP_WAIT_TIME);
boolean currentValue = master.readCoils(8192, 1).getBit(0);
sendNewBitValueToActivity (currentValue, intentReceiveName);
Thread.sleep(CAP_WAIT_TIME);
master.disconnect();
} catch (Exception e) {
System.out.println("Exception in reading coil " + e);
}
}
private void sendNewBitValueToActivity (boolean newValue, String intentReceive) {
Intent intent = new Intent(intentReceive);
intent.putExtra("coil", newValue);
sendLocationBroadcast(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
@ -129,6 +163,11 @@ public class MyIntentService extends IntentService {
final int count = intent.getIntExtra(EXTRA_COUNT, 8);
final String intentName = intent.getStringExtra(EXTRA_INTENT_NAME);
// for write
final Boolean value = intent.getBooleanExtra(EXTRA_BIT_TO_SET, false);
final Boolean expectedValue = intent.getBooleanExtra("extra.coil.status", false);
switch(action) {
case ACTION_READ_DISCRETE_INPUT:
readDiscreteInputAction (ipAddress, port, ref, count, intentName);
@ -139,10 +178,14 @@ public class MyIntentService extends IntentService {
case ACTION_READ_INPUT_REGISTER :
readInputRegisterAction (ipAddress, port,ref, count, intentName);
break;
case ACTION_WRITE_COIL:
writeCoilAction(ipAddress, port, ref, value, intentName);
break;
}
}
}
private boolean[] toPrimitiveArray(final List<Boolean> booleanList) {
final boolean[] primitives = new boolean[booleanList.size()];
int index = 0;