Home > Net >  Just contact with OPC, found that when reading data read values are not complete, have who can help
Just contact with OPC, found that when reading data read values are not complete, have who can help

Time:10-12

using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;
Using OpcRcw.Com n;
Using OPCAutomation;
Using System.Net;

The namespace Demo
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent();
}
# region public variables
//OPC server
Private OPCServer KepServer;
//OPC group collection objects
Private OPCGroups KepGroups;
//OPC group objects
Private OPCGroup KepGroup;
//OPC tags collection objects
Private OPCItems KepItems;
//OPC object tag
Private OPCItem KepItem;
///client handle
Int itemHandleClient=0;
///IP address
String strHostIP;
String TempIP;
//computer name
String strHostName;
//the server name
String cmbServerName="";
///the server handle
Int itemHandleServer=0;
Public object readValue;
//set the server name
Public List ServerName=new List (a);
//value collection
Public List Tags=new List (a);

# endregion
# region enumeration local OPC server
Public void GetOPCServer ()
{
IPHostEntry IpHost=Dns. Resolve (Dns) GetHostName ());
Console. WriteLine (" MAC Address ");
Foreach (IPAddress IP in IpHost. Expressions such as AddressList)
{
Console. WriteLine (IP);
}
Console. WriteLine (" both Please Enter IPHOST ");
String strHostIP="localhost";//the Console. ReadLine ();
IPHostEntry IPHostEntry=Dns. GetHostEntry (strHostIP);
Try
{
KepServer=new OPCServer ();
The object serverList=KepServer. GetOPCServers (ipHostEntry. The HostName. The ToString ());
Int I=0;
Foreach (string serverName (Array) in serverList)
{
Console. WriteLine (i.T oString () + "" + serverName);
ComboBox1. Items. The Add (serverName);
}

}
The catch (Exception ex)
{

Console. WriteLine (" Connect Erro: "+ ex. Message);
}
}
# endregion
# region
//connect the OPCServer
Public void ConnectServer (String serverName)
{
IPHostEntry IPHost=Dns. Resolve (Environment. MachineName);
If (IPHost. Expressions such as AddressList. Length & gt; 0)
{
StrHostIP=IPHost. Expressions such as AddressList [0]. ToString ();
}
Try
{
KepServer. Connect (serverName, "172.31.88.171");
CreateGroup (" ");
CreateItems ();
MessageBox. Show (" connection success ");
}
Catch Exception (err)
{
MessageBox. Show (" enumeration local OPC server error: "+ err. The Message," Message ", MessageBoxButtons. OK, MessageBoxIcon. Warning).
}
}
//create a group collection
OPCGroup [] kepgroup=new OPCGroup [1].
Public void Temp1 (string Temps)
{
Try
{
Int id=1;
KepServer. Connect (comboBox1. Text, strHostIP);//OPC service name node IP
Kepgroup [0]=KepServer. OPCGroups. Add (" Kepware. KEPServerEX. V6. Test ");//group name
Kepgroup [0]. IsActive=true;
Kepgroup [0]. IsSubscribed=true;
Kepgroup [0]. DeadBand=0;
Kepgroup [0]. UpdateRate=1000;//read interval
# test channel region designated
If (Temps=="test")
{
//for processing customer data warehouse data
Kepgroup [0]. OPCItems. AddItem (" test. Test. Tag1 ", id);//bind variables and id for processing warehouse. S7-1215 c. WaitWareHouse. Inventory product fixed number
Id++;//id + 1
Kepgroup [0]. OPCItems. AddItem (" test. Test. Tag2 ", id);//bind variables and id
}
# endregion
The else
{
MessageBox. Show (" fail ");
}
Kepgroup [0]. DataChange +=new DIOPCGroupEvent_DataChangeEventHandler (KepGroup_DataChange);//event binding
}

The catch (Exception ex)
{

MessageBox. Show (" unable to get to the server of the group of variables: "+ ex. The Message," Message ", MessageBoxButtons. OK, MessageBoxIcon. Warning).
}
}
//create a group
Private void CreateGroup (String groupName)
{
Try
{
KepGroups=KepServer. OPCGroups;
KepGroup=KepGroups. Add (groupName);
KepServer. OPCGroups. DefaultGroupIsActive=true;
KepServer. OPCGroups. DefaultGroupDeadband=0;
KepGroup. UpdateRate=250;
KepGroup. IsActive=true;
KepGroup. IsSubscribed=true;
}
The catch (Exception ex)
{

Console. WriteLine (" Connect Erro: "+ ex. Message);
}
}

///& lt; Summary>
///to create a collection of objects
///& lt;/summary>
Public void CreateItems ()
{
KepItems=KepGroup. OPCItems;
KepGroup. DataChange +=new DIOPCGroupEvent_DataChangeEventHandler (KepGroup_DataChange);
}
String Temps="test", Tag1 and Tag2.
int i;
//assigned to the corresponding variable
Private void KepGroup_DataChange (int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
Try
{
For (I=1; I & lt;=NumItems; I++)
{

The object value=https://bbs.csdn.net/topics/ItemValues.GetValue (I);//label value
Int temp1=System. The Convert. ToInt32 (ClientHandles. GetValue (I));//label ID

//Tag1
If (Temps=="test")
{
If (temp1==1)
{
Tag1=value. The ToString ();
}
//Tag2
If (temp1==2)
{
Tag2=value. The ToString ();
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related