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

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

Re: External function with state

Save the state using an external object (see the specification on how to do this)

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.

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


Re: External function with state

Here's the example in case you'd like to check it out:

https://github.com/adamLange/modelicaSocketExample

There are 0 guests and 0 other users also viewing this topic
You are here: