WTclient创建OPC client方法

1.按照上一篇日志“ OPC客户端设计 ”的方法安装动态链接库及相关的注册工作。

2.包含头文件:

  opcda.h

  opc_ae.h

  WTclientAPI.h

3.添加库文件:

  WTclient.lib (要被加入解决方案)

  WTclient.dll

4.初始化方法:

  WTclientCoInit();

  CoUninitialize();

5.查询所有的OPC server:

 1   //得到OPC server个数
 2     int serverNumber = NumberOfOPCServers(TRUE, "");
 3 
 4     //得到所有OPC server的名字
 5     char buf[100];
 6     CString serverName;
 7     for(int i=0; i < serverNumber; i++)
 8     {
 9         GetOPCServerName (i, buf, 100);
10         serverName = buf;
11         MessageBox(0, serverName, 0);
12     }

6.建立OPC连接:

ConnectionHandle = ConnectOPC ("", serverName, FALSE);

7.断开OPC连接:

DisconnectOPC(ConnectionHandle);
ConnectionHandle = INVALID_HANDLE_VALUE;

8.添加组:

 1 CString groupName = "groupName";
 2     DWORD Rate = 100;
 3     float DeadBand = 100;
 4     HANDLE    hGroup = AddOPCGroup (ConnectionHandle, groupName, &Rate, &DeadBand);
 5 
 6     if (hGroup == INVALID_HANDLE_VALUE)
 7     {
 8         MessageBox("Error in adding group.", "警告", 0);
 9         return;
10     }

9.删除组:

void  RemoveOPCGroup (HANDLE hConnect, HANDLE hGroup);

10.添加项目:

CString itemName = "itemName";
HANDLE itemHandle = AddOPCItem(ConnectionHandle, hGroup, itemName);

11.移除项目:

void  RemoveOPCItem (HANDLE hConnect, HANDLE hGroup, HANDLE hItem);

12.读取项目:

//读取项目
VARIANT *pVar = new VARIANT;
FILETIME *pTimeStamp = new FILETIME;
DWORD *pQuality = new DWORD;
ReadOPCItem (ConnectionHandle, hGroup, itemHandle, pVar, pTimeStamp, pQuality);

13.写入项目

VARIANT newVar;
newVar.dblVal = 3.141592;

WriteOPCItem (ConnectionHandle, hGroup, itemHandle, &newVar, 1);

我的完整示例:

  1 //初始化
  2     WTclientCoInit();
  3 
  4     //得到OPC server个数
  5     int serverNumber = NumberOfOPCServers(TRUE, "");
  6 
  7     //得到所有OPC server的名字
  8     char buf[100];
  9     CString serverName;
 10     vector<CString> serverNames;
 11 
 12     for(int i=0; i < serverNumber; i++)
 13     {
 14         GetOPCServerName (i, buf, 100);
 15         serverName = buf;
 16         serverNames.push_back(serverName);
 17     }
 18 
 19     if(serverNumber == 0)
 20     {
 21         MessageBox("没有可用的OPC服务器", "警告", 0);
 22         return ;
 23     }
 24 
 25     //建立OPC连接
 26     ConnectionHandle = ConnectOPC ("", serverNames[0], TRUE);
 27 
 28     if (ConnectionHandle == INVALID_HANDLE_VALUE)
 29     {
 30         MessageBox("Error in connecting.", "警告", 0);
 31         return;
 32     }
 33 
 34     //得到项目个数
 35     int itemNum = NumberOfOPCItems(ConnectionHandle);
 36 
 37     //得到所有项目名称
 38     char itemBuf[128];
 39     CString iName;
 40     vector<CString> itemNames;
 41     for(int index = 0; index < itemNum; index++)
 42     {
 43         if(GetOPCItemName (ConnectionHandle, index, itemBuf, 128))
 44         {
 45             iName = itemBuf;
 46             itemNames.push_back(iName);
 47         }
 48         else
 49         {
 50             MessageBox("读取item名失败", "警告", 0);
 51             return ;
 52         }
 53 
 54     }
 55 
 56     //得到所有项目属性
 57     for(vector<CString>::iterator iter = itemNames.begin(); iter != itemNames.end(); ++iter)
 58     {
 59         //得到当前项目的属性个数
 60         int propNum = NumberOfItemProperties(ConnectionHandle, *iter);
 61 
 62         for(int i = 0; i < propNum; i++)
 63         {
 64             //得到属性特征
 65             DWORD propertyID = 0;
 66             VARTYPE vt;
 67             BYTE describtion[1024];
 68             GetItemPropertyDescription (ConnectionHandle, i, &propertyID, &vt, describtion, 1024);
 69 
 70             //读取当前属性值
 71             VARIANT value;
 72             ReadPropertyValue (ConnectionHandle, *iter, propertyID, &value);
 73         }
 74     }
 75 
 76     //添加组
 77     CString groupName = "groupName";
 78     DWORD Rate = 1;
 79     float DeadBand = 1;
 80     HANDLE    hGroup = AddOPCGroup (ConnectionHandle, groupName, &Rate, &DeadBand);
 81 
 82     if (hGroup == INVALID_HANDLE_VALUE)
 83     {
 84         MessageBox("Error in adding group.", "警告", 0);
 85         return;
 86     }
 87 
 88     //添加项目
 89     CString itemName = "Bucket Brigade.Boolean";
 90     HANDLE itemHandle = AddOPCItem(ConnectionHandle, hGroup, itemName);
 91 
 92     if (itemHandle == INVALID_HANDLE_VALUE)
 93     {
 94         MessageBox("该项不存在.", "警告", 0);
 95         return;
 96     }
 97 
 98     //得到项目类型
 99     VARTYPE type;
100     DWORD AccessRights;
101     GetOPCItemType(ConnectionHandle, hGroup, "Bucket Brigade.Boolean", &type,  &AccessRights);
102 
103 
104 
105     //读取项目
106     VARIANT *pVar = new VARIANT;
107     FILETIME *pTimeStamp = new FILETIME;
108     DWORD *pQuality = new DWORD;
109     if( !ReadOPCItem (ConnectionHandle, hGroup, itemHandle, pVar, pTimeStamp, pQuality))
110     {
111         MessageBox("读取失败.", "警告", 0);
112         return ;
113     }
114 
115 
116     //写入项目
117     VARIANT newVar;
118     newVar.vt = 4;
119     newVar.fltVal = 3.14;
120     if(!WriteOPCItem (ConnectionHandle, hGroup, itemHandle, &newVar, 1))
121     {
122         MessageBox("写入失败.", "警告", 0);
123         return ;
124     }
125 
126 
127     //读取项目
128     ReadOPCItem (ConnectionHandle, hGroup, itemHandle, pVar, pTimeStamp, pQuality);
原文地址:https://www.cnblogs.com/johnpher/p/2721014.html