如何用C#读取wincc的变量 仿照VB中引用CCHMIRuntime

.Net技术 码拜 9年前 (2015-08-12) 4984次浏览
 

Dim HMIRuntime 

Set HMIRuntime=CreateObject(“CCHMIRuntime.HMIRuntime”)

g_DataSourceName = HMIRuntime.Tags(“@DatasourceNameRT”).Read

#1

15分

                     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" });
                    

                            ...
                    

#2
如何用C#读取wincc的变量 仿照VB中引用CCHMIRuntime alt=””>

在这里先谢谢gxingmin 可是貌似还有些小问题 上边调试时的截图 

VB中的代码在VB中已经调试成功

#3
哦,那说明Tags是属性,不是方法

你把System.Reflection.MethodInfo mi = oType.GetMethod(“Tags”);

换成

System.Reflection.MethodInfo mi = oType.GetMethod(“get_Tags”);

试试

#4
调试结果仍然没有变 下边是我在对象管理器里截的图

如何用C#读取wincc的变量 仿照VB中引用CCHMIRuntime alt=””>
如何用C#读取wincc的变量 仿照VB中引用CCHMIRuntime alt=””>

#5
呵呵,那用PropertyInfo试试

                     object HMIRuntime = null;
                    

                            System.Type oType = System.Type.GetTypeFromProgID("CCHMIRuntime.HMIRuntime");
                    

                            HMIRuntime = System.Activator.CreateInstance(oType);
                    

                            System.Reflection.PropertyInfo  pi = oType.GetProperty("Tags");       
                    

                            object tags = pi.GetValue(HMIRuntime, new object[] { "@DatasourceNameRT" });              
                    

                    

                            //g_DataSourceName = HMIRuntime.Tags("@DatasourceNameRT").Read;
#6
后面继续用PropertyInfo得到Read

                      System.Type tType = System.Type.GetTypeFromProgID("CCHMIRuntime.HMITags");
                    

                            System.Reflection.PropertyInfo pit = tType.GetProperty("Read");
                    

                            g_DataSourceName = pit.GetValue(tags);
#7
还是不行 对了  @DatasourceNameRT 是我要读取WINCC实时数据库的变量名 

CCHMIRuntime.HMIRuntime 是wincc实时运行数据库的com控件 是叫控件吧 我不专业

#8
.read 应该并不是VB的.read  而是 HMIRUNTIME的点read
#9
问下 那个wincc变量 是不是读取winccExlporer软件的里的变量? 如果是 采用OPC的方式读取

如果是请回复下我 我有代码 如果不是 你说的这个 我没遇到过 我只能帮顶了

#10
是 亲 就是winccExlporer
#11
 @DatasourceNameRT 读出来的就是WINCC ole DB 要读的 wincc 历史数据库的名称 是经常变的 其实读这个目的就是从WINCC 历史归档里读数据做报表  看来是遇到亲人了 哈哈
#12

