using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpcRcw.Comn;
using OPCAutomation;
using System.Net;
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 公共變數
//OPC服務器
private OPCServer KepServer;
//OPC組集合物件
private OPCGroups KepGroups;
//OPC組物件
private OPCGroup KepGroup;
//OPC標簽集合物件
private OPCItems KepItems;
//OPC標簽物件
private OPCItem KepItem;
/// 客戶端句柄
int itemHandleClient = 0;
///IP地址
string strHostIP;
string TempIP;
//電腦名稱
string strHostName;
// 服務器名稱
String cmbServerName = "";
/// 服務端句柄
int itemHandleServer = 0;
public object readValue;
//服務器名稱集合
public List<String> ServerName = new List<string>();
//值集合
public List<String> Tags = new List<string>();
#endregion
#region 列舉本地OPC服務器
public void GetOPCServer()
{
IPHostEntry IpHost = Dns.Resolve(Dns.GetHostName());
Console.WriteLine("MAC Address");
foreach (IPAddress ip in IpHost.AddressList)
{
Console.WriteLine(ip);
}
Console.WriteLine("Please Enter IPHOST");
String strHostIP = "localhost";//Console.ReadLine();
IPHostEntry ipHostEntry = Dns.GetHostEntry(strHostIP);
try
{
KepServer = new OPCServer();
object serverList = KepServer.GetOPCServers(ipHostEntry.HostName.ToString());
int i = 0;
foreach (string serverName in (Array)serverList)
{
Console.WriteLine(i.ToString() + "." + serverName);
comboBox1.Items.Add(serverName);
}
}
catch (Exception ex)
{
Console.WriteLine("Connect Erro:" + ex.Message);
}
}
#endregion
#region
//連接OPCServer
public void ConnectServer(String serverName)
{
IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[0].ToString();
}
try
{
KepServer.Connect(serverName, "172.31.88.171");
CreateGroup("");
CreateItems();
MessageBox.Show("連接成功");
}
catch (Exception err)
{
MessageBox.Show("列舉本地OPC服務器出錯:" + err.Message, "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//創建組集合
OPCGroup[] kepgroup = new OPCGroup[1];
public void Temp1(string Temps)
{
try
{
int id = 1;
KepServer.Connect(comboBox1.Text,strHostIP);// OPC服務名稱 節點 IP
kepgroup[0] = KepServer.OPCGroups.Add("Kepware.KEPServerEX.V6.測驗");//組名
kepgroup[0].IsActive = true;
kepgroup[0].IsSubscribed = true;
kepgroup[0].DeadBand = 0;
kepgroup[0].UpdateRate = 1000;//讀取間隔
#region 測驗通道定點
if (Temps == "測驗")
{
//待加工倉庫的資料 客戶資料
kepgroup[0].OPCItems.AddItem("測驗.測驗.Tag1", id);//系結變數和id 待加工倉庫.S7-1215C.WaitWareHouse.庫存產品固定編號
id++;//id+1
kepgroup[0].OPCItems.AddItem("測驗.測驗.Tag2", id);//系結變數和id
}
#endregion
else
{
MessageBox.Show("操作失敗");
}
kepgroup[0].DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);//事件系結
}
catch (Exception ex)
{
MessageBox.Show("無法獲取到服務器下的組變數:" + ex.Message, "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//創建組
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;
}
catch (Exception ex)
{
Console.WriteLine("Connect Erro:" + ex.Message);
}
}
/// <summary>
/// 創建物件集合
/// </summary>
public void CreateItems()
{
KepItems = KepGroup.OPCItems;
KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
}
string Temps="測驗",Tag1,Tag2;
int i;
//賦值給對應的變數
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 <= NumItems ; i++)
{
object value = ItemValues.GetValue(i);//標簽值
int temp1 = System.Convert.ToInt32(ClientHandles.GetValue(i));//標簽ID
//Tag1
if (Temps == "測驗")
{
if (temp1 == 1)
{
Tag1 = value.ToString();
}
//Tag2
if (temp1 == 2)
{
Tag2 = value.ToString();
}
timer1.Start();
#endregion
}
else
{
MessageBox.Show("操作失敗");
timer1.Stop();
}
}
}
catch (Exception ex)
{
MessageBox.Show("賦值錯誤:" + ex.Message, "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Warning);
timer1.Stop();
}
}
/// <summary>
/// 創建Tag值
/// </summary>
/// <param name="tagName"></param>
private void GetTagValue(String tagName)
{
try
{
readValue = "";
if (itemHandleClient != 0)
{
Array Errors;
OPCItem bItem = KepItems.GetOPCItem(itemHandleServer);
//注:OPC中以1為陣列的基數
int[] temp = new int[2] { 0, bItem.ServerHandle };
Array serverHandle = (Array)temp;
//移除上一次選擇的項
KepItems.Remove(KepItems.Count, ref serverHandle, out Errors);
}
itemHandleClient = 12345;
KepItem = KepItems.AddItem(tagName, itemHandleClient);
itemHandleServer = KepItem.ServerHandle;
}
catch (Exception ex)
{
itemHandleClient = 0;
Console.WriteLine("Read value erroe:" + ex.Message);
}
}
/// <summary>
/// 寫值
/// </summary>
/// <param name="tagName"></param>
/// <param name="_value"></param>
public void WiteValue(String tagName, object _value)
{
GetTagValue(tagName);
OPCItem bitem = KepItems.GetOPCItem(itemHandleServer);
int[] temp = new int[2] { 1, bitem.ServerHandle };
Array serverHandles = (Array)temp;
object[] valueTemp = new object[2] { "", _value };
Array values = (Array)valueTemp;
Array Errors;
int cancelID;
KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
//KepItem.Write(txtWriteTagValue.Text);//這句也可以寫入,但并不觸發寫入事件
GC.Collect();
}
private void button1_Click_1(object sender, EventArgs e)
{
ConnectServer(comboBox1.Text);
}
private void Form1_Load_1(object sender, EventArgs e)
{
//下拉框控制元件系結服務器名稱
GetOPCServer();
}
private void button2_Click(object sender, EventArgs e)
{
String tagName = "測驗.測驗." + textBox2.Text;
object _value = textBox1.Text;
WiteValue(tagName, _value);
}
private void button4_Click(object sender, EventArgs e)
{
Temp1(Temps);
}
private void timer1_Tick(object sender, EventArgs e)
{
NewMethod();
}
//回圈顯示
private void NewMethod()
{
label1.Text = Tag1;
label1.Text = Tag2;
}
int button = 0;
private void button3_Click(object sender, EventArgs e)
{
//釋放資源
button++;
if (button == 1)
{
KepServer.OPCGroups.RemoveAll();
KepServer.Disconnect();
MessageBox.Show("斷開成功");
button = 0;
}
else
{
MessageBox.Show("已斷開連接");
}
}
}
}
uj5u.com熱心網友回復:
別沉啊,求大佬幫助
uj5u.com熱心網友回復:
具體協議是什么樣的,uj5u.com熱心網友回復:
是指在配置OPC服務器的時候選擇的那個嗎,如果是的話是選的西門子的TCP/IPuj5u.com熱心網友回復:
么有環境不好幫你分析答案,檢查下是不是資料包有特殊字符截斷了或是資料粘包了uj5u.com熱心網友回復:
配置是配置DCOM配置,然后我發現只要多點擊幾次讀取的按鈕,他讀到的值就完整了uj5u.com熱心網友回復:
讀取的資料就是Boolean和QWord型別uj5u.com熱心網友回復:
我覺得你還是看看 他們的協議,如果一致都這樣,我也無法幫到你,如果你只是第一次啟動出現這種,你可以延時下周期。然后再正是采集資料uj5u.com熱心網友回復:
好的,我先去看看uj5u.com熱心網友回復:
能請問一下為什么我讀的時候沒辦法寫入嗎,發現是無法獲得服務端句柄,把讀的關了就可以寫了轉載請註明出處,本文鏈接:https://www.uj5u.com/net/133795.html
標籤:C#
