@@ -17,25 +17,45 @@ import kotlin.system.exitProcess
17
17
description = [" Modbus TCP Simulator" ])
18
18
class Checksum : Callable <Int > {
19
19
20
- @Option(names = [" -f" , " --file" ], description = [" configuration file (JSON)" ])
21
- lateinit var file: File
20
+ @Option(names = [" -f" , " --file" ], description = [" custom simulation configuration file (JSON)" ])
21
+ var file = " "
22
22
23
23
@Option(names = [" -p" , " --port" ], description = [" server TCP port" ])
24
24
var port = " 502"
25
25
26
+ @Option(names = [" -sr" , " --simulation-random" ], description = [" random number simulation" ])
27
+ var simulationRandomValues = false
28
+
29
+
26
30
27
31
private lateinit var plcMemory: PlcMemory
28
32
private lateinit var plcSimulation: PlcSimulation
33
+ private lateinit var modbusServer: ModbusServer
29
34
30
35
override fun call (): Int {
31
36
val mainCoroutineScope = CoroutineScope (Dispatchers .Default )
32
37
val configuration = ConfigurationParser ()
33
- plcMemory = PlcMemory (configuration)
34
- plcSimulation = PlcSimulation (configuration, plcMemory, mainCoroutineScope)
38
+
39
+ if (simulationRandomValues && file.isNotEmpty()){
40
+ println (" -f and -sr cannot be mixed, one of the simulations must be chosen" )
41
+ return - 1
42
+ }
35
43
// val fileContents = Files.readAllBytes(file.toPath())
36
44
// val digest = MessageDigest.getInstance(port).digest(fileContents)
37
45
// println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
38
- var modbusServer = ModbusServer (plcMemory)
46
+ if (simulationRandomValues) {
47
+ modbusServer = ModbusServer (plcMemory)
48
+ }else {
49
+ if (file.isNotEmpty()) {
50
+ // -f file based (Custom simulation)
51
+ configuration.setFileName(file)
52
+ }
53
+ // if not set default, reading internal xml
54
+ plcMemory = PlcMemory (configuration)
55
+ plcSimulation = PlcSimulation (configuration, plcMemory, mainCoroutineScope)
56
+ modbusServer = ModbusServer (plcMemory)
57
+ }
58
+
39
59
try {
40
60
modbusServer.start()
41
61
modbusServer.block()
0 commit comments