使用C# OPCSiemensDAAutomation通过simaticnet访问PLC的问题

已锁定

OptiD

  • 帖子

    2
  • 精华

    0
  • 被关注

    0

论坛等级:游民

注册时间:2012-05-01

普通 普通 如何晋级?

使用C# OPCSiemensDAAutomation通过simaticnet访问PLC的问题

6304

2

2013-02-20 14:34:14

代码如下:
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这方面资料不太好找,希望高手指点,谢谢。
使用C# OPCSiemensDAAutomation通过simaticnet访问PLC的问题 已锁定
编辑推荐: 关闭

请填写推广理由:

本版热门话题

通信与网络组件

共有4510条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
X 图片
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。