Übertragen von Strukturen an die SPS

Download

Voraussetzungen

Sprache / IDE

Beispielprogram auspacken

C# / Visual Studio

Sample02.zip

Aufgabe

Von einer .NET Applikation soll eine Struktur in die SPS geschrieben werden. Die Elemente der Struktur haben verschiedene Datentypen.

Beschreibung

Die Struktur, die in die SPS geschrieben werden soll:


TYPE PLCStruct
STRUCT
intVal : INT; (*Offset 0*)
dintVal : DINT; (*Offset 2*)
byteVal : SINT; (*Offset 6*)
lrealVal : LREAL;(*Offset 7*)
realVal : REAL; (*Offset 15 --> Gesamtgrösse 19 Bytes*)
END_STRUCT
END_TYPE

In C# würde die Struktur wie folgt aussehen:


public struct PLCStruct
{
public short intVal;
public int dintVal;
public byte byteVal;
public double lrealVal;
public float realVal;
}

Anstatt diese Struktur direkt zu verwenden, wird wie in Sample01 ein AdsStream verwendet. Das AdsStream Objekt wird mit einer Grösse von 19 Bytes initialisiert. Dies entspricht der Grösse des Datentyps PLCStruct in der SPS. Ein BinaryWriter wird verwendet, um die Daten der Editierfelder in den Stream zu schreiben. Nachdem die Position des Streams auf 0 gesetzt wird, werden die einzelnen Felder der Struktur in den Stream geschrieben. Als letztes wird der gesamte Stream mit Hilfe der TcAdsClient.Write in die SPS geschrieben.

Übertragen von Strukturen an die SPS 1:

C# Programm


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using TwinCAT.Ads;

namespace Sample02
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button btnWrite;
private System.Windows.Forms.TextBox tbInt;
private System.Windows.Forms.TextBox tbDint;
private System.Windows.Forms.TextBox tbByte;
private System.Windows.Forms.TextBox tbLReal;
private System.Windows.Forms.TextBox tbReal;

private System.ComponentModel.Container components = null;

private int hVar;private TcAdsClient tcClient;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )...

private void InitializeComponent()...

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
// Create instance of class TcAdsClient
tcClient = new TcAdsClient();

// Connect to local PLC - Runtime 1 - TwinCAT 3 Port=851
tcClient.Connect(851);

try
{
hVar = tcClient.CreateVariableHandle("MAIN.PLCVar");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}

private void btnWrite_Click(object sender, System.EventArgs e)
{
AdsStream dataStream = new AdsStream(19);
BinaryWriter binWrite = new BinaryWriter(dataStream);

dataStream.Position = 0;
try
{
//Fill stream according to the order with data from the text boxes
binWrite.Write(short.Parse(tbInt.Text));
binWrite.Write(int.Parse(tbDint.Text));
binWrite.Write(byte.Parse(tbByte.Text));
binWrite.Write(double.Parse(tbLReal.Text));
binWrite.Write(float.Parse(tbReal.Text));

//Write complete stream in the PLC
tcClient.Write(hVar,dataStream);
}
catch( Exception err)
{
MessageBox.Show(err.Message);
}
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Enable resources
try
{
tcClient.DeleteVariableHandle(hVar);
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
tcClient.Dispose();
}
}
}

PLC Programm


TYPE PLCStruct
STRUCT
intVal : INT;
dintVal : DINT;
byteVal : SINT;
lrealVal : LREAL;
realVal : REAL;
END_STRUCT
END_TYPE

PROGRAM MAIN
VAR
PLCVar : PLCStruct;
END_VAR