Accessing an array in the PLC

Requirements

Visual Studio 2010

Silverlight 4 Tools

TwinCAT ADS WCF 1.1.0.1

Description

Reading an array of integer variables from the PLC by the use of the TwinCAT ADS WCF service.
The TwinCAT.Ads.SL DLL (Silverlight) is used to read the data from the received byte array in an easy to handle way.

While using the WCF service within a Silverlight application, all methods of the used context interface are appended with the postfix Async.
And there is a extra callback method for each method with the postfix Completed too.

Example: Read1 -> Read1Async, Read1Completed

This is important because silverlight allows asynchronous communication only. A read action by the use of the Read1 method is requested by the use of the Read1Async method and the response will raise the Read1Completed callback.

Accessing an array in the PLC 1:

Silverlight 4 application

using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using TcAdsWcf_Sample06.TwinCAT.Ads.Wcf;
using TwinCAT.Ads.SL;

namespace TcAdsWcf_Sample06
{
    public partial class MainPage : UserControl
    {
    // If the netid string is empty, the TwinCAT ADS WCF service will use the TwinCAT Runtime on its host machine.
    private const string NETID = "";
    private const int PORT = 801;

    private TcAdsServiceSimplexClient wcfClient;

    public MainPage ( )
    {
        InitializeComponent ( );

        // Initialize WCF service client.
        wcfClient = new TcAdsServiceSimplexClient (
        new BasicHttpBinding ( ),
        new EndpointAddress ( "http://localhost:8003/TwinCAT/Ads/Wcf/TcAdsService/UnsecBasicHttp" ) );

        // Register for Read1Completed Event
        wcfClient.Read1Completed += new EventHandler<Read1CompletedEventArgs1>(wcfClient_Read1Completed);
    }

    private void wcfClient_Read1Completed(object sender, Read1CompletedEventArgs1 e)
    {
        // Create AdsStream and AdsBinaryReader for buffer handling.
        AdsStream stream = new AdsStream(e.Result);
        AdsBinaryReader binReader = new AdsBinaryReader(stream);
        stream.Position = 0;

        lbArray.Items.Clear();
        for (int i = 0; i < 100; i++)
        {
        lbArray.Items.Add(binReader.ReadInt16());
        }
    }

    private void btnRead_Click ( object sender, RoutedEventArgs e )
    {
        // Read array. 
        int buffLen = 100 * 2;
        wcfClient.Read1Async(NETID, PORT, "MAIN.PLCVar", buffLen, null);
    }
    }
}

 

TcAdsWcf_Sample06.zip