diff --git a/README.md b/README.md index 67f3105b..b8062560 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,17 @@ var myTag = new TagDint() }; // Read the value from the PLC -myTag.Read(); -int output = myTag.Value; +int output = myTag.Read(); // Output to Console -Console.WriteLine($"SomeProgram.SomeDINT = {output}"); +Console.WriteLine($"Original value: SomeProgram.SomeDINT = {output}"); + +// Write a new value to the PLC then read it back +myTag.Write(37); +output = myTag.Read(); + +// Output to Console +Console.WriteLine($"Updated value: SomeProgram.SomeDINT = {output}"); ``` In advanced scenarios, tags can be instantiated using generics (ex. `Tag`, `Tag`) and can be referenced via an `ITag` interface. diff --git a/src/Examples/CSharp DotNetCore/SimpleExample.cs b/src/Examples/CSharp DotNetCore/SimpleExample.cs new file mode 100644 index 00000000..0208b553 --- /dev/null +++ b/src/Examples/CSharp DotNetCore/SimpleExample.cs @@ -0,0 +1,40 @@ +using libplctag; +using libplctag.DataTypes; +using System; +using System.Net; +using System.Threading; + +namespace CSharpDotNetCore +{ + class SimpleExample + { + public static void Run() + { + //This is the absolute most simplified example code + //Please see the other examples for more features/optimizations + + //Instantiate the tag with the proper mapper and datatype + var myTag = new Tag() + { + Name = "PROGRAM:SomeProgram.SomeDINT", + Gateway = "10.10.10.10", + Path = "1,0", + PlcType = PlcType.ControlLogix, + Protocol = Protocol.ab_eip, + Timeout = TimeSpan.FromSeconds(5) + }; + + //Write value to PLC + //This will call Initialize internally since it's the first use of this tag + //myTag.Value will be set to 3737 before being transferred to PLC + myTag.Write(3737); + + //Read value from PLC + //Value will also be accessible at myTag.Value + int myDint = myTag.Read(); + + //Write to console + Console.WriteLine(myDint); + } + } +} diff --git a/src/libplctag/ITag.cs b/src/libplctag/ITag.cs index 3c10a2f2..2c1684b2 100644 --- a/src/libplctag/ITag.cs +++ b/src/libplctag/ITag.cs @@ -23,9 +23,11 @@ public interface ITag : IDisposable Status GetStatus(); void Initialize(); Task InitializeAsync(CancellationToken token = default); - void Read(); - Task ReadAsync(CancellationToken token = default); + object Read(); + Task ReadAsync(CancellationToken token = default); void Write(); Task WriteAsync(CancellationToken token = default); + + Object Value { get; set; } } } \ No newline at end of file diff --git a/src/libplctag/TagOfT.cs b/src/libplctag/TagOfT.cs index 488325f9..c72e3efa 100644 --- a/src/libplctag/TagOfT.cs +++ b/src/libplctag/TagOfT.cs @@ -132,19 +132,25 @@ public async Task InitializeAsync(CancellationToken token = default) } /// - public async Task ReadAsync(CancellationToken token = default) + public async Task ReadAsync(CancellationToken token = default) { await _tag.ReadAsync(token); DecodeAll(); + return Value; } /// - public void Read() + public T Read() { _tag.Read(); DecodeAll(); + return Value; } + object ITag.Read() => Read(); + + async Task ITag.ReadAsync(CancellationToken token) => await ReadAsync(); + /// public async Task WriteAsync(CancellationToken token = default) { @@ -155,6 +161,13 @@ public async Task WriteAsync(CancellationToken token = default) await _tag.WriteAsync(token); } + /// + public async Task WriteAsync(T value, CancellationToken token = default) + { + Value = value; + await WriteAsync(token); + } + /// public void Write() { @@ -165,6 +178,13 @@ public void Write() _tag.Write(); } + /// + public void Write(T value) + { + Value = value; + Write(); + } + void DecodeAll() { Value = _plcMapper.Decode(_tag); @@ -189,7 +209,7 @@ void EncodeAll() /// The local memory value that can be transferred to/from the PLC /// public T Value { get; set; } - + object ITag.Value { get => Value; set => Value = (T)value; } public event EventHandler ReadStarted {