OPC-UA open62541 C++测试代码

初级代码游戏的专栏介绍与文章目录-CSDN博客

我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。

这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。


        这是之前写的open62541测试代码(意思是我现在也不太看得懂,但是程序能正常运行)。

        环境为visual studio 2022,C++控制台项目。可移植到unix/linux。

目录

项目概况

程序概况

源代码

兼容性


项目概况

        上面方框里面是编译好的open62541,下面方框是main函数所在文件。

        附加的库:

程序概况

        程序自己写了一个server,又写了一个client,自己搞自己,输出如下(注意程序首先启动server,获得一部分输出,然后要敲一下回车才会启动client,输出信息然后程序自动结束):

Hello World!
[ 13940][2024-05-11 08:48:47.155 (UTC+0800)] warn/server        AccessControl: Unconfigured AccessControl. Users have all permissions.
[ 13940][2024-05-11 08:48:47.170 (UTC+0800)] info/server        AccessControl: Anonymous login is enabled
[ 13940][2024-05-11 08:48:47.170 (UTC+0800)] info/server        AccessControl: x509 certificate user authentication is enabled
[ 13940][2024-05-11 08:48:47.170 (UTC+0800)] warn/server        Username/Password Authentication configured, but no encrypting SecurityPolicy. This can leak credentials on the network.
[ 13940][2024-05-11 08:48:47.170 (UTC+0800)] warn/userland      AcceptAll Certificate Verification. Any remote certificate will be accepted.
ns: 1 S[the NodeId]
ns: 1 S[the NodeI]
ns: 0 N[50510]
ns: 0 N[50218]
ns: 0 N[50219]
[ 13940][2024-05-11 08:48:47.170 (UTC+0800)] info/network       TCP network layer listening on opc.tcp://mac-2023-11-09:5000/

