Wednesday, August 17, 2016

S7 Siemens to Modbus TCP/IP


In this article I will try explain how to push data from Siemens S7 plc to Modbus device. For this purpose I will use Fenix software. To carry data between drivers we will use C# script.

We will have two remote devices:
Siemens PLC [S7Plc] -> 192.168.142.134:102
Modbus Device [ModPlc] -> 192.168.142.134:502

Issue is take data from S7Plc and push to ModPlc. Assigning addresses are shown below:
Var1 DB1.DBD0  -> HoldingRegister [40001]
Var2 DB1.DBD4  -> HoldingRegister [40003]
Var3 DB1.DBD8  -> HoldingRegister [40005]
Var4 DB1.DBD12 -> HoldingRegister [40007]

Now we will create project in Fenix. Start fenix and create new project. Picture below:


Next step is to add two connections to project:


Then we will create device and tags for S7 communication:

We will do the same for Modbus communication:


When we finished you will get something similar like picture below:


When project is created we will set all important parameters. We will create script. Script will be invoked every 50 ms. We must set time default timer for script. 

1. Click on Scripts on Solution.
2. Go to Properties. Click on filed Timers
3. CustomerTimer window will appear. Change default time from 1500 to 50 ms the click OK.

Now add Timer to script to do this:

1. Click on Script.cs on Solution
2. Go to Properties and from ComboBox 03 Script choose Timer.


Now we can write script. Open script in editor and add code:


Code:
Script have 3 methotds. Start() is invoking during start of driver. Stop() is invoking during driver stop. Cycle() is invoking by Timer every 50ms. Script have access to all Tags in project and for that you can use functions:
SetTag("TagName", Value);
GetTag("TagName");

using System;
using ProjectDataLib;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.IO;

public class Script : ScriptModel
{

    public override void Start()
    {
    }

    public override void Stop()
    {
    }
    public override void Cycle()
    {
        var Var0 = GetTag("S7Var0");
        SetTag("ModVar0",Var0);

        var Var1 = GetTag("S7Var1");
        SetTag("ModVar1",Var1);
           
        var Var2 = GetTag("S7Var2");
        SetTag("ModVar1",Var1);
  
        var Var3 = GetTag("S7Var3");
        SetTag("ModVar3",Var3);
    }
}

And Start communication:

1. Click on Solution on Example1
2. Click on TableView
3. Click to start all communication drivers.


No comments:

Post a Comment