java 模拟ua,使用OPCFoundation/UA-Java项目工作OPC UA Simulator示例

我使用org.opcfoundation.ua.examples.SampleClient实用程序检查连接和样本值,但无法这样做。如果任何人有工作考试w.r.t请与代码一起分享。一旦这个工作,我需要在apache Nifi中配置这个设置来制作数据管道。

代码:

public class SampleClient {

public static final Locale ENGLISH = Locale.ENGLISH;

public static final Locale ENGLISH_FINLAND = new Locale("en", "FI");

public static final Locale ENGLISH_US = new Locale("en", "US");

public static final Locale FINNISH = new Locale("fi");

public static final Locale FINNISH_FINLAND = new Locale("fi", "FI");

public static final Locale GERMAN = Locale.GERMAN;

public static final Locale GERMAN_GERMANY = new Locale("de", "DE");

public static void main(String[] args)

throws Exception {

// if (args.length==0) {

// System.out.println("Usage: SampleClient [server uri]");

// return;

// }

//String url = /*args[0]*/"opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer";

//String url = /*args[0]*/"opc.tcp://uademo.prosysopc.com:53530";

//String url = /*args[0]*/"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";

String url="opc.tcp://mfactorengineering.com:4840";

//String url="opc.tcp://commsvr.com:51234/UA/CAS_UA_Server";

//String url="opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer";

//String url="opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";

//String url="opc.tcp://opcua.demo-this.com:51211/UA/SampleServer";

//String url="opc.tcp://opcua.demo-this.com:51212/UA/SampleServer";

//String url="opc.tcp://demo.ascolab.com:4841";

//String url="opc.tcp://alamscada.dynu.com:4096";

System.out.print("SampleClient: Connecting to "+url+" .. ");

// CLIENT //

// Create Client

Application myApplication = new Application();

Client myClient = new Client(myApplication);

myApplication.addLocale(ENGLISH);

myApplication.setApplicationName(new LocalizedText("Java Sample Client", Locale.ENGLISH));

myApplication.setProductUri("urn:JavaSampleClient");

CertificateUtils.setKeySize(1024); // default = 1024

KeyPair pair = ExampleKeys.getCert("SampleClient");

myApplication.addApplicationInstanceCertificate(pair);

// The HTTPS SecurityPolicies are defined separate from the endpoint securities

myApplication.getHttpsSettings().setHttpsSecurityPolicies(HttpsSecurityPolicy.ALL);

// Peer verifier

myApplication.getHttpsSettings().setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

myApplication.getHttpsSettings().setCertificateValidator(CertificateValidator.ALLOW_ALL);

// The certificate to use for HTTPS

KeyPair myHttpsCertificate = ExampleKeys.getHttpsCert("SampleClient");

myApplication.getHttpsSettings().setKeyPair(myHttpsCertificate);

// Connect to the given uri

SessionChannel mySession = myClient.createSessionChannel(url);

// mySession.activate("username", "123");

mySession.activate();

//

/ EXECUTE //

// Browse Root

BrowseDescription browse = new BrowseDescription();

browse.setNodeId(Identifiers.RootFolder);

browse.setBrowseDirection(BrowseDirection.Forward);

browse.setIncludeSubtypes(true);

browse.setNodeClassMask(NodeClass.Object, NodeClass.Variable);

browse.setResultMask(BrowseResultMask.All);

BrowseResponse res3 = mySession.Browse(null, null, null, browse);

System.out.println(res3);

// Read Namespace Array

ReadResponse res5 = mySession.Read(

null,

null,

TimestampsToReturn.Neither,

new ReadValueId(Identifiers.Server_NamespaceArray, Attributes.Value, null, null)

);

String[] namespaceArray = (String[]) res5.getResults()[0].getValue().getValue();

System.out.println(Arrays.toString(namespaceArray));

// Read a variable

ReadResponse res4 = mySession.Read(

null,

500.0,

TimestampsToReturn.Source,

new ReadValueId(new NodeId(6, 1710), Attributes.Value, null, null)

);

System.out.println(res4);

res4 = mySession.Read(

null,

500.0,

TimestampsToReturn.Source,

new ReadValueId(new NodeId(6, 1710), Attributes.DataType, null, null)

);

System.out.println(res4);

/ SHUTDOWN /

mySession.close();

mySession.closeAsync();

//

}

}

