I am modifying a .txt file with the following code in PlantSimulation at the end of the simulation:
var file :object var file2 :object var i : integer var r : real file:=fileOut /*file2:=RandomSeed file2.remove*/ /*for i:=1 to OutData.ydim r:=str_to_time(OutData[1,i]) file2.writeLn(R) next*/ r:=str_to_time(OutData[2,1]) file.writeln(R) r:=str_to_time(OutData[4,1]) file.writeLn(R) r:=str_to_time(OutData[3,1]) file.writeLn(R) r:=str_to_time(OutData[4,1]) file.writeLn(R) file.close
This works properly, however when I combine it with the .NET api (that I use on Matlab) to control when the file is modified it gets stuck and do not see the change of the file. The .NET code that I use is the following:
watcher = system.IO.FileSystemWatcher; watcher.Path = 'C:\somewhere\somefolder'; watcher.Filter = 'OutData.txt'; watcher.NotifyFilter = System.IO.NotifyFilters.LastWrite; watcher.WaitForChanged(System.IO.WatcherChangeTypes.Changed);
However this code works properly and not relevant for that. I just want to explain what happens. This code basically control my OutData.txt (linked to fileOut), blocking the rest of the program until this file is modified. I do that because I need PlantSimulation to modify some .txt that will be then analysed with Matlab. As said, this Matlab code works fine in any other situation and thus the problem is not here.
My idea is that how the file is modified in Plant Simulation is the reason of why the change is not revealed and so how can I fix that?
To do the same thing I have looked at the .COM interface event SimulationFinished, but this tells when the simulation is finished and not also when all the methods of endsim have finished to work. Any other option would be helpful.
Solved! Go to Solution.
We are opening a file with the following C function "_wfsopen(<file path>, "a+", _SH_DENYWR)".
The flag in the last parameter prohibits that any other process can write to the file.
Perhaps the call 'watcher.WaitForChanged' needs write access for watching the file status.
Can you write the output to a temporary file and then copy it to the wanted destination?
You can use the system function: system("copy <temp file> OutData.txt")
I'm not sure to understand when I have to do this copy. Do you mean when the OutData.txt is overwritten by Plant Simulation?
However, the "watcher.WaitForChanged" don't require write access to detect the change.
The idea is that in Plant Simulation you first write to a temporary file and the copy this temporary file to the final file named OutData.txt.