- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » External function with state
External function with state
External function with state
Hello everyone,
I'm planning making a server application that will communicate with a client that is an external function. The communication will be sockets or shared memory. If possible I'd like to make the client stateful so the connection doesn't have to be re-made each time the external function is called. Is there a some way to do this or should I just try making the connection on every external function evaluation?
Adam
- adamLange
- 29 Posts
Re: External function with state
Save the state using an external object (see the specification on how to do this)
- sjoelund.se
- 1700 Posts
Re: External function with state
Hi, I have used external functions for serial communication.
Below is code to create serial object
Code:
class Serial
extends ExternalObject;
encapsulated function constructor
input Integer PortNum;
output SerialPort Sport;
external "c" Sport=openport(PortNum);
annotation (Library="cfile2");
end constructor;
encapsulated function destructor
input SerialPort Sport;
output Integer i;
external "c" i=closeport(Sport);
annotation (Library="cfile2");
end destructor;
end Serial;
You can use above object as
Code:
model PIcontroller
import Serial;
Serial sp = Serial(3); //opens serial port 3 and keeps it open
equation
.
.
end PIcontroller;
Hope this helps.
- swaroop.katta
- 23 Posts
Re: External function with state
Thank you sjoelund.se and swaroop.katta!
I read the chapter in the modelica specification about external classes. For an example case I'm making a python server that returns the mean of two numbers and a model that calls the server to get the average. I've got everything roughly implemented. The communication is handled with a unix socket. I'm still working out a few bugs and then need to get rid of some of the hardcoded paths (seeing how arguments are passed for the serial example will help with this...). Its going pretty well overall.
Adam
- adamLange
- 29 Posts
Re: External function with state
Here's the example in case you'd like to check it out:
https://github.com/adamLange/modelicaSocketExample
- adamLange
- 29 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » External function with state