Name | NuGet | MyGet |
---|---|---|
GodSharp.Opc.Da | ||
GodSharp.Opc.Da.OpcAutomation | ||
GodSharp.Opc.Da.OpcAutomation.Kepware | ||
GodSharp.Opc.Da.OpcAutomation.Graybox | ||
GodSharp.Opc.Da.OpcNetApi |
OpcDaClient
has two implementations, which one to choose depends on your own preferences.
- OpcAutomation :
GodSharp.Opc.Da.OpcAutomation
- OpcNetApi :
GodSharp.Opc.Da.OpcNetApi
-
OpcAutomation
you should register
OPCDAAuto.dll
, you can download from others or /assets/da folder. -
Graybox
you should register
gbda_aut.dll
, you can download from http://www.gray-box.net/download_daawrapper.php?lang=en. -
Kepware
you should install
Kepware
.
you should install OPC Core Components
, you can download from others or /assets/da folder.
This tutorial uses GodSharp.Opc.Da.OpcAutomation
library, you should install this package first.
PM> Install-Package GodSharp.OpcDa.OpcAutomation
If you want to use data change event, you should set IsSubscribed
value to true
,default is false
.
Sample Code :
// initial with data info
// The group `Name`, `ClientHandle` is unique and required, `UpdateRate` is required too.
// The tag `ItemName`, `ClientHandle` is unique and required.
var groups = new List<GroupData>
{
new GroupData
{
Name = "default", UpdateRate = 100, ClientHandle = 010, IsSubscribed = true,
Tags = new List<Tag>
{
new Tag("Test.Simulator.Booleans.B0001", 011),
new Tag("Test.Simulator.Numbers.N0001", 012),
new Tag("Test.Simulator.Characters.C0001", 013)
}
},
new GroupData
{
Name = "group1", UpdateRate = 100, ClientHandle = 100,IsSubscribed = true,
Tags = new List<Tag>
{
new Tag("Test.Simulator.Booleans.B0002", 101),
new Tag("Test.Simulator.Numbers.N0002", 102),
new Tag("Test.Simulator.Characters.C0002", 103)
}
},
new GroupData
{
Name = "group2", UpdateRate = 100, ClientHandle = 200,IsSubscribed = false,
Tags = new List<Tag>
{
new Tag("Test.Simulator.Booleans.B0003", 201),
new Tag("Test.Simulator.Numbers.N0003", 202),
new Tag("Test.Simulator.Characters.C0003", 203)
}
}
};
var server = new ServerData
{
Host = "127.0.0.1",
ProgId = "Kepware.KEPServerEX.V6",
// initial with data info,after connect will be add to client
// if this is null,you should add group and tag manually
Groups = groups
};
var client = DaClientFactory.Instance.CreateOpcAutomationClient(new DaClientOptions(
server,
OnDataChangedHandler,
OnShoutdownHandler,
OnAsyncReadCompletedHandler,
OnAsyncWriteCompletedHandler));
- The group
Name
,ClientHandle
is unique and required,UpdateRate
is required too.- The tag
ItemName
,ClientHandle
is unique and required.
var connected = client.Connect();
If you want to use data change event, you should set IsSubscribed
value to true
,default is false
.
client.Add(new Group() {Name = "default", UpdateRate = 100, ClientHandle = 010});
client.Add(new Group() {Name = "group1", UpdateRate = 100, ClientHandle = 100,IsSubscribed = true});
client.Add(new Group() {Name = "group2", UpdateRate = 100, ClientHandle = 200,IsSubscribed = false});
Important! The group
Name
,ClientHandle
is unique and required,UpdateRate
is required too.
// add one by one
client.Groups["group1"].Add(new Tag("Channel1.Device1.Group1.Tag1", 100));
client.Groups["group1"].Add(new Tag("Channel1.Device1.Group1.Tag2", 101));
// add multi one time
client.Groups["group1"].Add(
new Tag("Channel1.Device1.Group1.Code", 100),
new Tag("Channel1.Device1.Group1.Number", 101));
Important! The tag
ItemName
,ClientHandle
is unique and required.
If you want to use data change event, you should set IsSubscribed
value to true
,default is false
.
public static void OnDataChangedHandler(DataChangedOutput output)
{
Console.WriteLine($"{output.Data.ItemName}:{output.Data.Value},{output.Data.Quality} / {output.Data.Timestamp}");
}
public static void OnAsyncReadCompletedHandler(AsyncReadCompletedOutput output)
{
Console.WriteLine(
$"Async Read {output.Data.Result.ItemName}:{output.Data.Result.Value},{output.Data.Result.Quality} / {output.Data.Result.Timestamp} / {output.Data.Code}");
}
public static void OnAsyncWriteCompletedHandler(AsyncWriteCompletedOutput output)
{
Console.WriteLine($"Async Write {output.Data.Result.ItemName}:{output.Data.Code}");
}
public static void OnShoutdownHandler(Server server, string reason)
{
}
Refresh tags in group when you want get all tags latest values by group OnDataChangedHandler
event handler.
client.Groups["group1"].RefreshAsync();
// or
client["group1"].RefreshAsync();
See sample code.
client.Disconnect();
Free!