@@ -35,7 +35,7 @@ class PlcSimulation(
35
35
}
36
36
37
37
is Add -> {
38
- // TODO
38
+ addOperation(element, configuration, memory)
39
39
}
40
40
41
41
is Sub -> {
@@ -61,6 +61,62 @@ class PlcSimulation(
61
61
62
62
}
63
63
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
+
64
120
private fun setOperation (element : Set , configuration : Configuration , memory : PlcMemory ) {
65
121
println (" Set symbol ${element.symbol} value ${element.value} " )
66
122
var variable = configuration.registers.getVarConfiguration(element.symbol)
0 commit comments