代碼連接服務器成功,可以讀取到服務器節點,但是呼叫SyncRead讀取資料的方法時報錯
找了好多資料都找不到錯誤點在哪,求各位大神支招。附代碼public void kepProcess()
{
KepGroup = KepGroups.Add("Group0");
KepGroup.UpdateRate = 250;
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true;
//當KepGroup中資料發生改變的觸發事件
KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
KepItems = KepGroup.OPCItems;
int[] temp = new int[2];
temp[0] = 0;
KepItems.AddItem("Random Int1", 1);
OPCItem bItem = KepItems.Item(1);
Array serverHandles = (Array)temp;
Array values;
Array Errors;
Object cancel;
Object Qualities;
KepGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCCache, serverHandles.Length, ref serverHandles, out values, out Errors, out Qualities, out cancel);
}
uj5u.com熱心網友回復:
serverHandles要賦值好像。uj5u.com熱心網友回復:
我上面不是賦值了嗎uj5u.com熱心網友回復:
從1開始啊
uj5u.com熱心網友回復:
我寫過類似的,但是沒用到你那個代碼..注冊完datachange之后 就可以了啊 你后面的方法是干嘛的?
uj5u.com熱心網友回復:
菜鳥一枚,學習啦uj5u.com熱心網友回復:
private void OPCUAStart_Click(object sender, EventArgs e){
//KepServer = new OPCServer();
_OpcTaskEnd = false;
try{
KepServer.Connect("Techmation.OPC.Server.V1"); //需要修改設定OPCDA服務器名 //KEPware.KEPServerEx.V4
if (KepServer.ServerState == (int)OPCServerState.OPCRunning){
//海天OPC初始化,需要 KepServer連接上服務器才能初始化
InitializeOPC();
OPCUAStatusTxt.Text = "已連接上";
OPUDAStartBt.Enabled = false;
OPCUAStopBt.Enabled = true;
Task.Run(() =>{
while (_OpcTaskEnd == false){
try{
//讀OPC變數
//批量讀
KepGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCCache, 27, ServerHandles, out Values, out Errors, out Qualities, out TimeStamps);
//更新HMI界面
Invoke(new Action(() =>{
..................
..................
..............
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message );
if (_OpcTaskEnd == true) return;
}
}));
Thread.Sleep(500);
}
catch (Exception)
{
}
}
});
}
else
{
OPCUAStatusTxt.Text = "未連接";
OPUDAStartBt.Enabled = true;
OPCUAStopBt.Enabled = false;
}
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
private void InitializeOPC(){
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add("Group1"); //需要修改設定組名
KepGroup.UpdateRate = 250;
KepGroup.IsActive = true;
KepGroup.IsSubscribed = false;
KepItems = KepGroup.OPCItems;
//string OpcDaPath = System.Configuration.ConfigurationManager.AppSettings["HT_OPC_IP"]; //"192_168_100_1."; //需要修改變數前的路徑名 // "Channel1.Device1.Group1.";
ServerHandles[1] = KepItems.AddItem(OpcDaPath + "tmTemp1_Current", 1).ServerHandle; //需要修改設定變數名
ServerHandles[2] = KepItems.AddItem(OpcDaPath + "tmTemp2_Current", 2).ServerHandle;
//設定的變數名要和設備的一樣的 OpcDaPath 是IP地址 這樣獲取對應設備下的資料
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;
...................
.....................
.............................
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/100756.html
標籤:C#
