12/13/2023
Hi everyone,
Â
I'm trying to create a simple OPC server in C#. Problem is that I can access to opc.tcp://localhost:4840 but I don't see folder and variables.
What I'm missing?
Here is the code:
public void StartServer()
{
ApplicationInstance application = new ApplicationInstance();
application.ApplicationName = "MyServer";
application.ApplicationType = ApplicationType.Server;
// load the application configuration.
ApplicationConfiguration config = new ApplicationConfiguration()
{
ApplicationName = "MyServer",
ApplicationType = ApplicationType.Server,
ApplicationUri = "urn:localhost:MyServer",
ServerConfiguration = new ServerConfiguration
{
BaseAddresses = new StringCollection { "opc.tcp://localhost:4840/MyServer" },
SecurityPolicies =
{
new ServerSecurityPolicy()
{
SecurityMode = MessageSecurityMode.None,
SecurityPolicyUri = SecurityPolicies.None,
}
},
},
SecurityConfiguration = new SecurityConfiguration
{
ApplicationCertificate = new CertificateIdentifier { StoreType = "Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\MachineDefault", SubjectName = "MyServer" },
TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\UA Applications" },
RejectedCertificateStore = new CertificateTrustList { StoreType = "Directory", StorePath = @"%CommonApplicationData%\OPC Foundation\CertificateStores\RejectedCertificates" },
AutoAcceptUntrustedCertificates = true,
AddAppCertToTrustedStore = true
},
TransportConfigurations = new TransportConfigurationCollection(),
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 },
TraceConfiguration = new TraceConfiguration()
};
config.Validate(ApplicationType.Server).GetAwaiter().GetResult();
if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
{
config.CertificateValidator.CertificateValidation += (s, e) =>
{
e.Accept = (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted);
};
}
application.ApplicationConfiguration = config;
// check the application certificate.
bool haveAppCertificate = application.CheckApplicationInstanceCertificate(false, 0).Result;
if (!haveAppCertificate)
{
throw new Exception("Application instance certificate invalid!");
}
// start the server.
application.Start(new MySampleServer()).Wait();
Console.ReadLine();
}
}
public class MySampleServer : StandardServer
{
private BaseDataVariableState m_myVariable;
protected override MasterNodeManager CreateMasterNodeManager(IServerInternal server, ApplicationConfiguration configuration)
{
List<INodeManager> nodeManagers = new List<INodeManager>
{
new MyNodeManager(server, configuration)
};
return new MasterNodeManager(server, configuration, null, nodeManagers.ToArray());
}
protected override ServerProperties LoadServerProperties()
{
ServerProperties properties = new ServerProperties
{
ManufacturerName = "My Manufacturer",
ProductName = "My Sample Server",
ProductUri = "http://my.sample.server.com/",
SoftwareVersion = Utils.GetAssemblySoftwareVersion(),
BuildNumber = Utils.GetAssemblyBuildNumber(),
BuildDate = Utils.GetAssemblyTimestamp()
};
return properties;
}
}
public class MyNodeManager : CustomNodeManager2
{
private FolderState myFolder;
private BaseDataVariableState myVariable;
public MyNodeManager(IServerInternal server, ApplicationConfiguration config)
: base(server, config, Namespaces.OpcUa)
{
SystemContext.NodeIdFactory = this;
myFolder = new FolderState(null)
{
SymbolicName = "MyFolder",
NodeId = new NodeId("MyFolder", NamespaceIndex),
BrowseName = new QualifiedName("MyFolder", NamespaceIndex),
DisplayName = new LocalizedText("MyFolder"),
TypeDefinitionId = ObjectTypeIds.FolderType,
EventNotifier = EventNotifiers.None
};
// Create a variable.
myVariable = new BaseDataVariableState(myFolder)
{
SymbolicName = "MyVariable",
NodeId = new NodeId("MyVariable", NamespaceIndex),
BrowseName = new QualifiedName("MyVariable", NamespaceIndex),
DisplayName = new LocalizedText("MyVariable"),
DataType = DataTypeIds.Int32,
ValueRank = ValueRanks.Scalar,
AccessLevel = AccessLevels.CurrentReadOrWrite,
UserAccessLevel = AccessLevels.CurrentReadOrWrite,
Historizing = false,
Value = 123,
TypeDefinitionId = VariableTypeIds.BaseDataVariableType
};
myFolder.AddChild(myVariable);
}
}
1 Guest(s)