例外:

SampleClient: Connecting to opc.tcp://mfactorengineering.com:4840 .. 2017-07-20 11:24:34,909 [main] INFO CryptoUtil - SecurityProvider initialized from org.bouncycastle.jce.provider.BouncyCastleProvider

2017-07-20 11:24:34,909 [main] INFO CryptoUtil - Using SecurityProvider BC

2017-07-20 11:24:35,549 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connecting

2017-07-20 11:24:36,142 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connected

2017-07-20 11:24:36,753 [main] INFO SecureChannelTcp - 1804305022 Closed

2017-07-20 11:24:36,768 [TcpConnection/Read] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Closed (expected)

2017-07-20 11:24:36,768 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Closed

2017-07-20 11:24:36,768 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connecting

2017-07-20 11:24:37,408 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connect failed

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:209)

at java.net.SocketInputStream.read(SocketInputStream.java:141)

at java.net.SocketInputStream.read(SocketInputStream.java:223)

at org.opcfoundation.ua.utils.bytebuffer.InputStreamReadable._get(InputStreamReadable.java:53)

at org.opcfoundation.ua.utils.bytebuffer.InputStreamReadable.getInt(InputStreamReadable.java:144)

at org.opcfoundation.ua.transport.tcp.io.TcpConnection.open(TcpConnection.java:500)

at org.opcfoundation.ua.transport.tcp.io.SecureChannelTcp.open(SecureChannelTcp.java:565)

at org.opcfoundation.ua.application.Client.createSecureChannel(Client.java:641)

at org.opcfoundation.ua.application.Client.createSecureChannel(Client.java:555)

at org.opcfoundation.ua.application.Client.createSessionChannel(Client.java:370)

at org.opcfoundation.ua.application.Client.createSessionChannel(Client.java:345)

at org.opcfoundation.ua.examples.SampleClient.main(SampleClient.java:120)

2017-07-20 11:24:37,464 [main] WARN SecureChannelTcp - Connection failed: Bad_CommunicationError (code=0x80050000, description="2147811328, Connection reset")

2017-07-20 11:24:37,464 [main] WARN SecureChannelTcp - Bad_CommunicationError: Retrying

2017-07-20 11:24:37,464 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connecting

2017-07-20 11:24:38,056 [main] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Connected

2017-07-20 11:24:38,368 [TcpConnection/Read] WARN TcpConnection - mfactorengineering.com/184.173.118.46:4840 Error

org.opcfoundation.ua.common.ServiceResultException: Bad_SecurityChecksFailed (code=0x80130000, description="An error occurred verifying security")

at org.opcfoundation.ua.transport.tcp.io.TcpConnection$ReadThread.run(TcpConnection.java:782)

2017-07-20 11:24:38,368 [TcpConnection/Read] INFO TcpConnection - mfactorengineering.com/184.173.118.46:4840 Closed

Exception in thread "main" org.opcfoundation.ua.common.ServiceResultException: Bad_SecurityChecksFailed (code=0x80130000, description="An error occurred verifying security")

at org.opcfoundation.ua.transport.tcp.io.TcpConnection$ReadThread.run(TcpConnection.java:782)

+0

PROSYS树莓裨演示天气:opc.tcp://212.68.9.58:48401 –

+0

@ CamilleG。你好,我已经取代了opc.tcp://212.68.9.58:48401网址在SampleClient.java上面贴,我得到这个例外Bad_SecurityChecksFailed(代码= 0x80130000,说明=“Bad_SecurityChecksFailed(代码= 0x80130000,说明=”发生错误验证安全性。“)”)。请问您可以检查SampleClient java程序,看看我是否遗漏了任何东西 –

+0

看起来与OPC UA证书有关的错误。最好的办法是下载一个OPC UA服务器并在本地PC上运行,然后连接到您的JAVA程序。 (一个来自统一自动化应该没问题:https://www.unified-automation.com/downloads/opc-ua-servers.html) –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值