[ 22336][2024-05-11 08:48:49.329 (UTC+0800)] warn/userland      AcceptAll Certificate Verification. Any remote certificate will be accepted.
[ 13940][2024-05-11 08:48:49.329 (UTC+0800)] info/network       Connection 512 | New connection over TCP from ::1
[ 13940][2024-05-11 08:48:49.329 (UTC+0800)] info/channel       Connection 512 | SecureChannel 1 | SecureChannel opened with SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None and a revised lifetime of 600.00s
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/channel       Connection 496 | SecureChannel 1 | SecureChannel opened with SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None and a revised lifetime of 600.00s
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/client        Client Status: ChannelState: Open, SessionState: Closed, ConnectStatus: Good
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/client        Selected endpoint 0 in URL opc.tcp://localhost:5000 with SecurityMode None and SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/client        Selected UserTokenPolicy open62541-anonymous-policy with UserTokenType Anonymous and SecurityPolicy http://opcfoundation.org/UA/SecurityPolicy#None
[ 13940][2024-05-11 08:48:49.344 (UTC+0800)] info/session       SecureChannel 1 | Session "" | Session created
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/client        Client Status: ChannelState: Open, SessionState: Created, ConnectStatus: Good
[ 13940][2024-05-11 08:48:49.344 (UTC+0800)] info/session       SecureChannel 1 | Session "" | ActivateSession: Session activated
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/client        Client Status: ChannelState: Open, SessionState: Activated, ConnectStatus: Good
连接成功
[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      date is: 11-5-2024 0:48:49.344

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      Variable Value is: 27

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      New Variable Value is: 100

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      未能获取 the NodeId2

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      readVariable 出错

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      未能获取 the NodeId3

[ 22336][2024-05-11 08:48:49.344 (UTC+0800)] info/userland      readVariable 出错(符合预期)


i   j   关系 类型 BROWSE NAME      DISPLAY NAME     NODEID           typeDefinition
  0   1 组织 对象 Server           Server           ns: 0 N[2253]    ns: 0 N[2004]    : 不是变量
      0   1 属性 变量 Auditing         Auditing         ns: 0 N[2994]    ns: 0 N[68]      : ns: 0 N[1] [0]
      0   2 属性 变量 ServiceLevel     ServiceLevel     ns: 0 N[2267]    ns: 0 N[68]      : ns: 0 N[3] [255]
      0   3 属性 变量 NamespaceArray   NamespaceArray   ns: 0 N[2255]    ns: 0 N[68]      : ns: 0 N[12]数组2 [http://opcfoundation.org/UA/]
      0   4 属性 变量 ServerArray      ServerArray      ns: 0 N[2254]    ns: 0 N[68]      : ns: 0 N[12]数组1 [urn:open62541.server.application]
      0   5 成员 对象 ServerRedundancy ServerRedundancy ns: 0 N[2296]    ns: 0 N[2034]    : 不是变量
          0   1 属性 变量 RedundancySupport RedundancySupport ns: 0 N[3709]    ns: 0 N[68]      : ns: 0 N[6] [0]
      0   6 成员 对象 VendorServerInfo VendorServerInfo ns: 0 N[2295]    ns: 0 N[2033]    : 不是变量
      0   7 成员 对象 ServerDiagnostics ServerDiagnostics ns: 0 N[2274]    ns: 0 N[2020]    : 不是变量
          0   1 属性 变量 EnabledFlag      EnabledFlag      ns: 0 N[2294]    ns: 0 N[68]      : ns: 0 N[1] [0]
      0   8 成员 对象 ServerCapabilities ServerCapabilities ns: 0 N[2268]    ns: 0 N[2013]    : 不是变量
          0   1 属性 变量 SoftwareCertificates SoftwareCertificates ns: 0 N[3704]    ns: 0 N[68]      : ns: 0 N[22]多维1数组1 [未获取 UA_TYPES_EXTENSIONOBJECT]
          0   2 属性 变量 MaxHistoryContinuationPoints MaxHistoryContinuationPoints ns: 0 N[2737]    ns: 0 N[68]      : ns: 0 N[5] [0]
          0   3 属性 变量 MaxQueryContinuationPoints MaxQueryContinuationPoints ns: 0 N[2736]    ns: 0 N[68]      : ns: 0 N[5] [0]
          0   4 属性 变量 MaxBrowseContinuationPoints MaxBrowseContinuationPoints ns: 0 N[2735]    ns: 0 N[68]      : ns: 0 N[5] [5]
          0   5 属性 变量 MinSupportedSampleRate MinSupportedSampleRate ns: 0 N[2272]    ns: 0 N[68]      : ns: 0 N[11] [50.000000]
          0   6 属性 变量 LocaleIdArray    LocaleIdArray    ns: 0 N[2271]    ns: 0 N[68]      : ns: 0 N[12]数组1 [en]
          0   7 属性 变量 ServerProfileArray ServerProfileArray ns: 0 N[2269]    ns: 0 N[68]      : ns: 0 N[12]数组3 [http://opcfoundation.org/UA-Profile/Server/MicroEmbeddedDevice]
          0   8 成员 对象 AggregateFunctions AggregateFunctions ns: 0 N[2997]    ns: 0 N[61]      : 不是变量
          0   9 成员 对象 ModellingRules   ModellingRules   ns: 0 N[2996]    ns: 0 N[61]      : 不是变量
              0   1 成员 对象 ExposesItsArray  ExposesItsArray  ns: 0 N[83]      ns: 0 N[77]      : 不是变量
                  0   1 属性 变量 NamingRule       NamingRule       ns: 0 N[114]     ns: 0 N[68]      : ns: 0 N[6] [3]
              0   2 成员 对象 Mandatory        Mandatory        ns: 0 N[78]      ns: 0 N[77]      : 不是变量
                  0   1 属性 变量 NamingRule       NamingRule       ns: 0 N[112]     ns: 0 N[68]      : ns: 0 N[6] [1]
              0   3 成员 对象 MandatoryPlaceholder MandatoryPlaceholder ns: 0 N[11510]   ns: 0 N[77]      : 不是变量
                  0   1 属性 变量 NamingRule       NamingRule       ns: 0 N[11511]   ns: 0 N[68]      : ns: 0 N[6] [1]
              0   4 成员 对象 Optional         Optional         ns: 0 N[80]      ns: 0 N[77]      : 不是变量
                  0   1 属性 变量 NamingRule       NamingRule       ns: 0 N[113]     ns: 0 N[68]      : ns: 0 N[6] [2]
              0   5 成员 对象 OptionalPlaceholder OptionalPlaceholder ns: 0 N[11508]   ns: 0 N[77]      : 不是变量
                  0   1 属性 变量 NamingRule       NamingRule       ns: 0 N[11509]   ns: 0 N[68]      : ns: 0 N[6] [2]
          0  10 成员 对象 OperationLimits  OperationLimits  ns: 0 N[11704]   ns: 0 N[11564]   : 不是变量
              0   1 属性 变量 MaxMonitoredItemsPerCall MaxMonitoredItemsPerCall ns: 0 N[11714]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   2 属性 变量 MaxNodesPerNodeManagement MaxNodesPerNodeManagement ns: 0 N[11713]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   3 属性 变量 MaxNodesPerTranslateBrowsePathsToNodeIds MaxNodesPerTranslateBrowsePathsToNodeIds ns: 0 N[11712]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   4 属性 变量 MaxNodesPerRegisterNodes MaxNodesPerRegisterNodes ns: 0 N[11711]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   5 属性 变量 MaxNodesPerBrowse MaxNodesPerBrowse ns: 0 N[11710]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   6 属性 变量 MaxNodesPerMethodCall MaxNodesPerMethodCall ns: 0 N[11709]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   7 属性 变量 MaxNodesPerWrite MaxNodesPerWrite ns: 0 N[11707]   ns: 0 N[68]      : ns: 0 N[7] [0]
              0   8 属性 变量 MaxNodesPerRead  MaxNodesPerRead  ns: 0 N[11705]   ns: 0 N[68]      : ns: 0 N[7] [0]
      0   9 成员 变量 ServerStatus     ServerStatus     ns: 0 N[2256]    ns: 0 N[2138]    : ns: 0 N[862]*******************************未识别的值类型 []
          0   1 成员 变量 ShutdownReason   ShutdownReason   ns: 0 N[2993]    ns: 0 N[63]      : ns: 0 N[21] [未获取 UA_TYPES_LOCALIZEDTEXT]
          0   2 成员 变量 SecondsTillShutdown SecondsTillShutdown ns: 0 N[2992]    ns: 0 N[63]      : ns: 0 N[7] [0]
          0   3 成员 变量 BuildInfo        BuildInfo        ns: 0 N[2260]    ns: 0 N[3051]    : ns: 0 N[338]*******************************未识别的值类型 []
              0   1 成员 变量 BuildDate        BuildDate        ns: 0 N[2266]    ns: 0 N[63]      : ns: 0 N[13] [2024年5月11日 0:48:47]
              0   2 成员 变量 BuildNumber      BuildNumber      ns: 0 N[2265]    ns: 0 N[63]      : ns: 0 N[12] [May  9 2024 15:23:14]
              0   3 成员 变量 SoftwareVersion  SoftwareVersion  ns: 0 N[2264]    ns: 0 N[63]      : ns: 0 N[12] [1.2.2-unknown]
              0   4 成员 变量 ManufacturerName ManufacturerName ns: 0 N[2263]    ns: 0 N[63]      : ns: 0 N[12] [open62541]
              0   5 成员 变量 ProductUri       ProductUri       ns: 0 N[2262]    ns: 0 N[63]      : ns: 0 N[12] [http://open62541.org]
              0   6 成员 变量 ProductName      ProductName      ns: 0 N[2261]    ns: 0 N[63]      : ns: 0 N[12] [open62541 OPC UA Server]
          0   4 成员 变量 State            State            ns: 0 N[2259]    ns: 0 N[63]      : ns: 0 N[6] [0]
          0   5 成员 变量 CurrentTime      CurrentTime      ns: 0 N[2258]    ns: 0 N[63]      : ns: 0 N[13] [2024年5月11日 0:48:49]
          0   6 成员 变量 StartTime        StartTime        ns: 0 N[2257]    ns: 0 N[63]      : ns: 0 N[13] [2024年5月11日 0:48:47]
      0  10 成员 方法 GetMonitoredItems GetMonitoredItems ns: 0 N[11492]   ns: 0 N[0]       : 不是变量
          0   0 属性 变量 OutputArguments  OutputArguments  ns: 0 N[11494]   ns: 0 N[68]      : ns: 0 N[22]数组2 [未获取 UA_TYPES_EXTENSIONOBJECT]
          0   1 属性 变量 InputArguments   InputArguments   ns: 0 N[11493]   ns: 0 N[68]      : ns: 0 N[22]数组1 [未获取 UA_TYPES_EXTENSIONOBJECT]
  0   2 组织 变量 browseName       displayName      ns: 1 S[the NodeId] ns: 0 N[63]      : ns: 0 N[6] [100]
  0   3 组织 对象 browseName       displayName      ns: 1 S[the NodeI] ns: 0 N[58]      : 不是变量
      0   1 组织 变量 a                displayName      ns: 0 N[50510]   ns: 0 N[63]      : ns: 0 N[6] [99]
      0   2 组织 对象 obj              displayName      ns: 0 N[50218]   ns: 0 N[58]      : 不是变量
          0   1 成员 变量 b                displayName      ns: 0 N[50219]   ns: 0 N[63]      : ns: 0 N[6] [29]
  0   4 组织 对象 Student (Manual) Student (Manual) ns: 0 N[50220]   ns: 0 N[58]      : 不是变量
      0   1 成员 变量 StudentName      Name             ns: 0 N[50221]   ns: 0 N[63]      : ns: 0 N[12] [Xiao Ming]
      0   2 成员 变量 Gender           Gender           ns: 0 N[50222]   ns: 0 N[63]      : ns: 0 N[12] [Male]
      0   3 成员 变量 Age              Age              ns: 0 N[50223]   ns: 0 N[63]      : ns: 0 N[3] [16]
      0   4 成员 变量 Height (cm)      Height (cm)      ns: 0 N[50224]   ns: 0 N[63]      : ns: 0 N[5] [170]
      0   5 成员 变量 Weight (kg)      Weight (kg)      ns: 0 N[50225]   ns: 0 N[63]      : ns: 0 N[5] [60]

i   j   关系 类型 BROWSE NAME      DISPLAY NAME     NODEID           typeDefinition

i   j   关系 类型 BROWSE NAME      DISPLAY NAME     NODEID           typeDefinition
  0   1 组织 变量 a                displayName      ns: 0 N[50510]   ns: 0 N[63]      : ns: 0 N[6] [99]
  0   2 组织 对象 obj              displayName      ns: 0 N[50218]   ns: 0 N[58]      : 不是变量
      0   1 成员 变量 b                displayName      ns: 0 N[50219]   ns: 0 N[63]      : ns: 0 N[6] [29]
[ 13940][2024-05-11 08:48:49.422 (UTC+0800)] info/session       SecureChannel 1 | Session "" | Closing the Session
[ 22336][2024-05-11 08:48:49.422 (UTC+0800)] info/client        Client Status: ChannelState: Closed, SessionState: Closed, ConnectStatus: Good
[ 13940][2024-05-11 08:48:49.422 (UTC+0800)] info/channel       Connection 512 | SecureChannel 1 | CloseSecureChannel
[ 13940][2024-05-11 08:48:49.422 (UTC+0800)] info/network       Connection 512 | Closed

x64\Debug\CppOPCDemo.exe (进程 5096)已退出,代码为 0。
按任意键关闭此窗口. . .

源代码

        打个包算了:

【免费】OPC-UAopen62541C++测试代码资源-CSDN文库

        注意里面没有sln文件,直接打开项目文件即可。里面还有个小小的readme.txt:

Open62541是开源OPC UA库
可以使用编译好的lib,也可以使用.c文件
.c文件修改了日志输出

服务端默认端口号4840,如果启动报错,一般是端口号被占用

         我应该是也用OPC-UA测试工具测试过的。毕竟自己搞自己有点不放心。

兼容性

        这些代码我也移植到ubuntu上了,除了头文件位置之外,主要是char *和char const*的不兼容,visual studio没那么严格。


(这里是结束)

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值