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

Using external libraries to impletement communication base on socket

Using external libraries to impletement communication base on socket

Hi all,
Sorry for my poor english.
I am new to Modelica & Openmodelica. I want to use my c++ program to communicate with modelica program during simulation runtime on windows. I write a socket program which includes a .c file and a.h file and generate a .o file using gcc. Then i call the functions writen in .c file in the modelica program. But there are some errors when i do the simulation.
My problem is: how can i call socket function writen by c using external c?
Can some one help me on this problem?
Thanks & Regards

Re: Using external libraries to impletement communication base on socket

It's hard to tell without seeing some code or error messages, but here goes:

function myExt
  ....
external "C" ... = myExt(...) annotation(Library = "myExt.o"); // Note the required .o, not present in Dymola... We really should fix this
end myExt;

model A
  TypeA myType = myExt(...);
end A;

myExt.cpp:

... C++ code ...

extern "C" { /* Export the function as if it was a C function */
... myExt (...) {
  ...
}
}

Re: Using external libraries to impletement communication base on socket

Thanks for your reply. I tried the simple example using external libraries according to openmodelica user guide and that was successful. Now i try to using modelica program call the external socket function, and there are many problem about it. i post the related file in attachment, and then i will describe my mind in detail.
1.i write a .c file which contains a simple socket function called startUP(). the .c file called my_start.c and it includes <winsock2.h> because it uses socket.
2.i write a .h file called my_start.h includs the definition of the function startUP.In this function i print a sentence to see if the modelica model calls the function.
3.generate a .o file by command : gcc -c -o my_start.o my_start.c
4.write a modelica model which calls the external function.
5.write a .mos file to run the modelica model.
But it didn't print the sentence which i write in the c function. Does it call the function? How can it work well? Thanks.
All the files are in the .zip file in attaachments below.
TEXT

Re: Using external libraries to impletement communication base on socket

What program are you using to call the mos-file? If you're running OMShell, I don't think we redirect stdout, so you won't see that printf statement.

Add a getErrorString() after the simulation and loadFile() commands.


model mysocket
m_startup( );
end mysocket;

Is invalid syntax. getErrorString() after loadFile() should catch this.

model mysocket
equation
when initial()
  m_startup( );
end when;
end mysocket;

Would probably be better. Or possibly encapsulating the socket in an external object.

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