Transmitting structures to the PLC
Download
Language / IDE | Unpack the example program |
---|---|
Visual C# | |
Visual Basic (for .NET Framework) | |
Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) | |
Delphi for .NET (Borland Developer Studio 2006) |
Task
A structure is to be written into the PLC by the .NET application. The elements in the structure have different data types.
Description
The structure we want to write into the PLC:
TYPE PLCStruct
STRUCT
intVal : INT; (*Offset 0*)
dintVal : DINT; (*Offset 2*)
byteVal : SINT; (*Offset 6*)
lrealVal : LREAL;(*Offset 7*)
realVal : REAL; (*Offset 15 --> Total size 19 Bytes*)
END_STRUCT
END_TYPE
In c# the structure would look like this:
public struct PLCStruct
{
public short intVal;
public int dintVal;
public byte byteVal;
public double lrealVal;
public float realVal;
}
This structure will not be created directly. Instead we will use a AdsStream as in Sample01. This object is initialized with a size of 19 bytes in the write event method. This is equivalent to the size of the data type PLCStruct in the SPS. A BinaryWriter is used to write the data from the textboxes to the stream. After setting the stream position to 0 the individual fields are written to the stream. Finally the total stream is written into the SPS with the help of TcAdsClient.Write.
C# Program
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 - TwinCAT2 Port=801, TwinCAT3 Port=851
tcClient.Connect(801);
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 program
TYPE PLCStruct
STRUCT
intVal : INT;
dintVal : DINT;
byteVal : SINT;
lrealVal : LREAL;
realVal : REAL;
END_STRUCT
END_TYPE
PROGRAM MAIN
VAR
PLCVar : PLCStruct;
END_VAR