简体   繁体   English

远程OPC已连接但无法读取

[英]Remote OPC Connected but can't read

I am writing a custom OPC Client application in C#, enabling reading of data from a RSLinx Server. 我正在用C#编写自定义OPC客户端应用程序,从而可以从RSLinx服务器读取数据。

First of, I wasn't able to connect to the RSLINX Opc Server remotely. 首先,我无法远程连接到RSLINX Opc Server。 Exception Access Denied kept occurring. 异常访问被拒绝不断发生。

I then alteredthe DCOM Settings of MyComputer -> Com Security -> Access Permissions and Launch and Activation Permissions, enabled everything for the User 'Everyone' 然后,我更改了MyComputer的DCOM设置-> COM安全性->访问权限以及启动和激活权限,为用户“所有人”启用了所有功能

This then allowed me to connect, but when It comes to read the server I get the following exception - 然后,这使我可以连接,但是在读取服务器时,出现以下异常-

[System.Runtime.InteropServices.COMException]   {"Exception from HRESULT: 0x80040202"}  System.Runtime.InteropServices.COMException

I have scoured the web as much as I possibly can, and they all boil down to Dcom related issues. 我已尽我所能来搜索网络,它们都归结为Dcom相关问题。 I have simply tried everything with the Dcom Settings. 我只是使用Dcom设置尝试了所有操作。 I have made sure the settings are enabled for both MyComputer and RSLinx server. 我确保已为MyComputer和RSLinx服务器启用了设置。

I am using two .dll files from OPCFoundation - opcNetApi.dll , opcNetApi.Com.dll 我正在使用来自OPCFoundation的两个.dll文件-opcNetApi.dll,opcNetApi.Com.dll

Here is my code, (It may be handy) 这是我的代码,(可能很方便)

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++)
            {


            }

        }

I know for certain that the code works as I have tried building the application connecting as a localhost on the PC I am trying to remote connect to, it reads the data happily. 我确定代码可以正常工作,因为我尝试在要远程连接的PC上构建作为本地主机连接的应用程序,它愉快地读取数据。

Hopefully, someone will have some idea what is going on, I have spent over 12 hours in the last 4 working days trying to work it out! 希望有人会对发生的事情有所了解,在过去的4个工作日中,我花了12个小时来尝试解决问题!

This is working for me: 这为我工作:

_opcServer = new Server(_comFactory, null) { Url = new Opc.URL("opcda://localhost/FactoryTalk Gateway") };
_opcServer.Connect();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM