Hello Pfeifferle,
I tried to design a simple example that uses TCP sockets to exchange data between the simulation and external programs. In this example a quite simple manufacturing plant with two conveyors and one workpiece is controlled by an external control algorithm written in C#.
The control application is implemented as a TCP server to which the simulation can connect to during runtime. Once the connection is established the simulation sends information (in this case sensor signals about the position of the workpiece) to the server which calculates new output signals for the actuators in the simulation. The output signals are send back to the simulation via the TCP connection.
While the server is performing the control algorithm the simulation is waiting for the response of the server, which means simulation time is not increasing and there's no need for a realtime option.
A trace of the basic communication act is attached to this post.
The socket communication part for Modelica is written in c and consists of one c-file and its corresponding header file. I made five functions that encapsulate the functionality for different steps in the communication process. The basic:
- If you want to use sockets in c you first have to startup the network library of windows which is done by startUP().
- Before you can interchange data you have to create the TCP socket which is done by createSocket(IPAddress, port) where IPAddress is as string with the destination IP-Address and port is an int eger with the destination port.
- For sending a message (which is a string) you call sendMessage (message).
- For receiving a message (which is a string) you call message=receiveMessage()
- When you are finished with communication you shut down the network library by cleanUP()
In Modelica you have to write wrapper functions to use the c functions. In the attached Modelica model you will find these in the package Socket.Communication. The wrapper functions have the same name like the c functions and have the same parameters.
The Model Socket.Communication.Socket represents the TCP socket which is created in the initial simulation step.
To exchange dynamic messages (messages with varying content) one needs to define additional functions to build the message strings since in Modelica non-constant strings can not be handeled outside functions. These functions are situated in the package Socket.Control. They are not situated in Socket.Communication since they are problem specific to the simulation task.
The Model Socket.Control.ExternalConveyorControl manages the actual communication between the simulation and the external conveyor controller. Since the simulation would slow down extremly if in each simulation step communication over the socket would be performed, a communication act is triggered by a sample instruction inside the Socket.Control.ExternalConveyorControl model.
The TCP serves is written in C#. There are only two classes (Program and ConveyorControl).
The method main in Program is the main entry point. Here a TCP-Server socket is created that waits for incoming connection requests. Once a connection request arrives a new Thread of class ConveyorControl is created which manages the communication and the control task. The control task is described by control which is the entry point of the thread.
I tested the simulation and the external control using Dymola 6.0d with Visual Studio 2005 compiler. To run the external C# program you need the .NET Framework V2.x by Microsoft. Since I'm not using OpenModelica I don't know if it works using OpenModelica. But in principle it should be possible with some minor adjustments. On my PC it was necessary to manually copy the wsock32.lib and msvcrt.lib to the directory of the Modelica model. Of course one also needs the homebrew socket Library (socket.lib and its header file socket.h) attached to this post to use its functionality in the directory as well.
I hope you can get it running.
Regards,
Florian
Click to view attachmentClick to view attachmentClick to view attachmentClick to view attachment[attachmenti
d=23]
Click to view attachment