Git Product home page Git Product logo

adsremote's Introduction

High-level interface for Beckhoff's TwinCAT.Ads API library that might save a lot of development time. You don't need network threads or handles. Just declare a C# variable and bind it via the variable attribute to the PLC var. That's all.

Project is not affiliated with Beckhoff.

Getting started

Ads Route

First you have to give your device/machine the permission to communicate with the Twincat Ads server by adding a route.

There are different ways of doing this depending on the device. You can use the Twincat Remote Manager for example.

You can use Ads.Remote.Router.AmsRouter for broadcast searching of network PLCs. With help of Ads.Remote.PLC.Router you can add new route record into the remote PLC.

Installation

  • TwinCAT 2 or 3 have to be installed.
  • Use NuGet for full automation or download library from the release section then add project reference.

Demo

Broadcast searching

CxFinder 1.1

Remote variables

operator-pi

How To...

There is an unstable develop branch is selected by default. If you want to use this library in production see stable master branch or download from release section or use NuGet package AdsRemote.

PLC instance

First you have to create an instance of PLC object. This one wiil be like a factory that produces linked variables.

PLC plc = new PLC("5.2.100.109.1.1");

When device connected or disconnected

plc.DeviceReady += Plc_DeviceReady;
plc.DeviceLost += Plc_DeviceLost;

[...]

private void Plc_DeviceReady(object sender, AdsDevice e)
{
    Log("READY [" + e.Address.Port.ToString() + "]");
}

How to create and link variables

Create a copy of your PLC's variable then use it like an ordinary variable We use PLC object that produces linked variables. After that variables will autoupdating their state and value.

Var<short>  main_count = plc.Var<short> ("MAIN.count");
Var<ushort> main_state = plc.Var<ushort>("MAIN.state");
Var<short>  g_Version  = plc.Var<ushort>(".VERSION");

Var<ushort> frm0       = plc.Var<ushort>("Inputs.Frm0InputToggle", 27907);
Var<ushort> devState   = plc.Var<ushort>(0xF030, 0x5FE, 27907);

long framesTotal += frm0 / 2; // automatic type casting
MessageBox.Show(frm0);        // cast into the string type without call of the ToString()

From now you can subscribe on value changing.

main_count.ValueChanged +=
    delegate
    {
        counterStatusLabel.Text = main_count;
    };

or

main_count.ValueChanged +=
    delegate (object src, Var v)
    {
        ushort val = (ushort)v.GetValue();
        framesTotal += val / 2;
        counterStatusLabel.Text = val.ToString();
    };

Write-back to the PLC

Use "RemoteValue" propertie to write a new value to the PLC runtime.

main_count.RemoteValue = 123;

WinForms data binding

For example we will bind Text propertie of the Label control with default name label1. At the PLC side we have MAIN.count variable that contains value of counter that we should show.

Var<short> main_count = plc.Var<short>("MAIN.count");

Binding b = new Binding("Text", main_count, "RemoteValue");
b.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
b.DataSourceUpdateMode = DataSourceUpdateMode.Never;

label1.DataBindings.Add(b);

If we have to convert given value we define a format converter

Var<short> main_count = plc.Var<short>("MAIN.count");

Binding b2 = new Binding("ForeColor", main_count, "RemoteValue");
b2.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
b2.DataSourceUpdateMode = DataSourceUpdateMode.Never;
b2.Format += (s, ea) =>
{
    ea.Value =  (short)ea.Value < 0 ? Color.Blue : Color.Red;
};
label1.DataBindings.Add(b2);

WPF data bindings

In WPF you must use properties instead of variables.

PLC plc;
public Var<ushort> frm0 { get; set; }

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    plc = new PLC("5.2.100.109.1.1");
    frm0 = plc.Var<ushort>("Inputs.Frm0InputToggle", Port: 27907);
    
    DataContext = this;
}

And explicitly specifying the field .RemoteValue of the remote variable

<Grid>
    <Label x:Name="label" Content="{Binding frm0.RemoteValue}" />
</Grid>

Create variables with help of attributes

You can create special class with several variables then mark those ones as remote PLC variables. Remember, all variables must declare a type. Otherwise you'll get NULL.

Public fields only!

public class PRG_Main
{
    [LinkedTo("MAIN.count", As: typeof(short), Port: (int)AmsPort3.PlcRuntime1)]
    public Var count;

    [LinkedTo("MAIN.state", Port: (int)AmsPort3.PlcRuntime1)]
    public Var<ushort> state;
    
    [LinkedTo("Inputs.Frm0InputToggle", Port: 27907)]
    public Var<ushort> frm0_1;

    [LinkedTo(IGrp: 0xF030, IOffs: 0x5F4, Port: 27907)]
    public Var<ushort> frm0;
}

or more concisely for the PLC's Runtime #1

public class PRG_Main
{
    [LinkedTo("MAIN.count")]
    public Var<short> count;

    [LinkedTo("MAIN.state")]
    public Var<ushort> state;
}

Again in WPF-project you should use properties

public class PRG_Main
{
    [LinkedTo("MAIN.count")]
    public Var<short> count { get; set; }

    [LinkedTo("MAIN.state")]
    public Var<ushort> state { get; set; }
}

It's time to create instance of our class.

If you don't need of special class constructor just write:

PRG_Main Main = plc.Class<PRG_Main>();

otherwise for cunstructor with parameter list or something else we use it in this maner

Main = new PRG_Main(param1, param2, ...);
plc.Class(Main);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.