【发布时间】:2015-07-30 05:37:37
【问题描述】:
我正在用 C# 编写一个自定义 OPC 客户端应用程序,可以从 RSLinx 服务器读取数据。
首先,我无法远程连接到 RSLINX Opc 服务器。 Exception Access Denied 不断发生。
然后我更改了 MyComputer 的 DCOM 设置 -> Com 安全 -> 访问权限以及启动和激活权限,为用户“所有人”启用了一切
这然后允许我连接,但是当它读取服务器时,我得到以下异常 -
[System.Runtime.InteropServices.COMException] {"Exception from HRESULT: 0x80040202"} System.Runtime.InteropServices.COMException
我已经尽可能多地搜索了网络,它们都归结为与 Dcom 相关的问题。我只是用 Dcom 设置尝试了一切。我已确保为 MyComputer 和 RSLinx 服务器启用了设置。
我正在使用来自 OPCFoundation 的两个 .dll 文件 - opcNetApi.dll 、 opcNetApi.Com.dll
这是我的代码,(可能很方便)
private void readplc()
{
Opc.URL url = new Opc.URL("opcda://48.5.0.05/RSLinx OPC Server");
Opc.Da.Server server = null;
OpcCom.Factory fact = new OpcCom.Factory();
server = new Opc.Da.Server(fact, null);
try
{
server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
}
catch (Exception exy)
{
MessageBox.Show(exy.Message);
}
// Create a group
Opc.Da.Subscription group;
Opc.Da.SubscriptionState groupState = new Opc.Da.SubscriptionState();
groupState.Name = "Group";
groupState.Active = true;
group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
// add items to the group.
Opc.Da.Item[] items = new Opc.Da.Item[6];
items[0] = new Opc.Da.Item();
items[0].ItemName = "[ALARM]F20:9";
items[1] = new Opc.Da.Item();
items[1].ItemName = "[ALARM]F22:30";
items[2] = new Opc.Da.Item();
items[2].ItemName = "[ALARM]F22:6";
items[3] = new Opc.Da.Item();
items[3].ItemName = "[ALARM]F18:8";
items[4] = new Opc.Da.Item();
items[4].ItemName = "[ALARM]F22:32";
items[5] = new Opc.Da.Item();
items[5].ItemName = "[ALARM]F22:5";
items = group.AddItems(items);
try
{
group.DataChanged += new Opc.Da.DataChangedEventHandler(OnTransactionCompleted); // COM EXCEPTION THROWN HERE
Console.ReadLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Console.ReadKey();
}
}
private void OnTransactionCompleted(object group, object hReq, Opc.Da.ItemValueResult[] items)
{
for (int i = 0; i < items.GetLength(0); i++)
{
}
}
我确定代码可以正常工作,因为我尝试在我尝试远程连接的 PC 上构建连接为本地主机的应用程序,它可以愉快地读取数据。
希望有人知道发生了什么,在过去的 4 个工作日中,我花了超过 12 个小时试图解决这个问题!
【问题讨论】: