|
@@ -51,13 +51,22 @@ public class MODBUS implements Protocol {
|
|
//Function Request Codes
|
|
//Function Request Codes
|
|
public static final int READ_COILS = 1;
|
|
public static final int READ_COILS = 1;
|
|
public static final int READ_INPUT_DISCRETES = 2;
|
|
public static final int READ_INPUT_DISCRETES = 2;
|
|
|
|
+ public static final int READ_HOLDING_REGISTERS=3;
|
|
public static final int READ_INPUT_REGISTERS = 4;
|
|
public static final int READ_INPUT_REGISTERS = 4;
|
|
public static final int WRITE_COIL = 5;
|
|
public static final int WRITE_COIL = 5;
|
|
public static final int WRITE_SINGLE_REGISTER = 6;
|
|
public static final int WRITE_SINGLE_REGISTER = 6;
|
|
public static final int MODBUS_SERVICE = 17;
|
|
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_COILS = 15; Not sure of these commands
|
|
//public static final int WRITE_MULTIPLE_REGISTERS = 16; Not sure of these commands
|
|
//public static final int WRITE_MULTIPLE_REGISTERS = 16; Not sure of these commands
|
|
|
|
|
|
|
|
+ //Packet Constants
|
|
|
|
+
|
|
|
|
+ int FUNCTION_CODE;
|
|
|
|
+ int UNIT_NUMBER;
|
|
|
|
+ int DATA_ADDRESS;
|
|
|
|
+ int SIZE;
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
public int sid=1; // Denotes the Unit Number or Slave_ID of the device
|
|
public int sid=1; // Denotes the Unit Number or Slave_ID of the device
|
|
@@ -112,20 +121,52 @@ public class MODBUS implements Protocol {
|
|
break;
|
|
break;
|
|
|
|
|
|
case READ_INPUT_REGISTERS:
|
|
case READ_INPUT_REGISTERS:
|
|
|
|
+
|
|
|
|
+ sid=(request[6]);
|
|
int registerAddress = (request[9]);
|
|
int registerAddress = (request[9]);
|
|
- request[9]=(byte)readRegister(registerAddress);
|
|
|
|
- responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if(sid==1){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && registerAddress >= ANALOG_INPUT_START_ADDRESS && registerAddress<=ANALOG_INPUT_MAX_DATA_ADDRESS) {
|
|
|
|
+ request[9] = (byte) readRegister(registerAddress);
|
|
|
|
+ responsePackets.add(new Packet(request, getDeviceInfo()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && registerAddress < ANALOG_INPUT_START_ADDRESS || registerAddress > ANALOG_INPUT_MAX_DATA_ADDRESS ){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
+ case READ_HOLDING_REGISTERS:
|
|
|
|
+ sid=request[6];
|
|
|
|
+ int holdingRegisterAddress=request[9];
|
|
|
|
+
|
|
|
|
+ if (sid==1){
|
|
|
|
+ //exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if (sid==2 && holdingRegisterAddress >=HOLDING_REGISTERS_START_ADDRESS && holdingRegisterAddress <= HOLDING_REGISTERS_MAX_ADDRESS){
|
|
|
|
+
|
|
|
|
+ request[9] = (byte) readRegister(holdingRegisterAddress);
|
|
|
|
+ responsePackets.add(new Packet(request, getDeviceInfo()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && holdingRegisterAddress < HOLDING_REGISTERS_START_ADDRESS || holdingRegisterAddress > HOLDING_REGISTERS_MAX_ADDRESS ){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
case READ_COILS:
|
|
case READ_COILS:
|
|
|
|
|
|
sid= (request[6]);
|
|
sid= (request[6]);
|
|
int address = (request[9]);
|
|
int address = (request[9]);
|
|
|
|
|
|
- System.out.println(address);
|
|
|
|
-
|
|
|
|
- if(sid==1&&address<COIL_MAX_DATA_ADDRESS||address>0){
|
|
|
|
|
|
+ if(sid==1&&address<COIL_MAX_DATA_ADDRESS && address>=COIL_START_ADDRESS){
|
|
|
|
|
|
request[5]=4;
|
|
request[5]=4;
|
|
|
|
|
|
@@ -134,10 +175,9 @@ public class MODBUS implements Protocol {
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
- else if(sid==2&& address<0){
|
|
|
|
-
|
|
|
|
- int length= request.length;
|
|
|
|
|
|
|
|
|
|
+ //Imitating Siemens Simatic S7-200 Architecture
|
|
|
|
+ else if(sid==1 && address<COIL_START_ADDRESS || address>COIL_MAX_DATA_ADDRESS){
|
|
|
|
|
|
request[7]=(byte)129;
|
|
request[7]=(byte)129;
|
|
request[8]=(byte)2;
|
|
request[8]=(byte)2;
|
|
@@ -145,37 +185,106 @@ public class MODBUS implements Protocol {
|
|
request[10]=0;
|
|
request[10]=0;
|
|
request[11]=0;
|
|
request[11]=0;
|
|
|
|
|
|
- //byte[] message = new byte[request.length-2];
|
|
|
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ case READ_INPUT_DISCRETES:
|
|
|
|
|
|
- // System.arraycopy(request, 0, message, 0, request.length - 4);
|
|
|
|
|
|
+ sid =request[6];
|
|
|
|
+ int inputAddress = (request[9]);
|
|
|
|
|
|
- // message[7]=(byte)129;
|
|
|
|
- // message[8]=(byte)2;
|
|
|
|
- // message[5]=3;
|
|
|
|
|
|
+ if(sid==1&& inputAddress>DISCRETE_MAX_DATA_ADDRESS || inputAddress<DISCRETE_START_ADDRESS){
|
|
|
|
+ request[7]=(byte)129;
|
|
|
|
+ request[8]=(byte)2;
|
|
|
|
+ request[9]=0;
|
|
|
|
+ request[10]=0;
|
|
|
|
+ request[11]=0;
|
|
|
|
|
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==1&&inputAddress<DISCRETE_MAX_DATA_ADDRESS && inputAddress>=DISCRETE_START_ADDRESS){
|
|
|
|
|
|
- // System.out.println(message);
|
|
|
|
|
|
|
|
|
|
+ request[5]=4;
|
|
|
|
+ request[9]=(byte)readDiscrete(inputAddress);
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ else if(sid==2){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
case WRITE_COIL:
|
|
case WRITE_COIL:
|
|
|
|
+
|
|
|
|
+ sid=request[6];
|
|
|
|
+
|
|
int coilAddress = (request[9]);
|
|
int coilAddress = (request[9]);
|
|
int coilData = (request[10]);
|
|
int coilData = (request[10]);
|
|
- writeCoil(coilAddress, coilData);
|
|
|
|
- responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
|
|
+
|
|
|
|
+ if(sid==2){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==1 && coilAddress>COIL_MAX_DATA_ADDRESS){
|
|
|
|
+ //exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==1 && coilAddress<=COIL_MAX_DATA_ADDRESS && coilAddress>=COIL_START_ADDRESS) {
|
|
|
|
+ writeCoil(coilAddress, coilData);
|
|
|
|
+ responsePackets.add(new Packet(request, getDeviceInfo()));
|
|
|
|
+ }
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WRITE_SINGLE_REGISTER:
|
|
case WRITE_SINGLE_REGISTER:
|
|
|
|
+
|
|
|
|
+ sid = request[6];
|
|
int regAddress=(request[9]);
|
|
int regAddress=(request[9]);
|
|
int regData=(request[10]);
|
|
int regData=(request[10]);
|
|
- writeSingleRegister(regAddress,regData);
|
|
|
|
- responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
|
|
+
|
|
|
|
+ if (sid==1){
|
|
|
|
+ //exception
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && regAddress >= ANALOG_INPUT_START_ADDRESS && regAddress<=ANALOG_INPUT_MAX_DATA_ADDRESS) {
|
|
|
|
+
|
|
|
|
+ writeSingleRegister(regAddress,regData);
|
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && regAddress < ANALOG_INPUT_START_ADDRESS || regAddress > ANALOG_INPUT_MAX_DATA_ADDRESS ){
|
|
|
|
+ //Exception packet
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if (sid==2 && regAddress >=HOLDING_REGISTERS_START_ADDRESS && regAddress <= HOLDING_REGISTERS_MAX_ADDRESS){
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ writeSingleRegister(regAddress,regData);
|
|
|
|
+ responsePackets.add(new Packet(request,getDeviceInfo()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ else if(sid==2 && regAddress < HOLDING_REGISTERS_START_ADDRESS || regAddress > HOLDING_REGISTERS_MAX_ADDRESS ) {
|
|
|
|
+
|
|
|
|
+ //Exception Packet
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
@@ -189,7 +298,7 @@ public class MODBUS implements Protocol {
|
|
//Read Coil function
|
|
//Read Coil function
|
|
public int readCoil(int address) {
|
|
public int readCoil(int address) {
|
|
|
|
|
|
- address+=1;//offset 1
|
|
|
|
|
|
+ address+=1;//has an offset 1
|
|
|
|
|
|
if (coil.containsKey(address)) {
|
|
if (coil.containsKey(address)) {
|
|
int val = (Integer) coil.get(address);
|
|
int val = (Integer) coil.get(address);
|
|
@@ -216,7 +325,7 @@ public class MODBUS implements Protocol {
|
|
|
|
|
|
private String getDeviceInfo() {
|
|
private String getDeviceInfo() {
|
|
|
|
|
|
- DeviceInfo = "Siemens SIMATIC S7-200";
|
|
|
|
|
|
+ DeviceInfo = "5369656d656e732053494d415449432053372d323030";
|
|
return DeviceInfo;
|
|
return DeviceInfo;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -273,6 +382,26 @@ public class MODBUS implements Protocol {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ //Read Coil function
|
|
|
|
+ public int readDiscrete(int address) {
|
|
|
|
+
|
|
|
|
+ address+=1;//offset 1
|
|
|
|
+
|
|
|
|
+ if (discreteInput.containsKey(address)) {
|
|
|
|
+ int val = (Integer) discreteInput.get(address);
|
|
|
|
+ return val;
|
|
|
|
+ } else {
|
|
|
|
+ discreteInput.put(address, rand());
|
|
|
|
+ //System.out.println(coil);
|
|
|
|
+
|
|
|
|
+ int val = (Integer) discreteInput.get(address);
|
|
|
|
+ System.out.println("Address:" + String.valueOf(address) + "Data:" + String.valueOf(val));
|
|
|
|
+ return val;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
/* gets the type of request made from the master */
|
|
/* gets the type of request made from the master */
|
|
@@ -291,6 +420,12 @@ public class MODBUS implements Protocol {
|
|
} else if (requestType == 4) {
|
|
} else if (requestType == 4) {
|
|
requestType = READ_INPUT_REGISTERS;
|
|
requestType = READ_INPUT_REGISTERS;
|
|
}
|
|
}
|
|
|
|
+ else if (requestType==2){
|
|
|
|
+ requestType = READ_INPUT_DISCRETES;
|
|
|
|
+ }
|
|
|
|
+ else if (requestType==3){
|
|
|
|
+ requestType = READ_HOLDING_REGISTERS;
|
|
|
|
+ }
|
|
|
|
|
|
// System.out.println(requestType);
|
|
// System.out.println(requestType);
|
|
return requestType;
|
|
return requestType;
|
|
@@ -304,4 +439,3 @@ public class MODBUS implements Protocol {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|