对组态王OPC服务器值的读取c#版本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace kingview
{
public partial class Form1 : Form
{
///<summary>
writeline输出数值变量
///与组态王建⽴连接
///每次应⽤程序启动时,必须⽤该函数与组态王建⽴连接
///</summary>
///<param name="node">node为节点(IP),如果是本机,其值为空</param>
///<returns>返回错误码,见附录。</returns>
[DllImport("kingvewcliend.dll")]
public static extern int StartCliend(string node);
///<summary>
/
//得到组态王SDK中列出的项⽬(包括变量及其域)总数
///</summary>
[DllImport("kingvewcliend.dll")]
public static extern int ReadItemNo();
///<summary>
///得到某个项⽬的名称
///<param name="sName">将返回组态王的项⽬的名称</param>
///<param name="wItemId">为⽤户写⼊的其要取的变量的索引号,其为ReadItemNo返回的范围内的某个数</param> ///<returns>返回错误码,见附录</returns>
///</summary>
[DllImport("kingvewcliend.dll")]
//[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
public static extern int GetItemNames(StringBuilder sName, int wItemId);
///<summary>
///将某个项⽬添加到采集列中
///<param name="sRegName">是要加⼊采集的项⽬名</param>
///<param name="TagId">TagId项⽬采集的标识号</param>
///<param name="TagDataType">项⽬的数据类型</param>
///<returns>返回错误码,见附录</returns>
///</summary>
[DllImport("kingvewcliend.dll")]
public static extern int AddTag(string sRegName, ref int  TagId, ref int TagDataType);
///<summary>
/
//向某个项⽬中有应⽤程序向组态王⽅向写数据
///</summary>
///<param name="TagId">为要采集项⽬的标识号</param>
///<param name="bVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param>
///<param name="lVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param>
///<param name="fVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param>
///<param name="sVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param>
///<returns>返回错误码,见附录</returns>
[DllImport("kingvewcliend.dll")]
public static extern int WriteTag(ushort TagId, bool bVal, long lVal, float fVal, ref char sVal);
///<summary>
///从组态王中读某个项⽬的数据
///</summary>
///<param name="TagId">要采集的变量的表⽰号</param>
///<param name="bVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param> ///<param name="lVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param> ///<param name="fVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param> ///<param name="sVal">bVal、lVal、fVal、sVal为设定的数值,⽤户将根据变量的类型设定数值</param> ///<returns>返回错误码,见附录</returns>
[DllImport("kingvewcliend.dll")]
public static extern int ReadTag(int TagId, ref bool bVal, ref long lVal, ref Single fVal, StringBuilder sVal);
///<summary>
///断开与组态王OPC的连接
///</summary>
///<returns>返回错误码,见附录</returns>
[DllImport("kingvewcliend.dll")]
public static extern int StopCliend();
//        错误码含义
//0
//连接成功
//-1
//OPC SERVER已经被⾮法关闭
/
/-2
//不到OPC SERVER的PROGID
//-3
//连接OPC SERVER不成功
//-4
//枚举ITEMS错误
//-5
//OPC SERVER没有定义ITEMS
//-6
//内存分配错误
//-7
/
/在向GROUP中加⼊ITEMS时出现错误
//-8
//未使⽤
//-9
//读ITEMS时出现错误
//-10
//不能识别的数据类型
//-11
//读ITEMS的质量戳时出现错误
//-12
//向ITEMS中写⼊数据时出现错误
/
/-13
//⽤户添加变量的变量名错误
//-14
//⽤户读取的变量序号越界
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("连接到本地服务器···");
StartCliend("");
Console.WriteLine("得到项的个数");
int itemCount = ReadItemNo();
Console.WriteLine("得到项的个数为:"+itemCount.ToString());
StringBuilder regname = new StringBuilder(256);
string[] name = new string[itemCount];
Console.WriteLine("得到所有的Item····");
for (int i = 0; i < itemCount - 1; i++)
{
GetItemNames(regname, i);
name[i] = regname.ToString();
}
Console.WriteLine("输出所有的Item····");
for (int i = 0; i < itemCount - 1; i++)
{
Console.WriteLine(name[i].ToString());
}
int[] tagId =new int[itemCount];
int[] tagType =new int[itemCount];
Console.WriteLine("开始将要监控的Item添加····");
for (int i = 0; i < itemCount - 1; i++)
{
AddTag(name[i].ToString(), ref tagId[i], ref tagType[i]);
}
bool[] bVal = new bool[itemCount];
long[] lVal = new long[itemCount];
Single[] fVal = new Single[itemCount];
StringBuilder sVal =new StringBuilder(255);
string[] sValString = new string[itemCount];
Console.WriteLine("开始读取数据····");
for (int x = 1; x <5; x++)
{
Console.WriteLine("第"+x.ToString()+"次读取数据:");
for (int i = 0; i < itemCount - 1; i++)
{
ReadTag(tagId[i], ref bVal[i], ref lVal[i], ref fVal[i], sVal);
sValString[i] = sVal.ToString();
Console.WriteLine("item:" + name[i].ToString() + "值:" + sValString[i].ToString());                }
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。