singleton pattern, readfloat and readboolean are done
This commit is contained in:
		
							
								
								
									
										107
									
								
								src/main/java/ch/hevs/isi/field/ModbusAccessor.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								src/main/java/ch/hevs/isi/field/ModbusAccessor.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,107 @@ | ||||
| package ch.hevs.isi.field; | ||||
|  | ||||
| import com.serotonin.modbus4j.ModbusFactory; | ||||
| import com.serotonin.modbus4j.ModbusMaster; | ||||
| import com.serotonin.modbus4j.code.DataType; | ||||
| import com.serotonin.modbus4j.exception.ErrorResponseException; | ||||
| import com.serotonin.modbus4j.exception.ModbusTransportException; | ||||
| import com.serotonin.modbus4j.ip.IpParameters; | ||||
| import com.serotonin.modbus4j.ip.tcp.TcpMaster; | ||||
| import com.serotonin.modbus4j.locator.BaseLocator; | ||||
|  | ||||
| public class ModbusAccessor { | ||||
|  | ||||
|     private static ModbusAccessor mySelf = null; | ||||
|     private ModbusMaster master = null; | ||||
|  | ||||
|     /** | ||||
|      * private constructor | ||||
|      * */ | ||||
|     private ModbusAccessor(){ | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * static method to create a singleton pattern of the class | ||||
|      * checks if an instance of the class is already made | ||||
|      * if not, it creates an instance of the class ModbusAccessor | ||||
|      * @return instance of ModbusAccessor | ||||
|      */ | ||||
|     public static ModbusAccessor getMySelf(){ | ||||
|         if (mySelf == null){ | ||||
|             mySelf = new ModbusAccessor(); | ||||
|         } | ||||
|         return mySelf; | ||||
|     } | ||||
|  | ||||
|     public void connect(String ipAddress, int port){ | ||||
|         ModbusFactory MF = new ModbusFactory(); | ||||
|         IpParameters IpParams = new IpParameters(); | ||||
|  | ||||
|         this.master = MF.createTcpMaster(IpParams, true); | ||||
|         /** | ||||
|          * TODO finish connect() | ||||
|          */ | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * methods to write in the modbus registers | ||||
|      */ | ||||
|  | ||||
|     /** | ||||
|      * method to write a boolean value on the modbus address | ||||
|      * @param address the address of the modbus register | ||||
|      * @param value the value to write in the register | ||||
|      */ | ||||
|     public void writeBoolean (int address, boolean value){ | ||||
|         /** | ||||
|          * TODO writeBoolean() | ||||
|          */ | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * method to write a float value on the modbus address | ||||
|      * @param address the address of the modbus register | ||||
|      * @param value the value to write in the register | ||||
|      */ | ||||
|     public void writeFloat (int address, float value){ | ||||
|         /** | ||||
|          * TODO writeFloat() | ||||
|          */ | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * methods to read from the modbus registers | ||||
|      */ | ||||
|  | ||||
|     /** | ||||
|      * method to read a boolean value in the correct modbus register | ||||
|      * get the coil status of the register. | ||||
|      * @param register address of register | ||||
|      * @return  boolean value of the desired register | ||||
|      */ | ||||
|     public boolean readBoolean(int register) throws ModbusTransportException, ErrorResponseException { | ||||
|         /** | ||||
|          * first parameter = slaveID = always 1 because we only have one | ||||
|          * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address | ||||
|          */ | ||||
|         boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); | ||||
|        return booleanValue; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * method to read a boolean value in the correct modbus register | ||||
|      * get the value from the holding register | ||||
|      * @param register address of the register | ||||
|      * @return float value of the desired register | ||||
|      */ | ||||
|     public float readFloat(int register){ | ||||
|         /** | ||||
|          * first parameter = slaveID = always 1 because we only have one | ||||
|          * second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address | ||||
|          * thrid parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte | ||||
|          */ | ||||
|        float floatValue = this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT); | ||||
|         return floatValue; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user