Skip to content

Commit

Permalink
Merge pull request #93 from libplctag/overload-read-write
Browse files Browse the repository at this point in the history
Overload Read/Write to actually use Value directly
  • Loading branch information
jkoplo authored Dec 27, 2021
2 parents 8351786 + 7a9683c commit 4b58fc5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<DintPlcMapper, int>`, `Tag<BoolPlcMapper, bool>`) and can be referenced via an `ITag` interface.

Expand Down
40 changes: 40 additions & 0 deletions src/Examples/CSharp DotNetCore/SimpleExample.cs
Original file line number Diff line number Diff line change
@@ -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<DintPlcMapper, int>()
{
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);
}
}
}
6 changes: 4 additions & 2 deletions src/libplctag/ITag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> ReadAsync(CancellationToken token = default);
void Write();
Task WriteAsync(CancellationToken token = default);

Object Value { get; set; }
}
}
26 changes: 23 additions & 3 deletions src/libplctag/TagOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,25 @@ public async Task InitializeAsync(CancellationToken token = default)
}

/// <inheritdoc cref="Tag.ReadAsync"/>
public async Task ReadAsync(CancellationToken token = default)
public async Task<T> ReadAsync(CancellationToken token = default)
{
await _tag.ReadAsync(token);
DecodeAll();
return Value;
}

/// <inheritdoc cref="Tag.Read"/>
public void Read()
public T Read()
{
_tag.Read();
DecodeAll();
return Value;
}

object ITag.Read() => Read();

async Task<object> ITag.ReadAsync(CancellationToken token) => await ReadAsync();

/// <inheritdoc cref="Tag.WriteAsync"/>
public async Task WriteAsync(CancellationToken token = default)
{
Expand All @@ -155,6 +161,13 @@ public async Task WriteAsync(CancellationToken token = default)
await _tag.WriteAsync(token);
}

/// <inheritdoc cref="Tag.WriteAsync"/>
public async Task WriteAsync(T value, CancellationToken token = default)
{
Value = value;
await WriteAsync(token);
}

/// <inheritdoc cref="Tag.Write"/>
public void Write()
{
Expand All @@ -165,6 +178,13 @@ public void Write()
_tag.Write();
}

/// <inheritdoc cref="Tag.Write"/>
public void Write(T value)
{
Value = value;
Write();
}

void DecodeAll()
{
Value = _plcMapper.Decode(_tag);
Expand All @@ -189,7 +209,7 @@ void EncodeAll()
/// The local memory value that can be transferred to/from the PLC
/// </summary>
public T Value { get; set; }

object ITag.Value { get => Value; set => Value = (T)value; }

public event EventHandler<TagEventArgs> ReadStarted
{
Expand Down

0 comments on commit 4b58fc5

Please sign in to comment.