0

我连接到本地KepServerEX,现在我正在尝试KepServerEX使用 c# 连接到远程。

我已将DCOM我的计算机配置为连接到远程服务器,但不幸的是我仍然无法连接到远程KepServerEx

我使用了这个命令:

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

并为我的 PC 配置了 DCOM。

这是我的代码:

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);
        }
    }
}
4

1 回答 1

1

最后我解决了我的问题。我很高兴与您分享我的问题。

首先,您使用KepServer.Connect(remoteServerName, remoteServerIP);方法连接到远程服务器。

您必须为您的客户端和服务器计算机配置 DCOM。可以参考链接:http://support.sas.com/rnd/itech/doc9/admin_oma/sasserver/comdcom/xpsp2.html

这里重要的是,您必须在我的客户端计算机上创建一个本地用户,该用户与服务器上的用户名和密码相同。

您可以参考链接:http://www.elmajdal.net/win7/how_to_create_a_password_for_a_user_account_in_windows_7.aspx创建用户名密码。

我希望这可以帮助你。

于 2015-07-16T03:18:51.497 回答