How do I read a changing XML File with a Simulink Level-2 S Function?
3 views (last 30 days)
Show older comments
I am trying to read out a XML File, that is updated through a script with a rate of 200ms (I can change this variable).
I modified an example file from the matlab exchange to read out my XML file, by repeatedly loading it.
function Outputs(block)
global MyStruct;
isstruct(MyStruct)
txt = char(block.InputPort(1).Data);
File = strtrim(txt.');
MyStruct = Xml2Struct(...
File);
Speed = MyStruct.Vehicle.Speed;
RPM = MyStruct.Engine.RPM;
FuelLevel = MyStruct.Vehicle.FuelLevel;
block.OutputPort(1).Data(1) = Speed;
block.OutputPort(1).Data(2) = RPM;
block.OutputPort(1).Data(3) = FuelLevel;
I connected the S-function block to display blocks to give out the doubles and set the stop time to inf.
The output works as long as I am manually changing and saving the XML file.
Starting the actual script, that overites the XML gives out this error:
java.io.FileNotFoundException: C:\users\*** (The process cannot access the file because it is being used by another process)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
What can I do to access and read the XML file with Simulink, while the script is running?
0 Comments
Answers (1)
Abhishek Chakram
on 9 Nov 2023
Hi Tuyen,
It appears to me that you are experiencing difficulties using a Simulink Level-2 S function to read a changing XML file. The error you are encountering indicates that the XML file is being accessed or locked by another process, preventing Simulink from reading it. One of the probable ways to solve this is to delay the XML File Access. Since your script updates the XML file every 200ms, you can introduce a “delay” block in the Simulink model to ensure that the file is not accessed while the script is updating it. You can set the sample time of your “S-function” block to a value greater than 200ms, such as 250ms or 500ms, to avoid conflicts.
Kindly refer the below documentation to know more about the delay block: https://www.mathworks.com/help/simulink/slref/delay.html
Best Regards,
Abhishek Chakram
0 Comments
See Also
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!