项目作者: svroonland

项目描述 :
用于通过Reactive Extensions(Rx)与Beckhoff TwinCAT PLC连接的.NET库
高级语言: C#
项目地址: git://github.com/svroonland/TwinRx.git
创建时间: 2016-12-10T22:07:48Z
项目社区:https://github.com/svroonland/TwinRx

开源协议:Apache License 2.0

下载


NOTE

As of version 4.2.172.0, the Beckhoff ADS library for .NET also includes support for Reactive Extensions, making this library obsolete. See here for more information: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_adsnetref/7312584843.html&id=


Build status
NuGet version

TwinRx

TwinRx is a library for connecting a .NET application with a Beckhoff TwinCAT PLC program via Reactive Extensions (Rx) over ADS.

Features

  • Create an IObservable for a PLC variable, bringing changes to the PLC variable into the Reactive world.
  • Make use of Rx’s extensive event processing and querying capabilities to transform the observable into events of interest.
  • Stream (write) an existing IObservable to a PLC variable
  • Transparently reregister the notifications after a connection loss

Requires

  • .NET 4.5 or higher
  • Beckhoff TwinCAT PLC (v2 or v3)

Installation

  • Install the NuGet package “TwinRx” using the NuGet Package Manager in Visual Studio

Example code

  1. using System;
  2. using System.Reactive.Linq;
  3. using TwinCAT.Ads;
  4. using TwinRx;
  5. var adsClient = new TcAdsClient();
  6. adsClient.Connect(801); // 801 for TwinCAT 2, 851 for TwinCAT 3
  7. var client = new TwinCatRxClient(adsClient);
  8. var counter = client.ObservableFor<short>("MAIN.var1", 20);
  9. // Print out each value as it changes
  10. counter.Subscribe(v => Console.WriteLine("Variable is now:" + v));
  11. // Print out 10 values at a time
  12. var buffered = counter.Buffer(10);
  13. buffered.Subscribe(v => Console.WriteLine("Last 10 values were:" + String.Join(" - ", v)));
  14. // Values including timestamp
  15. var valuesWithTimestamp = counter.Select(i => new Tuple<short, DateTime>(i, DateTime.Now));
  16. valuesWithTimestamp.Subscribe(Console.WriteLine);
  17. // Take a single value each second
  18. valuesWithTimestamp
  19. .Sample(TimeSpan.FromSeconds(5))
  20. .Subscribe(Console.WriteLine);
  21. var myString = client.ObservableFor<string>("MAIN.var2");
  22. myString.Subscribe(Console.WriteLine);
  23. // Write a value to the PLC periodically
  24. var valueEverySecond = Observable
  25. .Interval(TimeSpan.FromSeconds(1))
  26. .Select(i => (short) i);
  27. var writer = client.StreamTo("MAIN.var3", valueEverySecond);
  28. // Only even ones
  29. var evens = client.ObservableFor<short>("MAIN.var4").Where(i => i%2 == 0);
  30. var evensWithTimestamp = evens
  31. .Timestamp()
  32. .Zip(evens.TimeInterval(), (valWithTimestamp, interval) => new { val = "Even value is " + valWithTimestamp, interval });
  33. evensWithTimestamp.Subscribe(Console.WriteLine);