Skip to content

Instantly share code, notes, and snippets.

@MrTrick MrTrick/opc_array.cs
Created Feb 29, 2012

Embed
What would you like to do?
Simple OPC DA Client to read/write arrays
//-----------------------------------------------------------------------------
// |
// Mindbleach Engineering, Australia |
// |
// Copyright (c) 2012 Mindbleach Engineering |
// All Rights Reserved |
// |
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OPC DA Array Client |
// |
// Filename : Program.cs |
// Version : 1.0.0 |
// Date : 29-Feb-2012 |
// |
// Description : Command-line client to simply read and write arrays from/to |
// OPC DA servers. |
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Softing.OPCToolbox;
using Softing.OPCToolbox.Client;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
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, code " + ((EnumResultCode)(result)).ToString());
// Connect a session to the server
session = new DaSession(server_name);
// Set up the item
subscription = new DaSubscription(1000, session);
item = new DaItem(item_name, subscription);
result = session.Connect(true, false, null);
if (!ResultCode.SUCCEEDED(result)) throw new Exception("Could not connect to the server, code " + ((EnumResultCode)(result)).ToString());
}
public ValueQT 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, code " + ((EnumResultCode)(result)).ToString());
if (!ResultCode.SUCCEEDED(results[0])) throw new Exception("Read item failed, code " + ((EnumResultCode)(results[0])).ToString());
if (values[0].Quality != EnumQuality.GOOD) throw new Exception("Value quality is not GOOD, quality " + values[0].Quality.ToString());
return values[0];
}
public void write(ValueQT value)
{
int result;
DaItem[] items = { item };
ValueQT[] values = { value };
int[] results = null;
result = subscription.Write(items, values, out results, new ExecutionOptions());
if (!ResultCode.SUCCEEDED(result)) throw new Exception("Write operation failed, code " + ((EnumResultCode)(result)).ToString());
if (!ResultCode.SUCCEEDED(results[0])) throw new Exception("Write item failed, code " + ((EnumResultCode)(results[0])).ToString());
}
protected static String bytesToHexString(Byte[] bytes)
{
SoapHexBinary shb = new SoapHexBinary(bytes);
return shb.ToString();
}
protected static Byte[] hexStringToBytes(String hex_string)
{
SoapHexBinary shb = SoapHexBinary.Parse(hex_string);
return shb.Value;
}
protected static void usage(String message)
{
if (message.Length > 0) Console.Out.WriteLine(message + "\n");
Console.Error.WriteLine(
"OPC DA Array Client - (c) Mindbleach Engineering 2012 - v1.0.0\n" +
"Usage: opc_array.exe SERVER ITEM [VALUE]\n"+
" SERVER : The id of the OPC server, \n"+
" eg \"opcda:///Schindler.Lobby.OPC-Server.DA\"\n"+
"\n" +
" ITEM : The id of the OPC array item, \n"+
" eg \"Building 1.Group 1.Lift 1.Switchable Lift States\"\n"+
"\n" +
" VALUE : Optionally, a byte array value encoded as a hexadecimal\n" +
" string, eg 02031B3A (Where each two digits represent one byte)\n"+
"\n" +
"\n" +
" If VALUE is omitted, the client is in READ mode. It will fetch that array\n"+
" item from the server, and print it to standard out as a hexadecimal string.\n"+
"\n"+
" If VALUE is given, the client is in WRITE mode. It will attempt to write the\n"+
" value to that array item. On success, no output will be given.\n"+
"\n"+
" On error, the client will print \"Error:(error message)\" to standard error,\n"+
" and return a non-zero exit code.\n");
Environment.Exit(255);
}
static void Main(string[] args)
{
ValueQT value;
if (args.Length < 2 || args.Length > 3) usage("This program requires 2 or 3 arguments, not "+args.Length);
string server = args[0];
string item = args[1];
try
{
Program program = new Program(server, item);
//Read mode?
if (args.Length == 2)
{
value = program.read();
//Console.Out.WriteLine("Read item: " + value.ToString());
Console.Out.WriteLine(bytesToHexString((Byte[])value.Data));
}
//Write mode
else if (args.Length == 3)
{
value = new ValueQT(hexStringToBytes(args[2]), EnumQuality.QUALITY_NOT_SET, new DateTime());
program.write(value);
}
//Success!
Environment.Exit(0);
}
catch (Exception e)
{
Console.Error.WriteLine("Error:" + e.Message);
Environment.Exit(1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.