日期:2014-05-17  浏览次数:21182 次

想用C#读取wincc的变量 下边是VB中引用CCHMIRuntime 想知道在C#中如何才能实现
Dim HMIRuntime 
Set HMIRuntime=CreateObject("CCHMIRuntime.HMIRuntime")
g_DataSourceName = HMIRuntime.Tags("@DatasourceNameRT").Read

------解决方案--------------------
 object HMIRuntime = null;

        System.Type oType = System.Type.GetTypeFromProgID("CCHMIRuntime.HMIRuntime");
        HMIRuntime = System.Activator.CreateInstance(oType);
        System.Reflection.MethodInfo mi = oType.GetMethod("Tags");
        object tags = mi.Invoke(HMIRuntime, new object[] { "@DatasourceNameRT" });
        ...

------解决方案--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Opc;
using Opc.Da;
using OpcCom;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        public static Opc.Da.Server m_server = null;//定义数据存取服务器
        public static Opc.Da.Subscription subscription = null;//定义组对象(订阅者)
        public static Opc.Da.SubscriptionState state = null;//定义组(订阅者)状态,相当于OPC规范中组的参数
        public static Opc.IDiscovery m_discovery = new ServerEnumerator();//定义枚举基于COM服务器的接口,用来搜索所有的此类服务器。

        static void Main(string[] args)
        {

            //StreamWriter dr = new StreamWriter(@"E:\test.txt",true, System.Text.Encoding.GetEncoding("GB2312"));
            //string str = DateTime.Now.ToString();
            //dr.WriteLine(str);
            //dr.Flush();
            //dr.Close();
            //return;
            StreamWriter dr = new StreamWriter(@"E:\system.log", true, System.Text.Encoding.GetEncoding("GB2312"));
            try
            {
                Opc.Server[] servers = m_discovery.GetAvailableServers(Specification.COM_DA_20, "192.168.1.5", null);
                if (servers != null)
                {
                    foreach (Opc