Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Linux System Function using C on OpenModelica

Linux System Function using C on OpenModelica

Hi,

I am interested in executing a executable C file on my Linux Machine from inside a modelica code.

I used Modelica.Utilities.System.command function to pass the command string to execute the executable file. The Simulation was completed but the file wasn't executed. The file changes a few values in a shared memory so I can cross check if the file was executed.

I am able to run the executable directly from the shell using : /home/<path_to_file>/test (test is the executable generated by gcc).

But when I implement the same using :

model SystemFunctionTest
  Real returnValue = Modelica.Utilities.System.command("/home/<path_to_file>/test");
end SystemFunctionTest;

The value of the variable in the shared memory isn't changed telling me that the executable "test" wasn't executed.

I am using a Ubuntu 16.04 32-bit machine with 1.11.0~dev-153-g45ed062 32-bit OMEdit.

Can someone guide me to solve this?

Thank you.

Re: Linux System Function using C on OpenModelica

Seems to be working fine for me (OpenModelica 1.11.0~dev-321-g5aa0206 Ubuntu 64-bit):

Code:

model SystemFunctionTest

  Real returnValue = Modelica.Utilities.System.command("/home/marsj/tmp/test");
end SystemFunctionTest;

marsj@Precision-5510:~/tmp$ cat test
#!/bin/sh

echo "ABC DEF"

marsj@Precision-5510:~/tmp$ ls -lh test
-rwxr-xr-x 1 marsj marsj 26 nov  4 08:12 test

$ ./SystemFunctionTest -output returnValue -override stepSize=0.5 -s euler
ABC DEF
ABC DEF
ABC DEF
ABC DEF
ABC DEF
time=1,returnValue=0

Make sure to check the return code from the system call.

Re: Linux System Function using C on OpenModelica

@sjoelund.se

What do you mean when you say "check the return code from the system call" ? could you please elaborate?

$ ./SystemFunctionTest -output returnValue -override stepSize=0.5 -s euler  - What is happening here?

Also Is there a way I can see the output from a printf/puts C command  written inside an C external function?

Re: Linux System Function using C on OpenModelica

Call ModelicaMessage instead of printf (printf is displayed if you use a terminal, but not OMEdit).

You can either print the return code or make assertions, etc. To make sure that the exit code is 0.

See the simulation flags documentation for an explanation of what is done (just printing the final values of the calls to see the exit status; you can probably use the plotting functionality of OMEdit): https://openmodelica.org/doc/OpenModeli … tion-flags

Re: Linux System Function using C on OpenModelica

Oh alright! I'll check that.
Where did you see the output of the echo command when you execute the C file from OMEdit?

Thank you from your quick replies current/smile

Re: Linux System Function using C on OpenModelica

I don't use OMEdit current/smile

Re: Linux System Function using C on OpenModelica

Oh! current/yikes
Do I need to use OMShell to see the output or is there another way?

Re: Linux System Function using C on OpenModelica

You can use OMShell, OMNotebook, mos-script or like me "omc -s" followed by make and running the executable yourself.

Re: Linux System Function using C on OpenModelica

Oh that's great! I'll try these out current/smile!

Thank you!

Re: Linux System Function using C on OpenModelica

Hi,

I tried the OMShell method I think I know where the problem lies. A file used to write in the shared memory used by need needs to access a few specific Environment path variables that direct to a few files(We are working with an existing system and we are integrating Modelica with it).

Is it possible that the omc isn't able to access these? Is there a way to check all the Environment Variables omc is using?

I don't face this problem with execution from Linux Shell probably because it has access to the predefined Path from the Environment Variables.

Re: Linux System Function using C on OpenModelica

If I directly use
>>>system ("echo 'abc' ")

in OMShell,where can I see the output of this command?

The User guide says:

system(str) Execute str as a system(shell) command in the operating system; return integer success value. Output
into stdout from a shell command is put into the console window.

But I don't see any output on the OMShell window.

Re: Linux System Function using C on OpenModelica

It's output to the console, which in OMShell's case is redirected to a log-file...

You can check env.vars using: https://openmodelica.org/doc/OpenModeli … ronmentvar

If you open OMEdit from a console window the env.vars might be preserved. It depends on where you set them as well (bashrc / /etc/profile, etc)

Re: Linux System Function using C on OpenModelica

Alright, I'll check the log file.

We've set the Environment variables in the bashrc file.

And opening OMEdit from a bash window solved the problem. We were able to access the ENV Varibles and the Shared memory was updated!
Thank you current/big_smile

Edited by: jalpanchal - Nov-07-16 03:41:31
There are 0 guests and 0 other users also viewing this topic
You are here: