-
Notifications
You must be signed in to change notification settings - Fork 46
/
ExampleNotifyChanged.cs
101 lines (86 loc) · 3.18 KB
/
ExampleNotifyChanged.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) libplctag.NET contributors
// https://github.com/libplctag/libplctag.NET
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
using libplctag.DataTypes;
using libplctag;
using System;
namespace CSharp_DotNetCore
{
internal class ExampleNotifyChanged
{
public static void Run()
{
var myTag = new NotifyValueChangedTag<DintPlcMapper, int>()
{
Name = "MyTag",
Gateway = "127.0.0.1",
Path = "1,0",
PlcType = PlcType.ControlLogix,
Protocol = Protocol.ab_eip,
AutoSyncReadInterval = TimeSpan.FromMilliseconds(500)
};
myTag.ValueChanged += (s, e) => Console.WriteLine($"{myTag.Name} changed at {DateTime.Now} to {myTag.Value}");
myTag.Initialize();
var myUdtTag = new NotifyValueChangedTag<MyUdtMapper, MyUdt>()
{
Name = "MyUdtTag",
Gateway = "127.0.0.1",
Path = "1,0",
PlcType = PlcType.ControlLogix,
Protocol = Protocol.ab_eip,
AutoSyncReadInterval = TimeSpan.FromMilliseconds(500)
};
myUdtTag.ValueChanged += (s, e) => Console.WriteLine($"{myUdtTag.Name} changed at {DateTime.Now} to {myUdtTag.Value.MyDint}|{myUdtTag.Value.MyFloat}");
myUdtTag.Initialize();
}
class NotifyValueChangedTag<M, T> : Tag<M, T>
where M : IPlcMapper<T>, new()
{
public event EventHandler<EventArgs> ValueChanged;
private int previousHash;
public NotifyValueChangedTag()
{
ReadCompleted += NotifyValueChangedTag_ReadCompleted;
}
private void NotifyValueChangedTag_ReadCompleted(object sender, TagEventArgs e)
{
var currentHash = this.Value.GetHashCode();
if (currentHash != previousHash)
{
ValueChanged?.Invoke(this, EventArgs.Empty);
}
previousHash = currentHash;
}
}
class MyUdt
{
public int MyDint { get; set; }
public float MyFloat { get; set; }
public override int GetHashCode()
{
// Use the tuple shortcut to get an okay hash
return (MyDint, MyFloat).GetHashCode();
}
}
class MyUdtMapper : PlcMapperBase<MyUdt>
{
public override int? ElementSize => 8;
public override MyUdt Decode(Tag tag, int offset)
{
return new MyUdt()
{
MyDint = tag.GetInt32(offset),
MyFloat = tag.GetFloat32(offset + 4)
};
}
public override void Encode(Tag tag, int offset, MyUdt value)
{
tag.SetInt32(offset, value.MyDint);
tag.SetFloat32(offset + 4, value.MyFloat);
}
}
}
}