工控超實用的OPCDA客戶端上位機編寫

1、軟件實現根據IP地址獲取主機名,然後獲取OPC服務器,連接服務器後獲OPC服務器上的所有節點,雙擊某個節點標籤,在下面可以訂閱標籤的值,點擊某個標籤,右鍵點擊可以更改寫入值:

工控超實用的OPCDA客戶端上位機編寫


工控超實用的OPCDA客戶端上位機編寫

2、軟件功能演示:

工控超實用的OPCDA客戶端上位機編寫

3、部分代碼如下:

/// <summary>

/// 根據IP地址獲取DNS主機名

///

/// <param>

/// <param>

private void button1_Click(object sender, EventArgs e)

{

try

{

IPHostEntry iPHostEntry = Dns.GetHostEntry(textBox1.Text);

textBox2.Text = iPHostEntry.HostName;

}

catch(Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

/// <summary>

/// 獲取OPC服務器

///

/// <param>

/// <param>

private void button2_Click(object sender, EventArgs e)

{

try

{

if (textBox2.Text != "")

{

kepServer = new OPCServer();

//獲取指定DNS主機名上的OPC服務器

object serverList = kepServer.GetOPCServers(textBox2.Text);

comboBox1.Items.Clear();

foreach (string serverName in (Array)serverList)

{

if (!comboBox1.Items.Contains(serverName))

{

comboBox1.Items.Add(serverName);

}

}

}

else

{

MessageBox.Show("請先獲取DNS主機名");

}

}

catch(Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

/// <summary>

/// 連接OPC服務器

///

/// <param>

/// <param>

private void button3_Click(object sender, EventArgs e)

{

listBox1.Items.Clear();

bool connFlag= ConnectServer(comboBox1.Text,textBox2.Text);

if(connFlag==true)

{

button3.Text = "已連接OPC";

}

GetOPC_Info();

kepBrowser = kepServer.CreateBrowser();

CreateKepBrowser(kepBrowser);

CreateGroup();

}

/// <summary>

/// 連接OPC服務器,並返回是否連接成功

///

/// <param>

/// <param>

/// <returns>

public bool ConnectServer(string serverName,String ServerNode)

{

try

{

kepServer.Connect(serverName, ServerNode);

//判斷OPC服務器的連接狀態

if (kepServer.ServerState == (int)OPCServerState.OPCRunning)

{

toolStripStatusLabel1.Text = "已連接到服務器:" + kepServer.ServerName + " ";

return true;

}

else

{

toolStripStatusLabel1.Text= "狀態:" + kepServer.ServerState.ToString() + " ";

return false;

}

}

catch

{

return false;

}

}

4、完整代碼較長,如需要完整代碼可先關注並留言,然後私信我發送“OPCDA”即可自動回覆,如果能幫助到你,感謝你的關注訂閱,可以第一時間接收後續更新。


分享到:


相關文章: