Opc DataAccess libary for .NET.
Name | Stable | Preview |
---|---|---|
GodSharp.OpcDa |
OpcTagItem tags = new OpcTagItem[]
{
new OpcTagItem { Id = 1, ItemId = "Channel.Test.Tag1" },
new OpcTagItem { Id = 2, ItemId = "Channel.Test.Tag2" }
});
Sample 1
OpcDaClient client = new OpcDaClient(x => {
x.ProgId = "KEPware.KEPServerEx.V4"; //required
x.Tags = tags;
});
Sample 2
OpcDaClient client = new OpcDaClient();
client.Initialize(x => {
x.ProgId = "KEPware.KEPServerEx.V4"; //required
x.Tags = tags;
});
sample 2 same as sample 1.
Sample 3
OpcDaClient client = new OpcDaClient(new OpcDaClientOption()
{
ProgId ="KEPware.KEPServerEx.V4", //required
Tags = tags;
});
Sample 4
OpcDaClient client = new OpcDaClient();
client.Initialize(new OpcDaClientOption()
{
ProgId ="KEPware.KEPServerEx.V4", //required
Tags = tags;
});
sample 4 same as sample 3.
Data changed event:
client.DataChange = (param, tags) =>
{
foreach (OpcTagItem item in tags)
{
Console.WriteLine($@"{item.ItemId}:{item.Value.ToString()}");
}
};
Server shutdown event:
client.Shutdown = (resaon, client) =>
{
Console.WriteLine("Opc server shutdown.");
};
It will throw InvalidOperationException
and message is Opc client is not initialized
when you call Connent
method if not initialized.
// Connect
client.Connent();
// Disconnect
client.Disconnect();
You can get connect status by client?.Connected
.
If you want to add tags, you must first connect to opc server.
Add single tag
client.AddTags(new OpcTagItem { Id = 3, ItemId = "Channel.Test.Tag3" });
Add multi tag
client.AddTags(new OpcTagItem[]
{
new OpcTagItem { Id = 3, ItemId = "Channel.Test.Tag3" },
new OpcTagItem { Id = 4, ItemId = "Channel.Test.Tag4" }
});
Remove single tag
client.RemoveTags(3); // OpcTagItem.Id
client.RemoveTags("Channel.Test.Tag3"); // OpcTagItem.ItemId
Remove multi tag
client.RemoveTags(new int[]{ 3, 4 });
client.RemoveTags(new string[]{ "Channel.Test.Tag3", "Channel.Test.Tag4" });
Remove single tag group
client.RemoveGroup("Channel.Test.Group1");
Remove multi tag group
client.RemoveGroups(new string[]{ "Channel.Test.Group1", "Channel.Test.Group2" });
Read single tag value
object val = client.SyncReadValue("Channel.Test.Tag1");
Read single tag
OpcTagItem val = client.SyncRead("Channel.Test.Tag1");
Read multi tag
IEnumerable<OpcTagItem> tags = client.SyncRead(new string[]{ "Channel.Test.Tag1", "Channel.Test.Tag2" });
Write single tag value
client.AsycnWriter("Channel.Test.Tag1",val);
Write multi tag
OpcTagItem[] items = new OpcTagItem[]
{
new OpcTagItem(){ ItemId="Channel.Test.Tag1" },
new OpcTagItem(){ ItemId="Channel.Test.Tag2" }
};
client.AsycnWriter(items);