You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
2.9 KiB
148 lines
2.9 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
|
|
namespace MasterClient.Client
|
|
{
|
|
class OpcUaMaster : ICommClient
|
|
{
|
|
private readonly string ENDPOINT_TMPL = "opc.tcp://{0}:{1}";
|
|
private Session session;
|
|
|
|
private string ip;
|
|
private int port;
|
|
|
|
public void Connect()
|
|
{
|
|
try
|
|
{
|
|
if (GetStatus())
|
|
Disconnect();
|
|
|
|
ConfiguredEndpoint endPoint = new ConfiguredEndpoint(null, new EndpointDescription(CreateEndpointURL()));
|
|
|
|
ApplicationConfiguration config = CreateOpcUaConfig();
|
|
|
|
session = Session.Create(config, endPoint, true, "MySession", 1000, null, null).Result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if (!GetStatus())
|
|
throw new Exception("Not connected");
|
|
|
|
try
|
|
{
|
|
session.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public bool GetStatus()
|
|
{
|
|
return session != null && session.Connected;
|
|
}
|
|
|
|
public string ReadValue(string name)
|
|
{
|
|
if (!GetStatus())
|
|
throw new Exception("Not connected");
|
|
|
|
try
|
|
{
|
|
return session.ReadValue(name).Value.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public void SetServer(string ip, int port)
|
|
{
|
|
this.ip = ip;
|
|
this.port = port;
|
|
}
|
|
|
|
public void WriteValue(string name, string value)
|
|
{
|
|
if (!GetStatus())
|
|
throw new Exception("Not connected");
|
|
|
|
try
|
|
{
|
|
WriteValue nodeToWrite = new WriteValue()
|
|
{
|
|
NodeId = new NodeId(name),
|
|
AttributeId = Attributes.Value,
|
|
Value = new DataValue()
|
|
};
|
|
|
|
Variant writeValue = new Variant(value);
|
|
nodeToWrite.Value.WrappedValue = writeValue;
|
|
|
|
WriteValueCollection nodesToWrite = new WriteValueCollection();
|
|
nodesToWrite.Add(nodeToWrite);
|
|
|
|
StatusCodeCollection results = null;
|
|
DiagnosticInfoCollection diagnosticInfos = null;
|
|
|
|
ResponseHeader responseHeader = session.Write(
|
|
null,
|
|
nodesToWrite,
|
|
out results,
|
|
out diagnosticInfos);
|
|
|
|
ClientBase.ValidateResponse(results, nodesToWrite);
|
|
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToWrite);
|
|
|
|
if (StatusCode.IsBad(results[0]))
|
|
throw ServiceResultException.Create(results[0], 0, diagnosticInfos, responseHeader.StringTable);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
private ApplicationConfiguration CreateOpcUaConfig()
|
|
{
|
|
ApplicationConfiguration config = new ApplicationConfiguration()
|
|
{
|
|
ApplicationName = "OPC-UA-Client",
|
|
ApplicationType = ApplicationType.Client,
|
|
|
|
SecurityConfiguration = new SecurityConfiguration()
|
|
{
|
|
ApplicationCertificate = new CertificateIdentifier()
|
|
},
|
|
|
|
ClientConfiguration = new ClientConfiguration()
|
|
{
|
|
DefaultSessionTimeout = 30 * 1000
|
|
}
|
|
};
|
|
|
|
config.Validate(ApplicationType.Client);
|
|
|
|
return config;
|
|
}
|
|
|
|
private string CreateEndpointURL()
|
|
{
|
|
return string.Format(ENDPOINT_TMPL, ip, port);
|
|
}
|
|
}
|
|
}
|
|
|