西门子OPC UA和.NET C#客户端无法连接到服务器

本文关键字:连接 服务器 客户端 OPC UA NET 西门子 | 更新日期: 2023-09-27 18:32:43

我尝试使用此页面上提供的客户端连接到OPC UA服务器:https://support.industry.siemens.com/cs/document/42014088/programming-an-opc-ua-net-client-with-c%23-for-the-simatic-net-opc-ua-server?dti=0&lc=en-US。使用西门子 OPC Scount v10 连接到 OPC UA 服务器工作正常。使用文章中提供的客户端连接到OPC UA服务器时,我收到以下消息:

无法打开 UA TCP 请求通道。

异常的堆栈跟踪如下:

    Server stack trace: 
   at Opc.Ua.Bindings.UaTcpRequestChannel.OnEndOpen(IAsyncResult result)
   at Opc.Ua.Bindings.UaTcpRequestChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
   at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Opc.Ua.ISessionChannel.CreateSession(CreateSessionMessage request)
   at Opc.Ua.SessionChannel.CreateSession(CreateSessionMessage request)
   at Opc.Ua.SessionClient.CreateSession(RequestHeader requestHeader, ApplicationDescription clientDescription, String serverUri, String endpointUrl, String sessionName, Byte[] clientNonce, Byte[] clientCertificate, Double requestedSessionTimeout, UInt32 maxResponseMessageSize, NodeId& sessionId, NodeId& authenticationToken, Double& revisedSessionTimeout, Byte[]& serverNonce, Byte[]& serverCertificate, EndpointDescriptionCollection& serverEndpoints, SignedSoftwareCertificateCollection& serverSoftwareCertificates, SignatureData& serverSignature, UInt32& maxRequestMessageSize)
   at Opc.Ua.Client.Session.Open(String sessionName, UInt32 sessionTimeout, IUserIdentity identity, IList`1 preferredLocales)
   at Opc.Ua.Client.Session.Open(String sessionName, IUserIdentity identity)

任何帮助将不胜感激。OPC UA服务器运行在Siemens Simatic HMI TP700 Comfort上。OPC UA 服务器上的配置是默认配置。

在 andrewcullen 提示之后,我们在跟踪日志.txt文件中得到以下日志,并在捕获异常时

出错

连接到服务器时发生意外错误。

    PID:4196 ************************* Logging started at 02/03/2016 07:41:34 *************************
4196 - 07:41:38.742 GetEndpoints Called. RequestHandle=1, PendingRequestCount=1
4196 - 07:41:38.992 SECURE CHANNEL CREATED [TcpClientChannel UA-TCP 1.00.238.1] [ID=12752] Connected To: opc.tcp://xxx.xxx.xxx.xxx:4870/
4196 - 07:41:39.008 TCPCLIENTCHANNEL SOCKET CONNECTED: 00000698, ChannelId=12752
4196 - 07:41:39.008 SECURE CHANNEL CREATED [Opc.Ua.ChannelBase WCF Client 1.00.238.1] [ID=] Connected To: opc.tcp://xxx.xxx.xxx.xxx:4870/
4196 - 07:41:39.101 GetEndpoints Completed. RequestHandle=1, PendingRequestCount=0
4196 - 07:41:39.132 TCPCLIENTCHANNEL SOCKET CLOSED: 00000698, ChannelId=12752
4196 - 07:41:44.230 Writing rejected certificate to directory: 
4196 - 07:41:59.694 CreateSession Called. RequestHandle=1, PendingRequestCount=1
4196 - 07:42:13.672 TCPCLIENTCHANNEL SOCKET CLOSED: 000007C0, ChannelId=0
4196 - 07:42:13.750 CreateSession Completed. RequestHandle=1, PendingRequestCount=0

我从西门子官方支持中得到了答案:

该应用程序未使用舒适面板进行测试。例如,代码包含面板服务器不支持的块读取和块写入。 所以这个应用程序将不起作用。

西门子OPC UA和.NET C#客户端无法连接到服务器

这个Siemens UaClient使用了一个库"ClientAPI",它扩展了OPC基金会的Opc.Ua.Core和Opc.Ua.Client。ClientAPI有很多不错的助手函数来简化连接和订阅。但是,我在连接(字符串 URL(的代码中看到它使用的是原始的 WCF 样式通道。堆栈跟踪显示 WCF 类型引发难以诊断的异常。我会改变两件事:

首先配置跟踪以写入文件。在 ClientAPI 中,找到 Helpers.CreateClientConfiguration(( 并添加

// add trace config before calling validate
configuration.TraceConfiguration = new TraceConfiguration {
OutputFilePath="tracelog.txt", 
DeleteOnLoad = true, 
TraceMasks = Utils.TraceMasks.All };
configuration.Validate(ApplicationType.Client);    

其次,升级用于连接的通道类型。在 ClientAPI 中,找到 Server.Connect(字符串 url( 并修改中间,如下所示:

// Initialize the channel which will be created with the server.
// SessionChannel channel = SessionChannel.Create(
//    configuration,
//    endpointDescription,
//    endpointConfiguration,
//    bindingFactory,
//    clientCertificate,
//    null);
ITransportChannel channel = WcfChannelBase.CreateUaBinaryChannel(
    configuration,
    endpointDescription,
   endpointConfiguration,
   clientCertificate,
   configuration.CreateMessageContext());
// Wrap the channel with the session object.
// This call will fail if the server does not trust the client certificate.
// m_Session = new Session(channel, configuration, endpoint);
 m_Session = new Session(channel, configuration, endpoint, clientCertificate);

编辑 2/4:

从跟踪日志中,您可能会发现证书错误。创建新会话时,客户端和服务器都提供并验证彼此的证书。默认情况下,UaClient 正在从 Windows 存储 LocalMachine''My(又名 Personal(中检索其证书。api 在首次运行时生成此证书(这需要以管理员身份首次运行((要查看此证书,请运行"certlm.msc"(。

在服务器计算机上,服务器将通过检查客户端是否与其"TrustedPeerList"中的证书匹配来验证客户端的证书。服务器通常使用目录来存储受信任的证书。 如果客户端证书不受信任,服务器会将客户端的证书复制到"拒绝证书"目录。您需要将您在"拒绝的证书"中找到的证书复制到受信任的证书目录。

返回客户端计算机,客户端将验证服务器的证书。此客户端使用 Windows 存储进行验证"本地计算机''我的"(也称为"个人"。客户端不使用"拒绝"目录,而是注册一个事件处理程序,该处理程序打开一个消息框,询问您是否要接受服务器的证书。如果选择接受,客户端将设置 eventArg e.Accept = true; 要禁止显示消息框,应使用工具"certlm.msc"将服务器的证书导入到客户端的"LocalMachine''My"(也称为"个人"中(。

尝试使用 DNS 名称 ping 服务器。如果服务器无法访问 C:''Windows''System32''drivers''etc... 必须进行编辑。以管理员身份打开记事本,然后打开主机文件并输入 IP 地址到主机名的映射,如下所示:

xxx.xxx.xxx.xxx 主机名