IP-S7-Link .Net Advanced Code Snippets

 Traeger Industry Components GmbH

© by TIS

Use one of the different PLC device providers by instantiating an instance of the appropriate PlcDevice class derivates.

PlcDevice device = new SiemensDevice(new IPDeviceEndPoint("192.168.0.80"));

After an instance of (for e.g.) the SiemensDevice class has been created just use the properties provided by the class to setup the appropriate device metadata.

SiemensDevice device = new SiemensDevice(new IPDeviceEndPoint("192.168.0.80"));
 
device.Type = SiemensDeviceType.S71200;
device.ChannelType = SiemensChannelType.OperationPanel;

In the most scenarios its already enough to create a device using the appropriate device type in the constructor of the device, because the default channel does mostly match the common needs.

SiemensDevice device = new SiemensDevice(new IPDeviceEndPoint("192.168.0.80"), SiemensDeviceType.S71200);


The framework does provide the ability to define different types of end points through the PlcDeviceEndPoint class. The most common end point implementation to use is the IPDeviceEndPoint class. Using this class it is possible to specify the IP address and optionally the rack and slot number of the PLC.

PlcDeviceEndPoint endPoint = new IPDeviceEndPoint("192.168.0.80");
 
// Alternatively also specify the rack.
PlcDeviceEndPoint endPointWithRack = new IPDeviceEndPoint("192.168.0.80", 0);
 
// Alternatively also specify the rack and slot.
PlcDeviceEndPoint endPointWithRackSlot = new IPDeviceEndPoint("192.168.0.80", 0, 2);


After creating an instance of one of the PlcDevice class derivates just create a new connection associated with the device represented. Creating an instance using this factory method will then return the device provider dependent implementation of the PlcDeviceConnection class.

PlcDevice device = new SiemensDevice(new IPDeviceEndPoint("192.168.0.80"));
PlcDeviceConnection connection = device.CreateConnection();

To retrieve the current connectivity state of the PlcDeviceConnection class instance use the State property to get the according PlcDeviceConnectionState enumeration member.

if (connection.State == PlcDeviceConnectionState.Connected) {
    // ...
}

To handle the state transitions use one or more of the state specific events of the PlcDeviceConnection class instance.

connection.Connected += Program.HandleConnectionConnected;

To handle the event declare the matching event handler as follows:

private static void HandleConnectionConnected(object sender, EventArgs e)
{
    Console.WriteLine("Connection established!");
}


To read a single value use one of the Read methods of the PlcDeviceConnection class.

int value = connection.ReadInt32("DB1.DBD 1");

To read an array of values use the additional read overloads to specify the number of items to read as an array as follows:

int[] values = connection.ReadInt32("DB1.DBD 1", 3);

To read a single value using an derivate of the PlcValue<T> class use one of the ReadValue overloads of the PlcDeviceConnection class as follows.

PlcInt32 value = new PlcInt32("DB1.DBD 1");
int valueData = connection.ReadValue(value);

To read an array of values using an derivate of the PlcArray<T, TElement> class use one of the ReadValue overloads as follows:

PlcInt32Array values = new PlcInt32Array("DB1.DBD 1", 3);
int[] values = connection.ReadValue(values);

To read multiple values at once using derivates of the PlcValue<T> class use one of the ReadValues overloads of the PlcDeviceConnection class as follows.

PlcInt32 value1 = new PlcInt32("DB1.DBD 1");
PlcInt32 value2 = new PlcInt32("DB2.DBD 1");
PlcInt32 value3 = new PlcInt32("DB3.DBD 1");
 
int[] values = connection.ReadValues(value1, value2, value3);

To read multiple arrays of values at once using derivates of the PlcArray<T, TElement> class use one of the ReadValues overloads of the PlcDeviceConnection class as follows.

PlcInt32Array values1 = new PlcInt32Array("DB1.DBD 1", 3);
PlcInt32Array values2 = new PlcInt32Array("DB2.DBD 1", 4);
PlcInt32Array values3 = new PlcInt32Array("DB3.DBD 1", 5);
 
int[][] values = connection.ReadValues(values1, values2, values3);

To read multiple derivates of the PlcValue<T> class or of the PlcArray<T, TElement> class use one of the ReadValues overloads of the PlcDeviceConnection class as follows.

PlcInt32 value1 = new PlcInt32("DB1.DBD 1");
PlcInt32Array values2 = new PlcInt32Array("DB2.DBD 1", 4);
PlcBoolean value3 = new PlcBoolean("DB3.DBX 1.0");
PlcBooleanArray values4 = new PlcBooleanArray("DB4.DBX 1.0", 5);
PlcString value5 = new PlcString("DB5.DBB 1", 32);
 
