1
1
import kotlinx.coroutines.*
2
+ import operations.*
2
3
import java.lang.Float
3
4
4
-
5
5
class PlcSimulation (
6
6
configurationParser : ConfigurationParser ,
7
7
memory : PlcMemory ,
@@ -39,7 +39,7 @@ class PlcSimulation(
39
39
}
40
40
41
41
is Sub -> {
42
- // TODO
42
+ subOperation(element, configuration, memory)
43
43
}
44
44
45
45
else -> throw UnsupportedOperationException (" Unknown simulation step type" )
@@ -61,130 +61,12 @@ 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
64
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
- var intValue = element.value.toInt()
101
- intValue + = currentValue.first().toInt()
102
- setHoldingRegisterInt16(memory, variable, intValue.toShort())
103
- }
104
- }
105
- AddressType .INPUT_REGISTER -> {
106
- val currentValue = memory.readInputRegister(variable.address.toInt(), 1 )
107
- val newValue = currentValue.first() + element.value.toShort()
108
- memory.setInputRegister(variable.address.toInt(),newValue.toShort())
109
- }
110
-
111
- else -> {
112
- throw CancellationException (" Error - Add" )
113
- }
114
- }
115
- }
116
- }
117
65
118
- private fun setOperation (element : Set , configuration : Configuration , memory : PlcMemory ) {
119
- println (" Set symbol ${element.symbol} value ${element.value} " )
120
- var variable = configuration.registers.getVarConfiguration(element.symbol)
121
- if (variable == null ) {
122
- println (" ERROR: Symbol ${element.symbol} not found during Set execution" )
123
- throw CancellationException (" Error - Set" )
124
- } else {
125
- if ((variable.addressType == AddressType .COIL || variable.addressType == AddressType .DISCRETE_INPUT ) && (variable.value != " 0" && variable.value != " 1" )) {
126
- println (" ERROR: Invalid value format. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${variable.value} " )
127
- }
128
- when (variable.addressType) {
129
- AddressType .HOLDING_REGISTER -> {
130
- if (variable.datatype == " FLOAT32" ) {
131
- val floatValue = element.value.toFloat()
132
- setHoldingRegisterFloat32(floatValue, memory, variable)
133
- } else {
134
- // Int16
135
- val intValue = element.value.toShort()
136
- setHoldingRegisterInt16(memory, variable, intValue)
137
- }
138
- }
139
66
140
- AddressType .COIL -> {
141
- memory.forceSingleCoil(
142
- variable.address.toInt(),
143
- element.value.toBooleanFromBinary()
144
- )
145
- }
146
67
147
- AddressType .DISCRETE_INPUT -> {
148
- memory.setDiscreteInput(
149
- variable.address.toInt(),
150
- element.value.toBooleanFromBinary()
151
- )
152
- }
153
68
154
- AddressType .INPUT_REGISTER -> {
155
- memory.setInputRegister(variable.address.toInt(), element.value.toShort())
156
- }
157
- }
158
- }
159
- }
160
69
161
- private fun setHoldingRegisterInt16 (memory : PlcMemory , variable : Register , intValue : Short ) {
162
- memory.presetMultipleRegisters(
163
- mutableListOf<Pair <Int , Short >>(
164
- Pair <Int , Short >(
165
- variable.address.toInt(),
166
- intValue
167
- )
168
- )
169
- )
170
- }
171
70
172
- private fun setHoldingRegisterFloat32 (floatValue : kotlin.Float , memory : PlcMemory , variable : Register ) {
173
- val intValue = Float .floatToIntBits(floatValue)
174
- val lowWord = intValue and 0xFFFF
175
- val highWord = (intValue ushr 16 ) and 0xFFFF
176
- memory.presetMultipleRegisters(
177
- mutableListOf<Pair <Int , Short >>(
178
- Pair <Int , Short >(
179
- variable.address.toInt(),
180
- lowWord.toShort()
181
- ),
182
- Pair <Int , Short >(
183
- variable.address.toInt() + 1 ,
184
- highWord.toShort()
185
- ),
186
- )
187
- )
188
- }
189
71
190
72
}
0 commit comments