Reputation: 123
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);
}
}
}
Upvotes: 0
Views: 2898
Reputation: 123
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.
Upvotes: 1