object[] values = connection.ReadValues(value1, values2, value3, values4, value5);

To read a custom derivate of the PlcObject class use the Read<T> method as follows:

MachineData data = connection.Read<MachineData>();

The MachineData class could look like as follows:

public class MachineData : PlcObject
{
    [PlcMember("DB1.DBX 100.0", Length = 7)]
    private bool[] toolConfigurations;
 
 
 
    public MachineData()
        : base()
    {
    }
 
 
 
    [PlcMember("DB1.DBB 120")]
    public DateTime EstimatedFinishDate { get; set; }
 
    [PlcMember("DB1.DBB 1", Length = 16)]
    public string JobNumber { get; set; }
 
    [PlcMember("DB1.DBD 100")]
    public int Speed { get; set; }
 
    [PlcMember("DB1.DBW 50")]
    public float Temperature { get; set; }
 
    [PlcMember("DB1.DBX 100.0")]
    public bool UseCuttingTool { get; set; }
 
 
 
    public bool IsToolConfigured(int toolIndex)
    {
        return this.toolConfigurations[toolIndex];
    }
}


To write a single value use one of the Write methods of the PlcDeviceConnection class.

connection.WriteInt32("DB1.DBD 1", 123);

To write an array of values use the additional write overloads to specify an array of items to write as follows:

connection.WriteInt32("DB1.DBD 1", 123, 456, 789);

To write a single value using an derivate of the PlcValue<T> class use one of the WriteValue overloads of the PlcDeviceConnection class as follows.

PlcInt32 value = new PlcInt32("DB1.DBD 1", 123);
connection.WriteValue(value);

To write an array of values using an derivate of the PlcArray<T, TElement> class use one of the WriteValue overloads as follows:

PlcInt32Array values = new PlcInt32Array("DB1.DBD 1", 123, 456, 789);
connection.WriteValue(values);

To write multiple values at once using derivates of the PlcValue<T> class use one of the WriteValues overloads of the PlcDeviceConnection class as follows.

PlcInt32 value1 = new PlcInt32("DB1.DBD 1", 123);
PlcInt32 value2 = new PlcInt32("DB2.DBD 1", 456);
PlcInt32 value3 = new PlcInt32("DB3.DBD 1", 789);
 
connection.WriteValues(value1, value2, value3);

To write multiple arrays of values at once using derivates of the PlcArray<T, TElement> class use one of the WriteValues overloads of the PlcDeviceConnection class as follows.

PlcInt32Array values1 = new PlcInt32Array("DB1.DBD 1", 1, 2, 3);
PlcInt32Array values2 = new PlcInt32Array("DB2.DBD 1", 4, 5, 6);
PlcInt32Array values3 = new PlcInt32Array("DB3.DBD 1", 7, 8, 9);
 
connection.WriteValues(values1, values2, values3);

To write multiple derivates of the PlcValue<T> class or of the PlcArray<T, TElement> class use one of the WriteValues overloads of the PlcDeviceConnection class as follows.

PlcInt32 value1 = new PlcInt32("DB1.DBD 1", 123);
PlcInt32Array values2 = new PlcInt32Array("DB2.DBD 1", 4, 5, 6);
PlcBoolean value3 = new PlcBoolean("DB3.DBX 1.0", true);
PlcBooleanArray values4 = new PlcBooleanArray("DB4.DBX 1.0", true, false, true, false, true);
PlcString value5 = new PlcString("DB5.DBB 1", "This is my string value.");
 
connection.WriteValues(value1, values2, value3, values4, value5);

To write a custom derivate of the PlcObject class use the Write<T> method as follows:

MillJob job = new MillJob("MJ:100012");
job.Input = 1;
job.RotationSpeed = 3500;
job.ToolDiameter = 12.8;
job.Output = 3;
 
connection.Write(job);

The MillJob class could look like as follows:

public class MillJob : PlcObject
{
    public MillJob(string number)
        : base()
    {
        this.Number = number;
    }
 
 
 
    [PlcMember("DB1.DBD 20")]
    public int Input { get; set; }
 
    [PlcMember("DB1.DBB 1")]
    public string Number { get; private set; }
 
    [PlcMember("DB1.DBD 25")]
    public int Output { get; set; }
 
    [PlcMember("DB1.DBD 30")]
    public int RotationSpeed { get; set; }
 
    [PlcMember("DB1.DBW 40")]
    public float ToolDiameter { get; set; }
}