0

I connected to local KepServerEX and now I am trying to connect to remote KepServerEX using c#.

I have configured DCOM for my computer to connect to remote Server, but unfortunately I still can not connect to remote KepServerEx.

I used this command:

KepServer.Connect("KEPware.KEPServerEx.V4", remoteServerIP) 

and configured DCOM for my PC.

This is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    OPCServer KepServer;
    OPCGroups KepGroups;
    OPCGroup KepGroup;
    OPCItems KepItems;
    OPCItem KepItem;
    string strHostIP = "";
    string strHostName = "";
    bool opc_connected = false;
    int itmHandleClient = 0;
    int itmHandleServer = 0;

    private void GetLocalServer()
    {

        IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
        if (IPHost.AddressList.Length > 0)
        {
            strHostIP = IPHost.AddressList[0].ToString();
        }
        else
        {
            return;
        }
        IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
        strHostName = ipHostEntry.HostName.ToString();

        try
        {

            KepServer = new OPCServer();
            object serverList = KepServer.GetOPCServers(strHostName);
            foreach (string turn in (Array)serverList)
            {
                bool bl = turn.Contains("KEPware");
                if (bl == true)
                    cmbServerName.Items.Add(turn);
            }
            cmbServerName.SelectedIndex = 0;
            btnConnLocalServer.Enabled = true;

        }
        catch (Exception)
        {
        }

    }

    private bool CreateGroup()
    {
        try
        {
            KepGroups = KepServer.OPCGroups;
            KepGroup = KepGroups.Add("OPCDOTNETGROUP");
            SetGroupProperty();
            KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
            KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
            KepItems = KepGroup.OPCItems;

        }
        catch (Exception)
        {
            return false;

        }
        return true;
    }

    private void SetGroupProperty()
    {
        KepServer.OPCGroups.DefaultGroupIsActive = true;
        KepServer.OPCGroups.DefaultGroupDeadband = 0;
        KepGroup.UpdateRate = 1000;
        KepGroup.IsActive = true;
        KepGroup.IsSubscribed = true;
    }

    private void GetServerInfo()
    {
        tsslServerStartTime.Text = "Start time:" + KepServer.StartTime.ToString() + "      ";
        tsslversion.Text = "version:" + KepServer.MajorVersion.ToString() + "." + KepServer.MinorVersion.ToString() + "." + KepServer.BuildNumber.ToString();
    }

    private bool ConnectRemoteServer(string remoteServerIP, string remoteServerName)
    {
        try
        {
            KepServer.Connect(remoteServerName, remoteServerIP);
            if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
            {
                tsslServerState.Text = "Is connected to the -" + KepServer.ServerName + " ";
            }
            else
            {
                tsslServerState.Text = "State: " + KepServer.ServerState.ToString() + "     ";

            }

        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        GetLocalServer();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!opc_connected)
        {
            return;
        }

        if (KepGroup != null)
        {
            KepGroup.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
        }

        if (KepServer != null)
        {
            KepServer.Disconnect();
            KepServer = null;
        }

        opc_connected = false;

    }

    private void btnSetGroupPro_Click(object sender, EventArgs e)
    {
        SetGroupProperty();

    }

    private void btnConnLocalServer_Click(object sender, EventArgs e)
    {
        try
        {
            if (!ConnectRemoteServer(txtRemoteServerIP.Text, "KEPware.KEPServerEx.V4"))
            {
                return;
            }

            btnSetGroupPro.Enabled = true;
            opc_connected = true;

            GetServerInfo();

            RecurBrowse(KepServer.CreateBrowser());

            if (!CreateGroup())
            {
                return;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("" + ex);
        }
    }
}
Dzyann
  • 5,062
  • 11
  • 63
  • 95
AJHope
  • 123
  • 2
  • 12
  • Do you get an error or something? Leave only the code relevant to your issue to make it easier for people to help you ( methods where you actually creating the connection. – Dzyann Jul 09 '15 at 04:27
  • I didn't get an error or anything. This is the method that I used to connect to remote Server: KepServer.Connect(remoteServerName, remoteServerIP); – AJHope Jul 09 '15 at 06:55
  • In your ConnectRemoteServer method you have a try, catch, if there is an exception it returns false. Does that line get called? If that doesn't get called what do you have in KepServer.ServerState after calling KepServer.Connect(remoteServerName, remoteServerIP);? – Dzyann Jul 09 '15 at 21:21
  • What method is the one that is failing, in what line? Do you have several empty try catchs that can be catching errors you need to see. Is your GetLocalServer method successful? Does the KepServer get created or the method throws an exception? – Dzyann Jul 09 '15 at 21:38
  • I am a new member on Stackoverflow, so I don't know clearly the rules of Stackoverflow forum. I am really sorry about my mistakes, I deleted my post comment as answer, thanks for Dzyann's advice. – AJHope Jul 10 '15 at 03:36
  • The GetLocalServer method is successful. I had a try, catch and catched errors like this: System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at OPCAutomation.IOPCAutoServer.Connect(String ProgID, Object Node) at OPC_Client.Form1.ConnectRemoteServer(String remoteServerIP, String remoteServerName) in d:\VS 2013\OPC_Client\OPC_Client\Form1.cs:line 137 – AJHope Jul 10 '15 at 03:48
  • The KepServer.Connect(remoteServerName, remoteServerIP) method is failure when I try to connect to remote Server – AJHope Jul 10 '15 at 03:53
  • Does [this post](http://stackoverflow.com/questions/30640942/how-to-connect-and-read-values-from-kepware-using-opcautomation-dll) help you? – Dzyann Jul 11 '15 at 17:05
  • Check [this one also](http://www.codeproject.com/Articles/490072/DA-OPC-Wrapper-DLL-and-Client-Example) – Dzyann Jul 11 '15 at 17:06
  • Thank you so much for your helps. But this articles only show how to connect to local KepServerEX, no remote KepServerEX – AJHope Jul 13 '15 at 02:05
  • feeling like everything is at a standstill ! – AJHope Jul 13 '15 at 02:43
  • I assume that pinging the remote server works, right? I am not expert on this technology I just been helping you format the question better so others could help you. So far is unclear what your issue is. Do you get a timeout maybe? – Dzyann Jul 13 '15 at 04:05
  • Finally I solved my problem. Dzyann, thank you so much for your helps. – AJHope Jul 14 '15 at 08:08
  • My problem is that the difference from user name and password on client and server computer. I created a local user on my client computer that has the same user name and password as the one on the server. Then my problem is resolved – AJHope Jul 14 '15 at 08:53
  • I am glad you could solve your issue! Just to clarify, when you were trying to connect you were probably getting an exception "Authentication Error" or something like that, right? But it was being catched by your code and returning false? If you found how to solve your issue I encourage you to post the answer with some details, so it may help others in the future. – Dzyann Jul 14 '15 at 14:15

1 Answers1

1

Finally I solved my problem. I am very happy to share my issue with you.

First, you use KepServer.Connect(remoteServerName, remoteServerIP); method to connect to remote server.

You have to configure DCOM for your client and server computer. You can refer to link:http://support.sas.com/rnd/itech/doc9/admin_oma/sasserver/comdcom/xpsp2.html.

Important here is that you have to create a local user on my client computer that has the same user name and password as the one on the server.

You can refer to link : http://www.elmajdal.net/win7/how_to_create_a_password_for_a_user_account_in_windows_7.aspx to create user name password.

I hope this could help you.

AJHope
  • 123
  • 2
  • 12