AdsRemote 0.5.0

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

PS: Charged with network broadcast searching of the PLCs.

Install-Package AdsRemote -Version 0.5.0
dotnet add package AdsRemote --version 0.5.0
<PackageReference Include="AdsRemote" Version="0.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdsRemote --version 0.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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);

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);

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.5.0 597 6/29/2017