read and write Field done. pushToField to do
This commit is contained in:
parent
88efeaf31a
commit
c3f15804b6
23
src/main/java/ch/hevs/isi/field/BooleanRegister.java
Normal file
23
src/main/java/ch/hevs/isi/field/BooleanRegister.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package ch.hevs.isi.field;
|
||||||
|
|
||||||
|
import ch.hevs.isi.core.BooleanDataPoint;
|
||||||
|
|
||||||
|
public class BooleanRegister extends ModbusRegister{
|
||||||
|
private boolean value;
|
||||||
|
private BooleanDataPoint bdp;
|
||||||
|
|
||||||
|
public BooleanRegister(String label, boolean isOutput, int address){
|
||||||
|
this.bdp = new BooleanDataPoint(label, isOutput);
|
||||||
|
updateMapOfRegisters(bdp, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read() {
|
||||||
|
bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write() {
|
||||||
|
ModbusAccessor.getMySelf().writeBoolean(address, bdp.getValue());
|
||||||
|
}
|
||||||
|
}
|
@ -4,31 +4,33 @@ import ch.hevs.isi.core.DataPoint;
|
|||||||
import ch.hevs.isi.core.DataPointListener;
|
import ch.hevs.isi.core.DataPointListener;
|
||||||
import ch.hevs.isi.core.FloatDataPoint;
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
|
||||||
public class FieldConnector implements DataPointListener {
|
public class FieldConnector implements DataPointListener {
|
||||||
|
|
||||||
private static FieldConnector mySelf = null;
|
private static FieldConnector mySelf = null;
|
||||||
|
|
||||||
private FieldConnector(){
|
private FieldConnector(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FieldConnector getMySelf(){
|
public static FieldConnector getMySelf(){
|
||||||
if (mySelf == null){
|
if (mySelf == null){
|
||||||
mySelf = new FieldConnector();
|
mySelf = new FieldConnector();
|
||||||
}
|
}
|
||||||
return mySelf;
|
return mySelf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(String host, int port){
|
public void initialize(String host, int port){
|
||||||
|
ModbusAccessor mbA = ModbusAccessor.getMySelf();
|
||||||
|
mbA.connect(host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pushToField(DataPoint dp){
|
private void pushToField(DataPoint dp){
|
||||||
System.out.println(dp.toString() + " -> Field");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewValue(DataPoint dp) {
|
public void onNewValue(DataPoint dp) {
|
||||||
pushToField(dp);
|
pushToField(dp);
|
||||||
|
ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp);
|
||||||
|
mr.write();
|
||||||
|
}
|
||||||
|
public void periodicalPolling(){
|
||||||
|
Timer pollTimer = new Timer();
|
||||||
|
PollTask pollTask = new PollTask();
|
||||||
|
pollTimer.scheduleAtFixedRate(pollTask,0,2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
src/main/java/ch/hevs/isi/field/FloatRegister.java
Normal file
23
src/main/java/ch/hevs/isi/field/FloatRegister.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package ch.hevs.isi.field;
|
||||||
|
|
||||||
|
import ch.hevs.isi.core.FloatDataPoint;
|
||||||
|
|
||||||
|
public class FloatRegister extends ModbusRegister{
|
||||||
|
private Float value;
|
||||||
|
private FloatDataPoint dataPoint;
|
||||||
|
|
||||||
|
public FloatRegister(String label, boolean isOutPut, int address) {
|
||||||
|
this.dataPoint = new FloatDataPoint(label, isOutPut);
|
||||||
|
updateMapOfRegisters(dataPoint,address);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read() {
|
||||||
|
dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write() {
|
||||||
|
ModbusAccessor.getMySelf().writeFloat(address, dataPoint.getValue());
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ import com.serotonin.modbus4j.exception.ModbusTransportException;
|
|||||||
import com.serotonin.modbus4j.ip.IpParameters;
|
import com.serotonin.modbus4j.ip.IpParameters;
|
||||||
import com.serotonin.modbus4j.ip.tcp.TcpMaster;
|
import com.serotonin.modbus4j.ip.tcp.TcpMaster;
|
||||||
import com.serotonin.modbus4j.locator.BaseLocator;
|
import com.serotonin.modbus4j.locator.BaseLocator;
|
||||||
|
import com.sun.org.apache.xpath.internal.operations.Mod;
|
||||||
|
|
||||||
public class ModbusAccessor {
|
public class ModbusAccessor {
|
||||||
|
|
||||||
@ -63,69 +64,95 @@ public class ModbusAccessor {
|
|||||||
* method to write a boolean value in the correct modbus register
|
* method to write a boolean value in the correct modbus register
|
||||||
* @param register address of the register
|
* @param register address of the register
|
||||||
* @param value the desired boolean value
|
* @param value the desired boolean value
|
||||||
* @throws ModbusTransportException
|
|
||||||
* @throws ErrorResponseException
|
|
||||||
*/
|
*/
|
||||||
public void writeBoolean (int register, boolean value) throws ModbusTransportException, ErrorResponseException{
|
public void writeBoolean (int register, boolean value){
|
||||||
//first parameter = slaveID = always 1 because we only have one
|
//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
|
//second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address
|
||||||
|
try{
|
||||||
this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register
|
this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register
|
||||||
}
|
}
|
||||||
|
catch (ModbusTransportException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
catch (ErrorResponseException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* method to write a float value in the correct modbus register
|
* method to write a float value in the correct modbus register
|
||||||
* @param register address of the register
|
* @param register address of the register
|
||||||
* @param value the desired float value
|
* @param value the desired float value
|
||||||
* @throws ModbusTransportException
|
|
||||||
* @throws ErrorResponseException
|
|
||||||
*/
|
*/
|
||||||
public void writeFloat (int register, float value) throws ModbusTransportException, ErrorResponseException{
|
public void writeFloat (int register, float value){
|
||||||
//first parameter = slaveID = always 1 because we only have one
|
//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
|
//second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address
|
||||||
//third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte
|
//third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte
|
||||||
|
try{
|
||||||
this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value);
|
this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value);
|
||||||
}
|
}
|
||||||
|
catch (ModbusTransportException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
catch (ErrorResponseException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
//methods to read
|
//methods to read
|
||||||
/**
|
/**
|
||||||
* method to read a boolean value in the correct modbus register
|
* method to read a boolean value in the correct modbus register
|
||||||
* get the coil status of the register.
|
* get the coil status of the register.
|
||||||
* @param register address of register
|
* @param register address of register
|
||||||
* @return boolean value of the desired register
|
* @return boolean value of the desired register
|
||||||
* @throws ModbusTransportException
|
|
||||||
* @throws ErrorResponseException
|
|
||||||
*/
|
*/
|
||||||
public boolean readBoolean(int register) throws ModbusTransportException, ErrorResponseException {
|
public boolean readBoolean(int register){
|
||||||
//first parameter = slaveID = always 1 because we only have one
|
//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
|
//second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address
|
||||||
|
try{
|
||||||
boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register));
|
boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register));
|
||||||
return booleanValue;
|
return booleanValue;
|
||||||
}
|
}
|
||||||
|
catch (ModbusTransportException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
catch (ErrorResponseException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* method to read a boolean value in the correct modbus register
|
* method to read a boolean value in the correct modbus register
|
||||||
* get the value from the holding register
|
* get the value from the holding register
|
||||||
* @param register address of the register
|
* @param register address of the register
|
||||||
* @return float value of the desired register
|
* @return float value of the desired register
|
||||||
* @throws ModbusTransportException
|
|
||||||
* @throws ErrorResponseException
|
|
||||||
*/
|
*/
|
||||||
public float readFloat(int register)throws ModbusTransportException, ErrorResponseException{
|
public float readFloat(int register){
|
||||||
//first parameter = slaveID = always 1 because we only have one
|
//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
|
//second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address
|
||||||
//third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte
|
//third parameter = DataType of the value. The max value is 3 Byte Float, but the class DataType has only 2 or 4 byte
|
||||||
|
try {
|
||||||
float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT));
|
float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT));
|
||||||
return floatValue;
|
return floatValue;
|
||||||
}
|
}
|
||||||
|
catch (ModbusTransportException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
catch (ErrorResponseException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return 0F;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* this main method is only for testing the ModbusAccessor class
|
* this main method is only for testing the ModbusAccessor class
|
||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//create an instance of ModbusAccessor and connect it with the server
|
//create an instance of ModbusAccessor and connect it with the server
|
||||||
ModbusAccessor test = ModbusAccessor.getMySelf();
|
ModbusAccessor test = ModbusAccessor.getMySelf();
|
||||||
test.connect("LocalHost", 1502);
|
test.connect("LocalHost", 1502);
|
||||||
//do this all the time
|
//do this all the time
|
||||||
while(true){
|
while(true){
|
||||||
try{
|
|
||||||
//get a boolean value => solar SetPoint
|
//get a boolean value => solar SetPoint
|
||||||
boolean solar_connect_st = test.readBoolean(609); //SOLAR_CONNECT_ST
|
boolean solar_connect_st = test.readBoolean(609); //SOLAR_CONNECT_ST
|
||||||
//get a float value => factory SetPoint
|
//get a float value => factory SetPoint
|
||||||
@ -145,13 +172,9 @@ public class ModbusAccessor {
|
|||||||
}else{
|
}else{
|
||||||
System.out.println("Solar is disconnected");
|
System.out.println("Solar is disconnected");
|
||||||
}
|
}
|
||||||
}catch (ModbusTransportException e){
|
|
||||||
|
|
||||||
}catch (ErrorResponseException e){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
29
src/main/java/ch/hevs/isi/field/ModbusRegister.java
Normal file
29
src/main/java/ch/hevs/isi/field/ModbusRegister.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package ch.hevs.isi.field;
|
||||||
|
|
||||||
|
import ch.hevs.isi.core.DataPoint;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class ModbusRegister {
|
||||||
|
protected int address;
|
||||||
|
public static HashMap<DataPoint, ModbusRegister> map = new HashMap<>();
|
||||||
|
public void updateMapOfRegisters(DataPoint dp, int address){
|
||||||
|
this.address = address;
|
||||||
|
map.put(dp,this);
|
||||||
|
}
|
||||||
|
public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){
|
||||||
|
return map.get(dp);
|
||||||
|
}
|
||||||
|
public int getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void poll(){
|
||||||
|
for (ModbusRegister mr : map.values()){
|
||||||
|
mr.read(); //read all values (registers) of the map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public abstract void read(); //abstract prototype of the method read
|
||||||
|
public abstract void write(); //abstract prototype of the method read
|
||||||
|
}
|
10
src/main/java/ch/hevs/isi/field/PollTask.java
Normal file
10
src/main/java/ch/hevs/isi/field/PollTask.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package ch.hevs.isi.field;
|
||||||
|
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
public class PollTask extends TimerTask {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ModbusRegister.poll();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user