OPC DA 客戶端例項[.net]
static void Main(string[] args)
{
// Create a server object and connect to the TwinCATOpcServer
Opc.URL url = new Opc.URL("opcda://localhost/BECKHOFF.TwinCATOpcServerDA");
Opc.Da.Server server = null;
OpcCom.Factory fact = new OpcCom.Factory();
server = new Opc.Da.Server(fact, null);
server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
// Create a group
Opc.Da.Subscription group;
Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
groupState.Name = "Group";
groupState.Active = true;
group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
// add items to the group.
Opc.Da.Item[] items = new Opc.Da.Item[3];
items[0] = new Opc.Da.Item();
items[0].ItemName = "PLC1.dyn_R8[1]";
items[1] = new Opc.Da.Item();
items[1].ItemName = "PLC1.dyn_R4[1]";
items[2] = new Opc.Da.Item();
items[2].ItemName = "PLC1.dyn_I4[1]";
items = group.AddItems(items);
group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted);
Console.ReadLine();
}
static void OnTransactionCompleted( object group, object hReq, Opc.Da.ItemValueResult[] items)
{
Console.WriteLine("------------------->");
Console.WriteLine("DataChanged ...");
for (int i = 0; i < items.GetLength(0); i++)
{
Console.WriteLine("Item DataChange - ItemId: {0}", items[i].ItemName);
Console.WriteLine(" Value: {0,-20}", items[i].Value);
Console.WriteLine(" TimeStamp: {0:00}:{1:00}:{2:00}.{3:000}",
items[i].Timestamp.Hour,
items[i].Timestamp.Minute,
items[i].Timestamp.Second,
items[i].Timestamp.Millisecond);
}
Console.WriteLine("-------------------<");
}