kvskrishna
Hi, 

Im currently working on a project where I need to connect the Open Plc to Matlab Simulink. Can you tell me if that can be possible or not? If so, How do I establish a connection. Data can be sent using the either TCP or UPD packets
Quote 0 0
thiagoralves
It is possible, but is not very straightforward. You can attach virtual sensors and actuators as UDP Blocks in the Simulink model. These blocks should be constantly sending data through some IP ports, and constantly listening for data at some other IP ports. You will have to use another program to communicate with the Simulink model over these IP ports, that is available here:  https://github.com/thiagoralves/OpenPLC_Simulink-Interface . The Simulink Interface program listens to the data coming from each virtual sensor, create an input packet with all this information and send it to OpenPLC through the Simulink I/O driver. Then, it must get the output packet with all output information from the OpenPLC and respond back with control data to each virtual actuator on the Simulink model. This way, the Simulink Interface program makes a bridge between OpenPLC and the virtual sensors and actuators in your model.

The Simulink Interface program relies on a configuration script to know some information about the virtual OpenPLC stations (it can talk to more than 1 OpenPLC instance) and the virtual sensors and actuators. The following example shows the configuration script for two OpenPLC stations, each one having one analog input and two digital outputs:

num_stations = "2"
simulink.ip = "192.168.171.1"

#Configuration for the first station
station0.ip = "192.168.171.101"
station0.add(analog_in) = "10001"
station0.add(digital_out) = "10002"
station0.add(digital_out) = "10003"

#Configuration for the second station
station1.ip = "192.168.171.102"
station1.add(analog_in) = "10004"
station1.add(digital_out) = "10005"
station1.add(digital_out) = "10006"

As it can be seen from the example above, the script defines the number of OpenPLC stations, the ip address for each station, including the machine running the Simulink model, and the sensors and actuators that should be connected to each OpenPLC station. The sensors are inputs to the OpenPLC, and they can be either digital or analog. On the other hand, the actuators are outputs from the OpenPLC, and can be as well either digital or analog. The number inside quotes after the sensor or actuator declaration denotes the IP port to which the device is connected to in the Simulink model.

The input and output ports added by this script are connected directly to the OpenPLC I/O buffers via the Simulink I/O driver. Therefore, at the end of the OpenPLC software installation, you will have to choose the Simulink driver for it to work. The Simulink driver adds inputs and outputs in the order they appear in the script, which means that, for instance, the first digital_out declaration will be connected to the first digital output of the OpenPLC, the second digital_out to the second digital output, and so on.
Quote 0 0
sachinsm
Hello,
Can you please provide instructions on how to use the files in   https://github.com/thiagoralves/OpenPLC_Simulink-Interface 
and If you also have a  example project on this topic can you please provide some details/content?
Thankyou
Quote 0 0
thiagoralves
https://github.com/thiagoralves/OpenPLC_Simulink-Interface/tree/master/Example
Quote 0 0
adriano.mele

Hi,

I tried to download the example, compiled SimLink using cygwin, moved the executable to the folder containing the simulink example (as specified in the readme) and started both OpenPLC Runtime and simulink (I loaded the hello world example in the Runtime). However, I guess that the Runtime should be configured properly in order to make the communication work (perhaps I need to set up a slave device? but I do not see any UDP slave)

Any hint?

Quote 1 0
thiagoralves
You need to select the Simulink hardware layer in the Hardware tab on the Runtime for it to work. You don't have to have simlink executable in the same folder as the simulink example. You just need to have it in the same folder where interface.cfg is. Launch OpenPLC, Simulink and simlink (preferably in that order) and check if it works. If it doesn't, post here the output of your simlink
Quote 1 0
adriano.mele
Hi Thiago, thanks for your answer. I compiled Simlink correctly, but I had assumed it was called implicitly by the runtime. Trying to run it, I get an error which says that he cannot find some cygwin files (in particular cygwin1.dll , cygstdc++-6.dll, cyggcc_s-seh-1.dll). I guess this is some path setting issue? Is there a way to instruct simlink on how to find those files, or does it need to be compiled in some specific folder?
Quote 0 0
thiagoralves
You need to open simlink inside Cygwin, otherwise it won’t work
Quote 0 0
adriano.mele
Ok, I tried as you suggested and indeed it seems to work. However, the simulink example still seems to struggle to communicate with OpenPLC. Here is Simlink's output
Quote 0 0
thiagoralves
You're using the wrong interface.cfg file. You must use the one from the /Example folder
Quote 0 0
adriano.mele
I had understood that the .cfg file had to be in the same folder of the simulink model, not in the simlink one. My mistake, I'll try again. Thanks for your support!
Quote 0 0
adriano.mele

I tried to put the simlink excutable in the same folder of the simulink file, but running it gives a strange result (as if it was reading a different .cfg). No idea where this is coming from, even though it reminds me of an example I prepared with factoryIO a while ago for the PID level control of a tank (but which I am not running right now of course). Any hint?

Just to be clear, I:

1. loaded and compiled the hello world program in the runtime

2. selected the simulink hw layer

3. launched the simulink model

4. navigated with cygwin to the simulink model folder, where I have the .cfg and where I compiled simlink

5. ran the simlink.exe from cygwin from inside that folder

 

Is all correct?

 

simlink2.png 

Quote 0 0
thiagoralves
It is correct. Ignore the terminal output for now, as it was made to debug a particular project. You should only worry about error messages on that terminal. From this, apparently it should be running fine.
Quote 0 0
adriano.mele
It seems to work now. Thanks Thiago!
Quote 0 0