From 86aeae8b6fc499897754a2fbf745a942d3574219 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Wed, 17 May 2023 16:08:18 +0200 Subject: [PATCH 1/4] ModbusAccessor finished and checked --- src/main/java/ch/hevs/isi/field/ModbusAccessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index d5612ae..5e9f96a 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -137,7 +137,7 @@ public class ModbusAccessor { test.writeBoolean(401,false); //REMOTE_SOLAR_SW //check the factory SetPoint - System.out.println("Factory Setpoint is: " + factory_st); + System.out.println("Factory SetPoint is: " + factory_st); //check the solar SetPoint if(solar_connect_st){ From c3f15804b6ec4389e14aa3dd33af2c4ffcc28999 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Tue, 23 May 2023 16:02:16 +0200 Subject: [PATCH 2/4] read and write Field done. pushToField to do --- .../ch/hevs/isi/field/BooleanRegister.java | 23 ++++++ .../ch/hevs/isi/field/FieldConnector.java | 20 +++--- .../java/ch/hevs/isi/field/FloatRegister.java | 23 ++++++ .../ch/hevs/isi/field/ModbusAccessor.java | 71 ++++++++++++------- .../ch/hevs/isi/field/ModbusRegister.java | 29 ++++++++ src/main/java/ch/hevs/isi/field/PollTask.java | 10 +++ 6 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 src/main/java/ch/hevs/isi/field/BooleanRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/FloatRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/ModbusRegister.java create mode 100644 src/main/java/ch/hevs/isi/field/PollTask.java diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java new file mode 100644 index 0000000..64c05c4 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -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()); + } +} diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index 3180d21..f0730b0 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -4,31 +4,33 @@ import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; +import java.util.Timer; + public class FieldConnector implements DataPointListener { - private static FieldConnector mySelf = null; - private FieldConnector(){ - } - public static FieldConnector getMySelf(){ if (mySelf == null){ mySelf = new FieldConnector(); } return mySelf; } - public void initialize(String host, int port){ - + ModbusAccessor mbA = ModbusAccessor.getMySelf(); + mbA.connect(host, port); } - private void pushToField(DataPoint dp){ - System.out.println(dp.toString() + " -> Field"); } - @Override public void onNewValue(DataPoint 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); } } diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java new file mode 100644 index 0000000..9af7bdd --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -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()); + } +} \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index 5e9f96a..f54253d 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -9,6 +9,7 @@ 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; +import com.sun.org.apache.xpath.internal.operations.Mod; public class ModbusAccessor { @@ -63,27 +64,39 @@ public class ModbusAccessor { * method to write a boolean value in the correct modbus register * @param register address of the register * @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 //second parameter = register address ==> it is named offset because it starts with 0 and goes to the desired address - this.master.setValue(BaseLocator.coilStatus(1,register), value); //set the desired value in the correct register + try{ + 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 * @param register address of the register * @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 //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 - this.master.setValue(BaseLocator.holdingRegister(1,register,DataType.FOUR_BYTE_FLOAT), value); + try{ + 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 /** @@ -91,41 +104,55 @@ public class ModbusAccessor { * get the coil status of the register. * @param register address of 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 //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; + try{ + boolean booleanValue = this.master.getValue(BaseLocator.coilStatus(1,register)); + 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 * get the value from the holding register * @param register address of the 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 //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 - float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT)); - return floatValue; + try { + float floatValue = (float) this.master.getValue(BaseLocator.holdingRegister(1,register, DataType.FOUR_BYTE_FLOAT)); + return floatValue; + } + catch (ModbusTransportException e){ + e.printStackTrace(); + } + catch (ErrorResponseException e){ + e.printStackTrace(); + } + return 0F; } /** * this main method is only for testing the ModbusAccessor class * @param args */ + /* public static void main(String[] args) { //create an instance of ModbusAccessor and connect it with the server ModbusAccessor test = ModbusAccessor.getMySelf(); test.connect("LocalHost", 1502); //do this all the time while(true){ - try{ //get a boolean value => solar SetPoint boolean solar_connect_st = test.readBoolean(609); //SOLAR_CONNECT_ST //get a float value => factory SetPoint @@ -145,13 +172,9 @@ public class ModbusAccessor { }else{ System.out.println("Solar is disconnected"); } - }catch (ModbusTransportException e){ - - }catch (ErrorResponseException e){ - - } } } + */ } diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java new file mode 100644 index 0000000..27f7924 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -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 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 +} diff --git a/src/main/java/ch/hevs/isi/field/PollTask.java b/src/main/java/ch/hevs/isi/field/PollTask.java new file mode 100644 index 0000000..a01ccc2 --- /dev/null +++ b/src/main/java/ch/hevs/isi/field/PollTask.java @@ -0,0 +1,10 @@ +package ch.hevs.isi.field; + +import java.util.TimerTask; + +public class PollTask extends TimerTask { + @Override + public void run() { + ModbusRegister.poll(); + } +} From 0d105fb64ecaf180595faf488f9538be573acc48 Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Fri, 26 May 2023 16:11:02 +0200 Subject: [PATCH 3/4] field not finished, connection works --- src/main/java/ch/hevs/isi/core/DataPoint.java | 2 -- .../ch/hevs/isi/field/BooleanRegister.java | 5 +-- .../ch/hevs/isi/field/FieldConnector.java | 13 +++---- .../java/ch/hevs/isi/field/FloatRegister.java | 11 +++--- .../ch/hevs/isi/field/ModbusRegister.java | 17 +++++----- src/test/java/Field.java | 34 +++++++++++++++++++ 6 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 src/test/java/Field.java diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 47f51e8..30c15c6 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -36,8 +36,6 @@ public abstract class DataPoint{ } - - /** * Just get the label of this DataPoint * @return the label in a string diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java index 64c05c4..b33c86e 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -8,16 +8,17 @@ public class BooleanRegister extends ModbusRegister{ public BooleanRegister(String label, boolean isOutput, int address){ this.bdp = new BooleanDataPoint(label, isOutput); + value = bdp.getValue(); updateMapOfRegisters(bdp, address); } @Override public void read() { - bdp.setValue(ModbusAccessor.getMySelf().readBoolean(address)); + bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); } @Override public void write() { - ModbusAccessor.getMySelf().writeBoolean(address, bdp.getValue()); + ModbusAccessor.getMySelf().writeBoolean(this.getAddress(), bdp.getValue()); } } diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index f0730b0..e146266 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -16,21 +16,22 @@ public class FieldConnector implements DataPointListener { } return mySelf; } + public void initialize(String host, int port){ - ModbusAccessor mbA = ModbusAccessor.getMySelf(); - mbA.connect(host, port); + } + private void pushToField(DataPoint dp){ + ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); + mr.write(); } @Override public void onNewValue(DataPoint dp) { pushToField(dp); - ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); - mr.write(); } - public void periodicalPolling(){ + public void startPeriodicalPolling(){ Timer pollTimer = new Timer(); PollTask pollTask = new PollTask(); - pollTimer.scheduleAtFixedRate(pollTask,0,2000); + pollTimer.scheduleAtFixedRate(pollTask,0,100); } } diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index 9af7bdd..1f0ddde 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -4,20 +4,21 @@ import ch.hevs.isi.core.FloatDataPoint; public class FloatRegister extends ModbusRegister{ private Float value; - private FloatDataPoint dataPoint; + private FloatDataPoint fdp; public FloatRegister(String label, boolean isOutPut, int address) { - this.dataPoint = new FloatDataPoint(label, isOutPut); - updateMapOfRegisters(dataPoint,address); + this.fdp = new FloatDataPoint(label, isOutPut); + value = fdp.getValue(); + updateMapOfRegisters(fdp,address); } @Override public void read() { - dataPoint.setValue(ModbusAccessor.getMySelf().readFloat(address)); + fdp.setValue(ModbusAccessor.getMySelf().readFloat(this.getAddress())); } @Override public void write() { - ModbusAccessor.getMySelf().writeFloat(address, dataPoint.getValue()); + ModbusAccessor.getMySelf().writeFloat(this.getAddress(), fdp.getValue()); } } \ No newline at end of file diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java index 27f7924..1511ea2 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusRegister.java +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -3,24 +3,25 @@ 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 map = new HashMap<>(); + private int address; + public static HashMap mapOfRegisters = 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); + mapOfRegisters.put(dp,this); } + public int getAddress() { return address; } + public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ + return mapOfRegisters.get(dp); + } + public static void poll(){ - for (ModbusRegister mr : map.values()){ + for (ModbusRegister mr : mapOfRegisters.values()){ mr.read(); //read all values (registers) of the map } } diff --git a/src/test/java/Field.java b/src/test/java/Field.java new file mode 100644 index 0000000..7d7fff6 --- /dev/null +++ b/src/test/java/Field.java @@ -0,0 +1,34 @@ +import ch.hevs.isi.core.BooleanDataPoint; +import ch.hevs.isi.core.FloatDataPoint; +import ch.hevs.isi.field.BooleanRegister; +import ch.hevs.isi.field.FieldConnector; +import ch.hevs.isi.field.FloatRegister; + +public class Field { + public static void main(String[] args) { + + + FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); + BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609); + FieldConnector.getMySelf().startPeriodicalPolling(); + + FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); + BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401); + + /* + fRO.fdp.setValue(0.8F); + bRO.bdp.setValue(true); + */ + + while(true) + { + /* + float value = fRI.dataPoint.getValue()*1000; + System.out.println(value); + */ + /* + System.out.println(bRI.bdp.getValue()); + */ + } + } +} From 55fa80c4d4200948b11f058eb0c72250e6c39fcd Mon Sep 17 00:00:00 2001 From: Nils Ritler <79571155+Slisls@users.noreply.github.com> Date: Tue, 30 May 2023 11:31:01 +0200 Subject: [PATCH 4/4] read csv file and add some comments --- src/main/java/ch/hevs/isi/core/DataPoint.java | 1 - .../ch/hevs/isi/field/BooleanRegister.java | 2 +- .../ch/hevs/isi/field/FieldConnector.java | 76 +++++++++++++++++-- .../java/ch/hevs/isi/field/FloatRegister.java | 6 +- .../ch/hevs/isi/field/ModbusAccessor.java | 1 - .../ch/hevs/isi/field/ModbusRegister.java | 31 +++++--- src/test/java/Field.java | 4 +- 7 files changed, 99 insertions(+), 22 deletions(-) diff --git a/src/main/java/ch/hevs/isi/core/DataPoint.java b/src/main/java/ch/hevs/isi/core/DataPoint.java index 30c15c6..6923ae4 100644 --- a/src/main/java/ch/hevs/isi/core/DataPoint.java +++ b/src/main/java/ch/hevs/isi/core/DataPoint.java @@ -35,7 +35,6 @@ public abstract class DataPoint{ } } - /** * Just get the label of this DataPoint * @return the label in a string diff --git a/src/main/java/ch/hevs/isi/field/BooleanRegister.java b/src/main/java/ch/hevs/isi/field/BooleanRegister.java index b33c86e..a71e5f0 100644 --- a/src/main/java/ch/hevs/isi/field/BooleanRegister.java +++ b/src/main/java/ch/hevs/isi/field/BooleanRegister.java @@ -1,6 +1,7 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.BooleanDataPoint; +import ch.hevs.isi.core.DataPoint; public class BooleanRegister extends ModbusRegister{ private boolean value; @@ -11,7 +12,6 @@ public class BooleanRegister extends ModbusRegister{ value = bdp.getValue(); updateMapOfRegisters(bdp, address); } - @Override public void read() { bdp.setValue(ModbusAccessor.getMySelf().readBoolean(this.getAddress())); diff --git a/src/main/java/ch/hevs/isi/field/FieldConnector.java b/src/main/java/ch/hevs/isi/field/FieldConnector.java index e146266..3d26045 100644 --- a/src/main/java/ch/hevs/isi/field/FieldConnector.java +++ b/src/main/java/ch/hevs/isi/field/FieldConnector.java @@ -4,12 +4,24 @@ import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.DataPointListener; import ch.hevs.isi.core.FloatDataPoint; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.util.Timer; public class FieldConnector implements DataPointListener { private static FieldConnector mySelf = null; private FieldConnector(){ + initialize("LocalHost",1502, "C:/Nils/Hesso/4_Semester/SIN/Minecraft_Electrical_Age_Project/ModbusMap.csv"); } + + /** + * 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 FieldConnector + * @return instance of FieldConnector + */ public static FieldConnector getMySelf(){ if (mySelf == null){ mySelf = new FieldConnector(); @@ -17,18 +29,70 @@ public class FieldConnector implements DataPointListener { return mySelf; } - public void initialize(String host, int port){ - + /** + * read the csv-file of the ModbusMap + * @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv) + */ + public static void createRegister(String pathToFile){ + try{ + BufferedReader csvFile = new BufferedReader(new FileReader(pathToFile)); + csvFile.readLine(); + + while(csvFile.ready()){ + String line = csvFile.readLine(); // read one line + String[] splitLine = line.split(";"); // split line between ";" + + String label = splitLine[0]; // first split is the label of the register + boolean isOutput = splitLine[3].equals("N"); // third split declares if it is an output + int address = new Integer((splitLine[4])); // fourth split is the address of the register + float range = new Float(splitLine[5]); // if it is a floatDatapoint, the fifth split is the range of the data + float offset = new Float(splitLine[6]); // if it is a floatDatapoint, the sixth split is the offset of the data + + /* + // create a float or a boolean register + if (splitLine[1].equals("B")){ + BooleanRegister b = new BooleanRegister(label, isOutput, address); + } else if(splitLine[1].equals("F")){ + FloatRegister f = new FloatRegister(label, isOutput, address, range, offset); + } + */ + } + } + catch(FileNotFoundException e){ + e.printStackTrace(); + } + catch (IOException e){ + e.printStackTrace(); + } } - private void pushToField(DataPoint dp){ - ModbusRegister mr = ModbusRegister.getRegisterFromDatapoint(dp); - mr.write(); + /** + * this method initialize the fieldConnector + * it connects the ModbusAccessor, reads the csv-file of the ModbusMap + * and starts the periodical polling of the modbus registers + * @param host ip address of the server + * @param port port of the server + * @param pathToFile path to the file of all modbus registers (C:/.../ModbusMap.csv) + */ + public void initialize(String host, int port, String pathToFile){ + ModbusAccessor.getMySelf().connect(host,port); + createRegister(pathToFile); + startPeriodicalPolling(); + } + private void pushToField(String label, String value){ + System.out.println("Field: " + label + " " + value); } @Override public void onNewValue(DataPoint dp) { - pushToField(dp); + ModbusRegister mR = ModbusRegister.getRegisterFromDatapoint(dp); + mR.write(); + pushToField(dp.getLabel(),dp.toString()); } + + /** + * method to start a periodical task + * in our case it is the reading of the modbus registers + */ public void startPeriodicalPolling(){ Timer pollTimer = new Timer(); PollTask pollTask = new PollTask(); diff --git a/src/main/java/ch/hevs/isi/field/FloatRegister.java b/src/main/java/ch/hevs/isi/field/FloatRegister.java index 1f0ddde..214bcf0 100644 --- a/src/main/java/ch/hevs/isi/field/FloatRegister.java +++ b/src/main/java/ch/hevs/isi/field/FloatRegister.java @@ -1,12 +1,14 @@ package ch.hevs.isi.field; +import ch.hevs.isi.core.DataPoint; import ch.hevs.isi.core.FloatDataPoint; +import java.util.HashMap; + public class FloatRegister extends ModbusRegister{ private Float value; private FloatDataPoint fdp; - - public FloatRegister(String label, boolean isOutPut, int address) { + public FloatRegister(String label, boolean isOutPut, int address, float range, float offset) { this.fdp = new FloatDataPoint(label, isOutPut); value = fdp.getValue(); updateMapOfRegisters(fdp,address); diff --git a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java index f54253d..bff52b3 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusAccessor.java +++ b/src/main/java/ch/hevs/isi/field/ModbusAccessor.java @@ -20,7 +20,6 @@ public class ModbusAccessor { * private constructor * */ private ModbusAccessor(){ - } /** diff --git a/src/main/java/ch/hevs/isi/field/ModbusRegister.java b/src/main/java/ch/hevs/isi/field/ModbusRegister.java index 1511ea2..f963845 100644 --- a/src/main/java/ch/hevs/isi/field/ModbusRegister.java +++ b/src/main/java/ch/hevs/isi/field/ModbusRegister.java @@ -1,25 +1,38 @@ package ch.hevs.isi.field; import ch.hevs.isi.core.DataPoint; +import ch.hevs.isi.core.FloatDataPoint; import java.util.HashMap; public abstract class ModbusRegister { private int address; - public static HashMap mapOfRegisters = new HashMap<>(); + + /** + * get the address of the modbus register + * @return address of the modbus register + */ + public int getAddress() { + return address; + } + private final static HashMap mapOfRegisters = new HashMap<>(); + + /** + * get the modbus register from the desired datapoint + * @param dp the desired datapoint + * @return modbus register + */ + public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ + return mapOfRegisters.get(dp); + } public void updateMapOfRegisters(DataPoint dp, int address){ this.address = address; mapOfRegisters.put(dp,this); } - public int getAddress() { - return address; - } - - public static ModbusRegister getRegisterFromDatapoint(DataPoint dp){ - return mapOfRegisters.get(dp); - } - + /** + * read periodically each modbus register + */ public static void poll(){ for (ModbusRegister mr : mapOfRegisters.values()){ mr.read(); //read all values (registers) of the map diff --git a/src/test/java/Field.java b/src/test/java/Field.java index 7d7fff6..7cd5802 100644 --- a/src/test/java/Field.java +++ b/src/test/java/Field.java @@ -8,11 +8,11 @@ public class Field { public static void main(String[] args) { - FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); + //FloatRegister fRI = new FloatRegister("GRID_U_FLOAT",false,89); BooleanRegister bRI = new BooleanRegister("SOLAR_CONNECT_ST", false ,609); FieldConnector.getMySelf().startPeriodicalPolling(); - FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); + //FloatRegister fRO = new FloatRegister("REMOTE_FACTORY_SP",true,205); BooleanRegister bRO = new BooleanRegister("REMOTE_SOLAR_SW", true ,401); /*