Home > Net >  C # through OPCAutomation development OPC
C # through OPCAutomation development OPC

Time:09-22

Code to connect server is successful, can be read to the server nodes, but call SyncRead method of data read times wrong where to look a lot of information can't get a wrong point, please everyone a great god action, attached code
Public void kepProcess ()
{
KepGroup=KepGroups. Add (" Group0 ");
KepGroup. UpdateRate=250;
KepGroup. IsActive=true;
KepGroup. IsSubscribed=true;

//when KepGroup triggering event data change in
KepGroup. DataChange +=new DIOPCGroupEvent_DataChangeEventHandler (KepGroup_DataChange);
KepItems=KepGroup. OPCItems;
Int [] temp=new int [2].
\ [0]=0;
KepItems. AddItem (" Random Int1 ", 1);
OPCItem bItem=KepItems. Item (1);
Array serverHandles=temp (Array);
An Array of values.
Array Errors;
Object the cancel;
The Object Qualities;
KepGroup. SyncRead ((short) OPCAutomation. OPCDataSource. OPCCache, serverHandles. Length, ref serverHandles, out values, out Errors, out Qualities, out cancel);
}

CodePudding user response:

ServerHandles assignment as,

CodePudding user response:

I am not on assignment

CodePudding user response:

refer to the second floor qq_34280999 response:
I am not on assignment?

Starting from 1

CodePudding user response:

I have written a similar, but useless to you the code..

After registered datachange can ah method is behind you?

CodePudding user response:

Rookie a, learning

CodePudding user response:


Private void OPCUAStart_Click (object sender, EventArgs e) {
//KepServer=new OPCServer ();
_OpcTaskEnd=false;
Try {
KepServer. Connect (" Techmation, OPC Server. V1 ");//need to modify the Settings OPCDA server name//KEPware KEPServerEx. V4
If (KepServer. ServerState==(int) OPCServerState. OPCRunning) {
//Haitian OPC initialization, need KepServer connected to the server can initialize
InitializeOPC ();
OPCUAStatusTxt. Text="connected";
OPUDAStartBt. Enabled=false;
OPCUAStopBt. Enabled=true;
Task. Run (()=& gt; {
While (_OpcTaskEnd==false) {
Try {
//read OPC variable
//bulk read
KepGroup. SyncRead ((short) OPCAutomation. OPCDataSource. OPCCache, 27, ServerHandles, out Values, out Errors, out Qualities, out TimeStamps);
//update the HMI interface
Invoke (new Action (()=& gt; {

.
.
.
}
The catch (Exception ex1)
{
MessageBox. Show (ex1. Message);
If (_OpcTaskEnd==true) return;
}

}));
Thread.sleep (500);
}
The catch (Exception)
{


}
}
});

}
The else
{
OPCUAStatusTxt. Text="not connected";
OPUDAStartBt. Enabled=true;
OPCUAStopBt. Enabled=false;
}
}
The catch (Exception ex1)
{
MessageBox. Show (ex1. Message);
}
}








Private void InitializeOPC () {
KepGroups=KepServer. OPCGroups;
KepGroup=KepGroups. Add (" Group1 ");//need to modify the Settings group name
KepGroup. UpdateRate=250;
KepGroup. IsActive=true;
KepGroup. IsSubscribed=false;
KepItems=KepGroup. OPCItems;
//string OpcDaPath=System. The Configuration. ConfigurationManager. The AppSettings (" HT_OPC_IP ");//"192 _168_100_1.";//need to modify the variable path name//in front of the "Channel1. Device1. Group1.";
ServerHandles. [1]=KepItems AddItem (OpcDaPath + "tmTemp1_Current", 1). ServerHandle;//need to modify the set the variable name
ServerHandles. [2]=KepItems AddItem (OpcDaPath + "tmTemp2_Current", 2) ServerHandle;
//set the variable name like OpcDaPath and equipment is the IP address of data so that to obtain the corresponding equipment
ServerHandles. [3]=KepItems AddItem (OpcDaPath + "tmTemp3_Current", 3). ServerHandle;
ServerHandles. [4]=KepItems AddItem (OpcDaPath + "tmTemp4_Current", 4). ServerHandle;
ServerHandles. [5]=KepItems AddItem (OpcDaPath + "tmTemp5_Current", 5). ServerHandle;
ServerHandles. [6]=KepItems AddItem (OpcDaPath + "tmChargePress1", 6). ServerHandle;
.
.
.


}
  •  Tags:  
  • C#
  • Related