C# 倍福ADS连接IPC,使用AdsRemote组件和ADS通讯

C# 倍福ADS的正确打开方式,使用AdsRemote组件优雅的通过ADS通讯,支持WPF窗体控件的绑定机制,做上位机页面很方便,大大节省了开发时间。

倍福的官方文档给的例子我就不多说了,今天介绍一种更改优雅的使用ADS组件进行通讯的方式,非常符合高级语言的编程风格,在也不用到处readany,writeany了。

https://github.com/nikvoronin/AdsRemote

AdsRemote:Beckhoff的TwinCAT.Ads API库的高级接口可以节省大量的开发时间。您不需要网络线程或句柄。只需声明一个C#变量,并通过变量属性将其绑定到PLC var。就这样。

我最喜欢的使用方式是变量变化后自动通知,类似观察者模式,不用傻傻的死等结果的反馈。Adsremote组件内部会使用一个线程来对取变量,当值发生变化时,调用ValueChanged事件。

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 main_count = plc.Var (“MAIN.count”);
Var main_state = plc.Var(“MAIN.state”);
Var g_Version = plc.Var(".VERSION");

Var frm0 = plc.Var(“Inputs.Frm0InputToggle”, 27907);
Var devState = plc.Var(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 main_count = plc.Var(“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 main_count = plc.Var(“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 frm0 { get; set; }

private void Window_Loaded(object sender, RoutedEventArgs e)
{
plc = new PLC(“5.2.100.109.1.1”);
frm0 = plc.Var(“Inputs.Frm0InputToggle”, Port: 27907);

DataContext = this;

}
And explicitly specifying the field .RemoteValue of the remote variable

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Seven Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值