|
@@ -1,16 +1,12 @@
|
|
|
package de.tudarmstadt.informatik.hostage.protocol;
|
|
|
|
|
|
-import android.util.Log;
|
|
|
-
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.InputMismatchException;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Random;
|
|
|
|
|
|
-import de.tudarmstadt.informatik.hostage.wrapper.Packet;
|
|
|
|
|
|
-import static java.lang.System.*;
|
|
|
+import de.tudarmstadt.informatik.hostage.wrapper.Packet;
|
|
|
|
|
|
/**
|
|
|
* Created by Shreyas Srinivasa on 25.05.15.
|
|
@@ -42,54 +38,45 @@ public class MODBUS implements Protocol {
|
|
|
return TALK_FIRST.CLIENT;
|
|
|
}
|
|
|
|
|
|
- //Request Codes (keeping the most essential ones)
|
|
|
|
|
|
+
|
|
|
+ //Declarations
|
|
|
+
|
|
|
+ HashMap<Integer,Integer> coil = new HashMap<Integer,Integer>();
|
|
|
+ HashMap<Integer,Integer> register = new HashMap<Integer,Integer>();
|
|
|
+ HashMap<Integer,Integer> discreteInput = new HashMap<Integer,Integer>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //Function Request Codes
|
|
|
public static final int READ_COILS = 1;
|
|
|
- // public static final int READ_INPUT_DISCRETES = 2; Not sure of these commands
|
|
|
+ public static final int READ_INPUT_DISCRETES = 2;
|
|
|
public static final int READ_INPUT_REGISTERS = 4;
|
|
|
public static final int WRITE_COIL = 5;
|
|
|
public static final int WRITE_SINGLE_REGISTER = 6;
|
|
|
+ public static final int MODBUS_SERVICE = 17;
|
|
|
//public static final int WRITE_MULTIPLE_COILS = 15; Not sure of these commands
|
|
|
//public static final int WRITE_MULTIPLE_REGISTERS = 16; Not sure of these commands
|
|
|
- public static final int MODBUS_SERVICE = 17;
|
|
|
|
|
|
- HashMap<Integer,Integer> coil = new HashMap<Integer,Integer>();
|
|
|
- HashMap<Integer,Integer> register = new HashMap<Integer,Integer>();
|
|
|
|
|
|
|
|
|
- //Read Coil function
|
|
|
- public int readCoil(int address) {
|
|
|
+ public int sid=1; // Denotes the Unit Number or Slave_ID of the device
|
|
|
|
|
|
- address+=1;//offset 1
|
|
|
+ public static final int COIL_MAX_DATA_ADDRESS = 128; // Max coil data address
|
|
|
|
|
|
- if (coil.containsKey(address)) {
|
|
|
- int val = (Integer) coil.get(address);
|
|
|
- return val;
|
|
|
- } else {
|
|
|
- coil.put(address, rand());
|
|
|
- System.out.println(coil);
|
|
|
+ public static final int COIL_START_ADDRESS = 1; // Start address of coil
|
|
|
|
|
|
- int val = (Integer) coil.get(address);
|
|
|
- System.out.println("Address:" + String.valueOf(address) + "Data:" + String.valueOf(val));
|
|
|
- return val;
|
|
|
- }
|
|
|
- }
|
|
|
- //Random input of 0 & 1 for coils
|
|
|
- private int rand() {
|
|
|
+ public static final int DISCRETE_MAX_DATA_ADDRESS = 10032; // Max DISCRETE_INPUT data address
|
|
|
|
|
|
- int num =(Math.random()<0.5)?0:1;
|
|
|
- return num;
|
|
|
- }
|
|
|
+ public static final int DISCRETE_START_ADDRESS = 10001; // Start address of DISCRETE_INPUT
|
|
|
|
|
|
+ public static final int ANALOG_INPUT_MAX_DATA_ADDRESS = 30009; //Max ANALOG_INPUT data address
|
|
|
|
|
|
- //Device Information
|
|
|
- private String DeviceInfo = getDeviceInfo();
|
|
|
+ public static final int ANALOG_INPUT_START_ADDRESS = 30001;//Start address of ANALOG_INPUT
|
|
|
|
|
|
- private String getDeviceInfo() {
|
|
|
+ public static final int HOLDING_REGISTERS_MAX_ADDRESS = 40009; // MAX HOLDING_REGISTER data_address
|
|
|
|
|
|
- DeviceInfo = "Siemens SIMATIC S7-200";
|
|
|
- return DeviceInfo;
|
|
|
- }
|
|
|
+ public static final int HOLDING_REGISTERS_START_ADDRESS = 40001; // Start address of the HOLDING_REGISTER
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -132,11 +119,48 @@ public class MODBUS implements Protocol {
|
|
|
break;
|
|
|
|
|
|
case READ_COILS:
|
|
|
+
|
|
|
+ sid= (request[6]);
|
|
|
int address = (request[9]);
|
|
|
- request[5]=4;
|
|
|
|
|
|
- request[9]=(byte)readCoil(address);
|
|
|
- responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+ System.out.println(address);
|
|
|
+
|
|
|
+ if(sid==1&&address<COIL_MAX_DATA_ADDRESS||address>0){
|
|
|
+
|
|
|
+ request[5]=4;
|
|
|
+
|
|
|
+ request[9]=(byte)readCoil(address);
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else if(sid==2&& address<0){
|
|
|
+
|
|
|
+ int length= request.length;
|
|
|
+
|
|
|
+
|
|
|
+ request[7]=(byte)129;
|
|
|
+ request[8]=(byte)2;
|
|
|
+ request[9]=0;
|
|
|
+ request[10]=0;
|
|
|
+ request[11]=0;
|
|
|
+
|
|
|
+ //byte[] message = new byte[request.length-2];
|
|
|
+
|
|
|
+ // System.arraycopy(request, 0, message, 0, request.length - 4);
|
|
|
+
|
|
|
+ // message[7]=(byte)129;
|
|
|
+ // message[8]=(byte)2;
|
|
|
+ // message[5]=3;
|
|
|
+
|
|
|
+
|
|
|
+ // System.out.println(message);
|
|
|
+
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
break;
|
|
|
|
|
|
case WRITE_COIL:
|
|
@@ -161,6 +185,44 @@ public class MODBUS implements Protocol {
|
|
|
return responsePackets;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ //Read Coil function
|
|
|
+ public int readCoil(int address) {
|
|
|
+
|
|
|
+ address+=1;//offset 1
|
|
|
+
|
|
|
+ if (coil.containsKey(address)) {
|
|
|
+ int val = (Integer) coil.get(address);
|
|
|
+ return val;
|
|
|
+ } else {
|
|
|
+ coil.put(address, rand());
|
|
|
+ //System.out.println(coil);
|
|
|
+
|
|
|
+ int val = (Integer) coil.get(address);
|
|
|
+ System.out.println("Address:" + String.valueOf(address) + "Data:" + String.valueOf(val));
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Random input of 0 & 1 for coils
|
|
|
+ private int rand() {
|
|
|
+
|
|
|
+ int num =(Math.random()<0.5)?0:1;
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //Device Information
|
|
|
+ private String DeviceInfo = getDeviceInfo();
|
|
|
+
|
|
|
+ private String getDeviceInfo() {
|
|
|
+
|
|
|
+ DeviceInfo = "Siemens SIMATIC S7-200";
|
|
|
+ return DeviceInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private int readRegister(int registerAddress) {
|
|
|
|
|
|
registerAddress+=30001; //Offset of 30001
|
|
@@ -170,8 +232,6 @@ public class MODBUS implements Protocol {
|
|
|
return val;
|
|
|
} else {
|
|
|
register.put(registerAddress, randvalue());
|
|
|
- System.out.println(coil);
|
|
|
-
|
|
|
int val = (Integer) register.get(registerAddress);
|
|
|
System.out.println("Address:" + String.valueOf(registerAddress) + "Data:" + String.valueOf(val));
|
|
|
return val;
|
|
@@ -193,7 +253,7 @@ public class MODBUS implements Protocol {
|
|
|
private int writeSingleRegister(int regAddress, int regData){
|
|
|
|
|
|
regData+=30001;
|
|
|
- register.put(regAddress,regData);
|
|
|
+ register.put(regAddress, regData);
|
|
|
int val = (Integer)register.get(regAddress);
|
|
|
return val;
|
|
|
|