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

标签 c# .net opc opc-ua

我尝试使用此页面上提供的客户端连接到 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 .使用 Siemens OPC Scount v10 连接到 OPC UA 服务器工作正常。使用文章中提供的客户端连接到 OPC UA 服务器时,我收到此消息:

Could not open UA TCP request channel.

异常的堆栈跟踪是这样的:

    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 提示之后,我们在 tracelog.txt 文件中得到以下日志,并且在捕获异常时出错

An unexpected error occurred while connecting to the server.

    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

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

The application was not tested with Comfort Panel. The code e.g. contains Block Read and Block Write which is not supported from the Panel Server. So this application will not work.

最佳答案

此 Siemens UaClient 使用库“ClientAPI”,它扩展了 OPC Foundation 的 Opc.Ua.Core 和 Opc.Ua.Client。 ClientAPI 有很多很好的帮助函数来简化连接和订阅。但是,我在 Connect(string Url) 的代码中看到它使用的是原始 WCF 样式的 channel 。并且您的堆栈跟踪显示 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);    

其次,升级用于连接的 channel 类型。在ClientAPI中,找到Server.Connect(string 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:

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

在服务器机器上,服务器将验证客户端的证书,检查它是否匹配其“TrustedPeerList”中的证书。服务器通常使用一个目录来存储受信任的证书。如果客户端证书不受信任,服务器会将客户端的证书复制到“RejectedCertificates”目录。您需要将在“RejectedCertificates”中找到的证书复制到受信任的证书目录。

回到客户端机器上,客户端将验证服务器的证书。此客户端使用 Windows 商店来验证“LocalMachine\My”(又名个人)。 客户端不使用“拒绝”目录,而是注册一个打开消息框的事件处理程序,询问您是否希望接受服务器的证书。如果选择接受,客户端设置 eventArg e.Accept = true;要抑制消息框,应使用工具“certlm.msc”将服务器的证书导入客户端的“LocalMachine\My”(又名个人)。

关于c# - 西门子 OPC UA 和 .NET C# 客户端无法连接到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35134614/

相关文章:

c# - SOAP 是简单对象访问协议(protocol)还是面向服务的应用程序平台?

.net - 如何在 WCF 中设置安全 token ?

c# - Html.DropDownList 所选值不起作用(将构造函数与 IEnumerable<SelectListItem> 一起使用)

c# - INSERT 语句中的列数多于 VALUES 子句中指定的值?

c# - 如何从 C++ dll 在 C# 函数中传递大小未知的数组?

java - 如何通过Utgard批量访问OPC DA服务器数据(一次100个信号)?

OPC UA 唯一 ID

C# Collection 选择属性的值与另一个属性的最小值

c# - 尝试浏览信号器/集线器时 HTTP 503 服务不可用

c++ - 使用自定义标识调用 QueryInterface