|
@@ -30,6 +30,8 @@ public class MODBUS implements Protocol {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ private StringBuffer command = new StringBuffer();
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
@@ -54,7 +56,11 @@ public class MODBUS implements Protocol {
|
|
|
HashMap<Integer,Integer> coil = new HashMap<Integer,Integer>();
|
|
|
HashMap<Integer,Integer> register = new HashMap<Integer,Integer>();
|
|
|
|
|
|
- public int readcoil(int address) {
|
|
|
+
|
|
|
+ //Read Coil function
|
|
|
+ public int readCoil(int address) {
|
|
|
+
|
|
|
+ address+=1;//offset 1
|
|
|
|
|
|
if (coil.containsKey(address)) {
|
|
|
int val = (Integer) coil.get(address);
|
|
@@ -68,6 +74,7 @@ public class MODBUS implements Protocol {
|
|
|
return val;
|
|
|
}
|
|
|
}
|
|
|
+ //Random input of 0 & 1 for coils
|
|
|
private int rand() {
|
|
|
|
|
|
int num =(Math.random()<0.5)?0:1;
|
|
@@ -114,24 +121,36 @@ public class MODBUS implements Protocol {
|
|
|
|
|
|
case MODBUS_SERVICE:
|
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+ responsePackets.add(new Packet(getDeviceInfo()+"\r\n","EE:FF:66:88:GH:JI:DJ"));
|
|
|
break;
|
|
|
|
|
|
case READ_INPUT_REGISTERS:
|
|
|
+ int registerAddress = (request[9]);
|
|
|
+ request[9]=(byte)readRegister(registerAddress);
|
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+
|
|
|
break;
|
|
|
|
|
|
case READ_COILS:
|
|
|
int address = (request[9]);
|
|
|
request[5]=4;
|
|
|
|
|
|
- request[9]=(byte)readcoil(address);
|
|
|
+ request[9]=(byte)readCoil(address);
|
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
break;
|
|
|
|
|
|
case WRITE_COIL:
|
|
|
int coilAddress = (request[9]);
|
|
|
int coilData = (request[10]);
|
|
|
- writeCoil(coilAddress,coilData);
|
|
|
+ writeCoil(coilAddress, coilData);
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case WRITE_SINGLE_REGISTER:
|
|
|
+ int regAddress=(request[9]);
|
|
|
+ int regData=(request[10]);
|
|
|
+ writeSingleRegister(regAddress,regData);
|
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
break;
|
|
|
|
|
@@ -142,8 +161,49 @@ public class MODBUS implements Protocol {
|
|
|
return responsePackets;
|
|
|
}
|
|
|
|
|
|
+ private int readRegister(int registerAddress) {
|
|
|
+
|
|
|
+ registerAddress+=30001; //Offset of 30001
|
|
|
+
|
|
|
+ if (register.containsKey(registerAddress)) {
|
|
|
+ int val = (Integer) register.get(registerAddress);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private int randvalue(){
|
|
|
+
|
|
|
+ int num =(Math.random()<0.5)?0:255; //Max Hex value that can be stored in 10 bit Binary is 255
|
|
|
+ return num;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private int writeSingleRegister(int regAddress, int regData){
|
|
|
+
|
|
|
+ regData+=30001;
|
|
|
+ register.put(regAddress,regData);
|
|
|
+ int val = (Integer)register.get(regAddress);
|
|
|
+ return val;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private int writeCoil(int coilAddress, int coilData) {
|
|
|
|
|
|
+ coilAddress+=1;//offset 1
|
|
|
+
|
|
|
coil.put(coilAddress,coilData);
|
|
|
|
|
|
int val=(Integer)coil.get(coilAddress);
|
|
@@ -152,6 +212,9 @@ public class MODBUS implements Protocol {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/* gets the type of request made from the master */
|
|
|
private int getRequestType(byte[] request) {
|
|
|
|
|
@@ -169,7 +232,7 @@ public class MODBUS implements Protocol {
|
|
|
requestType = READ_INPUT_REGISTERS;
|
|
|
}
|
|
|
|
|
|
- System.out.println(requestType);
|
|
|
+ // System.out.println(requestType);
|
|
|
return requestType;
|
|
|
|
|
|
|