代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCSiemensDAAutomation;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OPCServer MyOpcServer;
OPCGroup MyOpcGroup;
OPCItem MyOpcItem1;
OPCItem MyOpcItem2;
int[] ServerHandle = new int[2];
const int READASYNC_ID = 1;
const int WRITEASYNC_ID = 2;
private void Form1_Load(object sender, EventArgs e)
{
}
private void Conn_Click(object sender, EventArgs e)
{
try
{
MyOpcServer = new OPCServer();
MyOpcServer.Connect("OPC.SimaticNet", "192.168.1.96");
MyOpcGroup = MyOpcServer.OPCGroups.Add("MyGroup1");
MyOpcGroup.DeadBand = 0;
MyOpcGroup.UpdateRate = 1000;
MyOpcItem1 = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_2]DB256,X0.1", 1);
MyOpcItem2 = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_2]DB256,X0.0", 2);
ServerHandle[0] = MyOpcItem1.ServerHandle;
ServerHandle[1] = MyOpcItem2.ServerHandle;
MyOpcGroup.AsyncReadComplete += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(MyOpcGroup_ReadComplete);
Console.WriteLine("{0}", MyOpcItem1.Quality);
}
catch(System.Exception error)
{
MessageBox.Show(error.Message,"Result-connect server",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
void MyOpcGroup_CancelComplete(int CancelID)
{
}
private void READ_A_Click(object sender, EventArgs e)
{
int[] handle = new int[3] { ServerHandle[0], ServerHandle[1], 0 };
Array MyServerHandles = (Array)handle;
Array errors;
int cancelID;
try
{
MyOpcGroup.AsyncRead(2, ref MyServerHandles, out errors, READASYNC_ID, out cancelID);
}
catch (System.Exception error)
{
MessageBox.Show(error.Message,"Result-异步读",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
void MyOpcGroup_ReadComplete(int TransactionID, int NumItems, ref System.Array ClientHandles, ref System.Array ItemValues, ref System.Array Qualities, ref System.Array TimeStamps, ref System.Array Errors)
{
try
{
if (TransactionID == READASYNC_ID)
{
if (Convert.ToInt32(ClientHandles.GetValue(1)) == 1)
{
if (Convert.ToInt32(Errors.GetValue(1)) == 0)
{
Txt_Value.Text = ItemValues.GetValue(1).ToString();
Txt_Quality.Text = Qualities.GetValue(1).ToString();
Txt_TimeStamp.Text = TimeStamps.GetValue(1).ToString();
}
}
}
}
catch (System.Exception error)
{
MessageBox.Show(error.Message, "Result-异步读1", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
使用VS2008运行后出现 HRESULT:0x80040200错误
使用路由器以太网连接S7-400
通过设置断点调试发现
对象MyOpcItem1中
EUInfo “this.MyOpcItem1.EUInfo”引发了“System.NotImplementedException”类型的异常 object {System.NotImplementedException}
EUType “this.MyOpcItem1.EUType”引发了“System.NotImplementedException”类型的异常 short {System.NotImplementedException}
Quality 0
Value null
MyOpcServer中
Bandwidth -1
PublicGroupNames “((OPCSiemensDAAutomation.OPCServerClass)(this.MyOpcServer)).PublicGroupNames”引发了“System.NotImplementedException”类型的异常 object {System.NotImplementedException}
不知具体是什么原因,我认为是OPCserver没有连上但找不到原因,初学OPC这方面资料不太好找,希望高手指点,谢谢。