25分

                    

                    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.Da.Server server in servers)
                    

                                        {
                    

                                            if (String.Compare(server.Name, "192.168.1.5.OPCServer.WinCC", true) == 0)//为true忽略大小写
                    

                                            {
                    

                                                m_server = server;//建立连接。
                    

                                                break;
                    

                                            }
                    

                                        }
                    

                                    }
                    

                                    m_server.Connect();
                    

                    

                    

                                    state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数
                    

                                    state.Name = "newGroup";//组名
                    

                                    state.ServerHandle = null;//服务器给该组分配的句柄。
                    

                                    state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
                    

                                    state.Active = true;//激活该组。
                    

                                    state.UpdateRate = 100;//刷新频率为1秒。
                    

                                    state.Deadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
                    

                                    state.Locale = null;//不设置地区值。
                    

                    

                                    subscription = (Opc.Da.Subscription)m_server.CreateSubscription(state);//创建组
                    

                                    string[] itemName = new string[4];
                    

                                    itemName[0] = "G1_YM";
                    

                                    itemName[1] = "G2_YM";
                    

                                    itemName[2] = "G3_YM";
                    

                                    itemName[3] = "G4_YM";
                    

                    

                    

                                    Item[] items = new Item[4];
                    

                                    for (int i = 0; i < 4; i++)
                    

                                    {
                    

                                        items[i] = new Item();
                    

                                        items[i].ClientHandle = Guid.NewGuid().ToString();
                    

                                        items[i].ItemPath = null;
                    

                                        items[i].ItemName = itemName[i];
                    

                                    }
                    

                    

                                    subscription.AddItems(items);
                    

                                    ItemValueResult[] values = subscription.Read(subscription.Items);
                    

                                    if (values[0].Quality.Equals(Opc.Da.Quality.Good))
                    

                                    {
                    

                                        string s1 = values[0].Value.ToString();
                    

                                        string s2 = values[1].Value.ToString();
                    

                                        string s3 = values[2].Value.ToString();
                    

                                        string s4 = values[3].Value.ToString();
                    

                                        ExecSQL(s1, s2, s3, s4);
                    

                                    }
                    

                                    subscription.RemoveItems(subscription.Items);
                    

                                    m_server.CancelSubscription(subscription);//m_server前文已说明,通知服务器要求删除组。        
                    

                                    subscription.Dispose();//强制.NET资源回收站回收该subscription的所有资源。  
                    

                                    m_server.Disconnect();//
                    

                                    string str = DateTime.Now.ToString() + "-----------------chenggongle";
                    

                                    dr.WriteLine(str);
                    

                                    dr.Flush();
                    

                                    dr.Close();
                    

                                }
                    

                                catch (Exception ex)
                    

                                {
                    

                    

                                    
                    

                                    string str = DateTime.Now.ToString() + "-----------------" + ex.Message;
                    

                                    dr.WriteLine(str);
                    

                                    dr.Flush();
                    

                                    dr.Close();
                    

                    

                    

                                }
                    

                    

                    

                    

                            }
                    

                            /// <summary>
                    

                            /// 执行SQL语句,更新液位
                    

                            /// </summary>
                    

                            /// <returns></returns>
                    

                            public static void ExecSQL(string s1, string s2, string s3, string s4)
                    

                            {
                    

                                string sql = "";
                    

                                string time = DateTime.Now.ToString();
                    

                                System.Collections.ArrayList ar = new System.Collections.ArrayList();
                    

                                sql = "update xt_tc_TankIn set 当前液位=""" + s1 + """,最后一次更新时间=""" + time + """ where 罐号=""34""";
                    

                                ar.Add(sql);
                    

                                sql = "update xt_tc_TankIn set 当前液位=""" + s2 + """,最后一次更新时间=""" + time + """ where 罐号=""37""";
                    

                                ar.Add(sql);
                    

                                sql = "update xt_tc_TankIn set 当前液位=""" + s3 + """,最后一次更新时间=""" + time + """ where 罐号=""40""";
                    

                                ar.Add(sql);
                    

                                sql = "update xt_tc_TankIn set 当前液位=""" + s4 + """,最后一次更新时间=""" + time + """ where 罐号=""43""";
                    

                                ar.Add(sql);
                    

                                dosoft.DAL.DbHelperSQL.ExecuteSqlTran(ar);
                    

                    

                            }
                    

                        }
                    

                    }
                    

G1_YM 等是变量名称

s1    等就是读取到的数据

项目代码 亲测可用不知道对你有没有帮助

#14
using Opc; 请教 这个是对哪个控件的引用

#15
QQ group 84479667 call me 
#16
请问做过这种程序么: C#获取sql数据库中的数据,然后发送给wincc,我不专业,说的可能比较业余,就是那个意思。
#17
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using CCHMIRUNTIME;

namespace Wincc变量

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            HMIRuntime t = new HMIRuntime();

            t.Tags[“Str_Commication”].Write(textBox1.Text);

            label1.Text = t.Tags[“Str_Commication”].Read();

            

        }

    }

}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何用C#读取wincc的变量 仿照VB中引用CCHMIRuntime
喜欢 (0)
[1034331897@qq.com]
分享 (0)