Creating OPC server is not working as expected|Classic OPC: DA, A&E, HDA, XML-DA, etc.|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
Creating OPC server is not working as expected
Avatar
Josip
New Member
Members
Forum Posts: 1
Member Since:
12/13/2023
sp_UserOfflineSmall Offline
1
12/13/2023 - 06:56
sp_Permalink sp_Print

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

Avatar
Randy Armstrong
Admin
Forum Posts: 1568
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 33
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1435
Posts: 4862