eightbitsandwich
I'm wondering if there is any document that shows how to get started with how to model simulink and have it connect to OpenPLC (on a PC). 

Also, is Easy Modbus TCP still a preferable HMI interface? I'm hoping to build an HMI for both modbos and DNP3 devices.
Quote 0 0
thiagoralves
Unfortunately there isn't any documentation on that. Since my University wants to patent the technology that connects OpenPLC and Simulink together, there may never be an official document describing how to do it, but rather an official software that does that, but you may have to license it to use.

However, while it hasn't happened yet, you still can use SimLink to link both OpenPLC and Simulink models:  https://github.com/thiagoralves/SimLink

You model on Simulink needs to use UDP Send and Receive blocks to communicate with OpenPLC instances. Each UDP block is responsible for a single variable that OpenPLC is reading or writing. You must configure your receiving blocks on Simulink to have a 2 byte receive buffer, 1 byte maximum message length, and use uint16 as data type for message. The sample time can be changed at your discretion.

On the OpenPLC side, obviously you will have to use the Simulink driver on OpenPLC. The SimLink software should be running alongside OpenPLC and also must be configured using the interface.cfg file. Instructions on how to configure it are on the file itself.

About HMI, the official OpenPLC HMI is ScadaBR: http://www.openplcproject.com/scada
There are instructions on the forum on how to install ScadaBR on Windows (https://openplc.discussion.community/post/big-update-with-new-cool-features-8173271?pid=1292945147). Installing it on Linux is a little bit trickier, so there aren't instructions available for that yet. ScadaBR should work with both Modbus and DNP3, and it is really easy to use. You can create your HMIs in a snap, without writing any line of code. Also, the HMIs are web-based so they can run on any web browser.
Quote 0 0
eightbitsandwich
When setting up SimLink does it matter if i use localhost for both OpenPLC and SimLink? That is, can my IP be 127.0.0.1 in the interface.cfg?
Quote 0 0
thiagoralves
Yes, you can even write localhost on the config file, and it will work properly. Just keep in mind that on the simulink field you must write the IP address of the computer running the simulation model on Simulink (and it also can be localhost if you are running everything on the same computer)
Quote 0 0
epgodoy
Hello Thiago, that is very interesting. Correct me if I am wrong but for what I understood the SimLink would be a Software-In-the-Loop (SIL) solution allowing reading and writing I/O data from Simulink models to the OpenPLC server. Am I right?
Quote 0 0
thiagoralves
You are exactly right. Additionally, SimLink also allows you to link a single Simulink model to multiple OpenPLC instances, in case you are working on a complex model that require multiple PLCs 
Quote 0 0
epgodoy
That is great! Thanks.
Quote 0 0
rodrigo_rolle
Hello, Thiago. I just got the SimLink files on GitHub, do you suggest a specific compiler to compile the .cpp file? Can I just generate an .exe file (with CodeBlocks, for instance) that I'll run alongside with OpenPLC or do I have to run it in a different way?
Quote 0 0
thiagoralves
You'll need to use GNU GCC to compile it. It relies on sockets and POSIX threads to work, so if you try any windows compiler with it, it will fail. On Windows the best approach is compiling under Cygwin:

g++ simlink.cpp -o simlink -pthread
Quote 0 0
rodrigo_rolle
Thanks, I could compile it successfully, but let me ask another question. I'm trying to run that OpenPLC "hello world" example, using OpenPLC, Simlink and a Simulink application on the same computer. I start OpenPLC normally, but when I start SimLink, it shows random data (analog and digital) for 5 stations (values for pump, tank, valve, pressure) even after I changed the interface.cfg file in order to use only station0 and just one digital input and one digital output. My interface.cfg file is:

num_stations = "1"
comm_delay = "100"

# ------------
#   SIMULINK
# ------------
simulink.ip = "localhost"

# ------------
#  STATION 0
# ------------
station0.ip = "localhost"
station0.add(digital_in) = "10001"
station0.add(digital_out) = "10002"


All other lines are commented. I also tried to change the main code (simlink.cpp), by commenting all the printf lines of the main loop that don't refer to station0 but it also didn't work and I still can't get appropriate data for the digital in and out that I want to use. I changed the main loop to:

while(1)
    {
        pthread_mutex_lock(&bufferLock);
        printf("Station 1\nDigIn: %d\tDigOut: %d\n", stations_data[0].digitalIn[0], stations_data[0].digitalOut[0]);
        //printf("Station 2\nPressure: %d\t\t\tPump: %d\t\tValve: %d\n", stations_data[1].analogIn[0], stations_data[1].digitalOut[0], stations_data[1].digitalOut[1]);
        //printf("Station 3\nPressure: %d\t\t\tPump: %d\t\tValve: %d\n", stations_data[2].analogIn[0], stations_data[2].digitalOut[0], stations_data[2].digitalOut[1]);
        //printf("Station 4\nPressure: %d\t\t\tPump: %d\t\tValve: %d\n", stations_data[3].analogIn[0], stations_data[3].digitalOut[0], stations_data[3].digitalOut[1]);
        //printf("Station 5\nPressure: %d\tTank: %d\t\tPump: %d\t\tValve: %d\n\n", stations_data[4].analogIn[0], stations_data[4].analogIn[1], stations_data[4].digitalOut[0], stations_data[4].digitalOut[1]);
        pthread_mutex_unlock(&bufferLock);

        sleep_ms(3000);
    }

Any suggestions to solve this problem?
Quote 0 0
thiagoralves
I'm sorry, these printf lines are just specific debug messages for a project I was working on, they won't adapt to your configuration, unless you change them. I should have let them commented out on the code.

If you configured everything correctly, you should be receiving digital out information on your Simulink model on port 10002
Quote 0 0
thiagoralves
I created a simple Simulink model for the Hello World program. It will serve as a template for your projects. Remember that for every variable in your PLC program you will need a UDP Send or UDP Receive block. Those blocks must be configured exactly like the ones I'm providing you on the template. Here is a screenshot of the model running on Simulink 2017:
Capture.PNG 

I'm also attaching on the zip file a simplified version of the model compatible with Simulink 2012b, in case you don't have the most recent version of Simulink installed. This simplified version doesn't have dashboard elements.

If you run into communication problems, try restarting SimLink while the model is already running. The best approach is to have everything running (OpenPLC and Simulink model) prior to starting SimLink.
Quote 0 0
eightbitsandwich
(sorry for the late reply)

I am attempting to set this up using SimuLink running in windows and OpenPLC running in a ubuntu VM. 

Using wireshark I can see traffic going from Simulink to my VM but I am not sending any data back to Simulink from the VM. 

The configuration is as follows:

simulink.ip = "192.168.1.11"
station0.ip = "192.168.175.133"
station0.add(digital_in) = "10001"
station0.add(digital_out) = "10002"


I am using OpenPLC V2 and SimuLink R2017b.

Question: My VM sees the source IP of the incoming UDP packet as a different IP then the one listed in my configuration file. Basically, 192.168.1.11 ends up routing through 192.168.175.1. If OpenPLC sees this incoming data from a different IP will it ignore it?

I've done some packet captures and changed the source IP and replayed them but I haven't had much luck using this technique either. 

Thanks,

Jorge
Quote 0 0
thiagoralves
SimLink creates both a server and a client to connect OpenPLC and Simulink together. That's why you need the IP addresses for both the PLC and Simulink stations. From your description, it seems that you're trying to reach the host, which is running Simulink, using a different subnet. Your VM software usually creates many virtual networks, and depending on the configuration of your client VM, if you use the wrong network to communicate with the host, all you get is a dead end.

If your station's IP is on the 175 network (192.168.175.133), you should try to reach your host on the same 175 network, and not on the 1. Try changing the simulink.ip on the config file to "192.168.175.1". The best way to make sure that the ips are correct is to ping both machines from both sides. If you can ping, it means that you can talk. If you can't ping, there might be a chance that you're trying to reach the machine using the wrong network.
Quote 0 0
eightbitsandwich
Going from Windows (Simulink) to my VM (ubuntu / OpenPLC) I can ping and I can capture UDP data:
08:07:41.277020 IP 192.168.175.1.58624 > 192.168.175.133.10001: UDP, length 8
0x0000: 4500 0024 5fc5 0000 8011 fb2b c0a8 af01 E..$_......+....
0x0010: c0a8 af85 e500 2711 0010 23a4 0000 0000 ......'...#.....
0x0020: 0000 f03f 0000 0000 0000 0000 0000 ...?..........


This shows traffic originating from 192.168.1.11 (Simulink) and being received at the VM. The routing through the VM virtual NIC shows the source IP as 192.168.175.1 although it started at 192.168.1.11

Going from my VM (ubuntu / OpenPLC) I cannot see any traffic originating from the OpenPLC server, but I can ping and I can send data manually to the Simulink Server to activate the 'start' and 'stop' bits.

$echo -n -e '\x00''\x00' | nc -u 192.168.1.11 10002

08:17:08.509296 IP 192.168.175.133.47574 > 192.168.1.11.10002: UDP, length 2
0x0000: 4500 001e 0a74 4000 4011 fe79 c0a8 af85 E....t@.@..y....
0x0010: c0a8 010b b9d6 2712 000a 31fd 0000 ......'...1...


So, basically I can confirm that data is flowing between Windows and the VM, and that Simulink is sending data to the OpenPLC server. Also, upon button press the data field is populated with "00 00 00 00 00 F0 3F" and when idle it is "00 00 00 00 00 00 00 00", I am assuming this is normal operation.

Is there a way to troubleshoot the OpenPLC server? Does it generate logs on data received? I'm not sure why it is not responding / not attempting to send data back to the Simulink serveer.
Quote 0 0