Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Created February 29, 2012 12:04
Show Gist options
  • Save MrTrick/1940376 to your computer and use it in GitHub Desktop.
Save MrTrick/1940376 to your computer and use it in GitHub Desktop.
Simple OPC client - that doesn't work.
using System;
using System.Collections.Generic;
using System.Text;
using Softing.OPCToolbox;
using Softing.OPCToolbox.Client;
namespace opc_simple
{
class Program
{
protected DaSession session;
protected DaSubscription subscription;
protected DaItem item;
public Program(string server_name, string item_name) {
int result;
// TODO - Optionally pass in a license file
//result = Application.Instance.Activate(EnumFeature.DA_CLIENT, "XXXX-XXXX-XXXX-XXXX-XXXX");
//if (ResultCode.SUCCEEDED(result)) throw new Exception("Could not activate using this license key");
// END TODO - design time license activation
// Proceed with the OPC Toolbox core initialization
result = Application.Instance.Initialize();
if (!ResultCode.SUCCEEDED(result)) throw new Exception("Could not initialize application");
// Connect a session to the server
session = new DaSession(server_name);
result = session.Connect(true, false, null);
if (!ResultCode.SUCCEEDED(result)) throw new Exception("Could not connect to the server");
// Set up the item
subscription = new DaSubscription(1000, session);
item = new DaItem(item_name, subscription);
}
public void read()
{
int result;
DaItem[] items = { item };
ValueQT[] values = null;
int[] results = null;
result = subscription.Read(0, items, out values, out results, null);
if (!ResultCode.SUCCEEDED(result)) throw new Exception("Read operation failed");
if (!ResultCode.SUCCEEDED(results[0])) throw new Exception("Read item failed");
Console.Out.WriteLine("Read item: " + results[0].ToString());
}
static void Main(string[] args) {
Program program = new Program(
"opcda:///Softing.OPCToolboxDemo_ServerDA.1/{2E565242-B238-11D3-842D-0008C779D775}",
"maths.sin"
);
Console.Out.WriteLine("Connected to the server!");
program.read();
Console.Out.WriteLine("Read the item!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment