Skip to content

Commit 59349bf

Browse files
committed
Fixed issue on Add operation when handling FLOAT32 type
1 parent c880d36 commit 59349bf

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/main/kotlin/ConfigurationParser.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ data class Linear(
8989
data class Sub(
9090
@field:XmlAttribute(required = true)
9191
val symbol: String,
92-
@field:XmlAttribute(required = true)
92+
@field:XmlValue
9393
val value: String,
9494
){
9595
constructor(): this("", "")
@@ -101,10 +101,10 @@ data class Sub(
101101
data class Add(
102102
@field:XmlAttribute(required = true)
103103
val symbol: String,
104-
@field:XmlAttribute(required = true)
104+
@field:XmlValue
105105
val value: String,
106106
){
107-
constructor(): this("", "")
107+
constructor(): this("", "0")
108108
}
109109

110110
//<delay>100</delay>

src/main/kotlin/PlcSimulation.kt

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PlcSimulation(
3535
}
3636

3737
is Add -> {
38-
//TODO
38+
addOperation(element, configuration, memory)
3939
}
4040

4141
is Sub -> {
@@ -61,6 +61,62 @@ class PlcSimulation(
6161

6262
}
6363

64+
private fun addOperation(element: Add, configuration: Configuration, memory: PlcMemory){
65+
println("Add symbol ${element.symbol} value ${element.value}")
66+
var variable = configuration.registers.getVarConfiguration(element.symbol)
67+
if (variable == null) {
68+
println("ERROR: Symbol ${element.symbol} not found during Set execution")
69+
throw CancellationException("Error - Add")
70+
} else {
71+
if(variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT){
72+
println("ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Add operation")
73+
throw CancellationException("Error - Add")
74+
}
75+
76+
when (variable.addressType) {
77+
78+
AddressType.HOLDING_REGISTER -> {
79+
//get the current value
80+
//add
81+
//set back the new value
82+
83+
if (variable.datatype == "FLOAT32") {
84+
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2)
85+
if(currentValue.isEmpty()){
86+
println("ERROR: Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
87+
throw CancellationException("Error - Add")
88+
}
89+
val intValue = (( currentValue[1].toInt() shl 16) or (currentValue[0].toInt() and 0xFFFF))
90+
val currentFloatValue = java.lang.Float.intBitsToFloat(intValue)
91+
var floatValue = element.value.toFloat()
92+
floatValue += currentFloatValue
93+
setHoldingRegisterFloat32(floatValue, memory, variable)
94+
} else {
95+
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1)
96+
if(currentValue.isEmpty()){
97+
println("ERROR: Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
98+
throw CancellationException("Error - Add")
99+
}
100+
//Int16 VALIDATED!
101+
var intValue = element.value.toInt()
102+
intValue += currentValue.first().toInt()
103+
setHoldingRegisterInt16(memory, variable, intValue.toShort())
104+
}
105+
}
106+
AddressType.INPUT_REGISTER -> {
107+
//TODO NOT TESTED, NOT VALIDATED!!
108+
val currentValue = memory.readInputRegister(variable.address.toInt(), 1)
109+
val newValue = currentValue.first() + element.value.toShort()
110+
memory.setInputRegister(variable.address.toInt(),newValue.toShort())
111+
}
112+
113+
else -> {
114+
throw CancellationException("Error - Add")
115+
}
116+
}
117+
}
118+
}
119+
64120
private fun setOperation(element: Set, configuration: Configuration, memory: PlcMemory) {
65121
println("Set symbol ${element.symbol} value ${element.value}")
66122
var variable = configuration.registers.getVarConfiguration(element.symbol)

0 commit comments

Comments
 (0)