MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度解析 konimarti/opc 项目:Golang 实现的 OPC DA 客户端

项目背景与痛点

在工业自动化领域,OPC (OLE for Process Control) 是非常流行的数据访问规范,其中 OPC DA (Data Access) 是最早且应用最广泛的一种。其主要功能在于提供设备间的数据交互能力。然而,传统的 OPC DA 客户端通常基于 Windows COM,使用起来限制较多,尤其在异构系统和跨平台开发中存在痛点。

konimarti/opc 是一个用 Golang 实现的 OPC DA 客户端,它致力于为开发者提供一种更高效、更灵活的解决方案。Golang 的跨平台、高并发特性,使得 konimarti/opc 能在多种操作系统中运行,并且能够承载高负载的数据交互任务。

核心技术揭秘

技术栈与架构设计

konimarti/opc 项目的主体是通过 Go 语言绑定 COM 组件,来实现对 OPC DA 的支持。项目的核心,在于如何在 Go 环境中高效地实现 COM 下的数据交互能力。为达成这一目标,其采用了以下关键技术方案:

  • 使用 Golang 提供的 syscall 和 unsafe.Pointer 等底层 API 直接操作 Windows API,以完成 COM 对象的间接调用。
  • 通过自定义的数据结构和方法实现 OPC 标准数据模型,有效协调不同数据源的同步与异步读取。
数据流与架构图

为了更好地理解其数据处理流程,以下为一个简化的数据流示例图:

graph TD A[用户请求] --> B("解析请求参数") B --> C{"参数校验"} C -->|"通过"| D[发起 OPC 通道连接] C -->|"失败"| E[返回错误] D --> F[订阅或监听数据] F --> G[接收数据] G --> H("处理数据") H --> I{"数据验证"} I -->|"有效"| J[应用数据] H --> K[数据无效] J --> L[数据消费] L --> M[状态更新]

上述架构图展示了从用户请求到最终数据消费的全过程。

技术难点与优化策略

  • 底层 COM 交互优化:Golang 直接调用 C 原生函数库需要特别注意内存管理、数据类型转换等问题。这决定了性能和稳定性。
  • 异步处理能力:OPC DA 中数据传输通常需要异步转同步或反之,这要求开发人员对 Go 的并发模式(如 Channel、Context)有深入理解。
  • 兼容性问题:作为跨平台客户端,需要解决各操作系统 COM 实现上的细微差异,保持一致的执行效果。

功能亮点与差异

相比同类项目,konimarti/opc 以其轻量、高效著称,尤其在以下场景中表现更为突出:

  • 在嵌入式(如 Raspberry Pi)上的移植性,由于其对 C 库的依赖较少,可以更好适应嵌入式场景。
  • 支持开发便捷且容易上手,封装了大量的辅助函数,简化了底层技术的复杂度。

此外,一些传统 COM 客户端无法支持的跨平台或 Linux 上的 OPC 通讯需求,konimarti/opc 提供了多种方法实现相应功能,这无疑是其核心竞争优势之一。

应用场景与落地建议

konimarti/opc 项目适合用于需要进行实时工业数据访问与监控的应用,例如制造业生产线上,能够实现对设备状态和数据的有效监控和分析;在科研实验中,帮助快速获取并分析所需传感数据。但需要注意,由于是基于 COM,Windows平台上的开发与部署效果最佳,其他平台可能需要考虑兼容性问题;正确使用 COM 接口时要注意内存泄漏风险,确保有效管理内存。

综合评价

从优缺点角度进行总结:

  • 优点:跨平台支持能力强,适合进行高效、灵活的工业级数据访问;代码可读性好,易于开发和扩展。
  • 缺点:在部分操作系统上对 COM 的兼容性需进行特别检测;需要用户对底层技术有一定的理解与实践。

总体而言,konimarti/opc 项目在自动化数据获取与监控领域提供了优秀的技术实现,展现了卓越的细节处理和架构设计。无论是科研还是工业应用,它都是一份值得关注与考虑的优秀技术选择。

关于

GitHub 原始描述(英文)

OPC DA client in Golang for monitoring and analyzing process data based on Windows COM.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
466
价值分
188
活跃状态
活跃

语言占比

Go

Release

README

OPC DA in Go

License GoDoc goreportcard

Read and write process and automation data in Go from an OPC server for monitoring and data analysis purposes (OPC DA protocol).

go get github.com/konimarti/opc

Usage

client, _ := opc.NewConnection(
	"Graybox.Simulator", 		// ProgId
	[]string{"localhost"}, 		// Nodes
	[]string{"numeric.sin.float"}, 	// Tags
)
defer client.Close()
client.ReadItem("numeric.sin.float")
browser, _ := opc.CreateBrowser(
	"Graybox.Simulator", 		// ProgId
	[]string{"localhost"}, 		// Nodes	
)
opc.PrettyPrint(browser)

Installation

  • go get github.com/konimarti/opc

Troubleshooting

  • OPC DA Automation Wrapper 2.02 should be installed on your system (OPCDAAuto.dll or gbda_aut.dll); the automation wrapper is usually shipped as part of the OPC Core Components of your OPC Server.
  • You can get the Graybox DA Automation Wrapper here. Follow the installation instruction for this wrapper.
  • Depending on whether your OPC server and automation wrapper are 32-bit or 64-bit, set the Go architecture correspondingly:
    • For 64-bit OPC servers and wrappers: DLL should be in C:\Windows\System32, use $ENV:GOARCH="amd64"
    • For 32-bit OPC servers and wrappers: DLL should be in C:\Windows\SysWOW64, use $ENV:GOARCH="386"
  • Make sure to have correct DCOM settings on your local and remote computers: Dcomcnfg.exe

Reporting Issues

  • If you find a bug in the code, please create an issue and suggest a solution how to fix it.
  • Issues that are related to connection problems are mostly because of a faulty installation of your OPC automation wrapper or some peculiarties of your specific setup and OPC installation. Since we cannot debug your specific situation, these issues will be directly closed.

Debugging

  • Add opc.Debug() before the opc.NewConnection call to print more debug-related information.

Testing

  • Start Graybox Simulator v1.8. This is a free OPC simulation server and require for testing this package. It can be downloaded here.
  • If you use the Graybox Simulator, set $GOARCH environment variable to “386”, i.e. enter $ENV:GOARCH=386 in Powershell.
  • Test code with go test -v

Example

package main

import (
	"fmt"
	"github.com/konimarti/opc"
)

func main() {
	client, _ := opc.NewConnection(
		"Graybox.Simulator", // ProgId
		[]string{"localhost"}, //  OPC servers nodes
		[]string{"numeric.sin.int64", "numeric.saw.float"}, // slice of OPC tags
	)
	defer client.Close()

	// read single tag: value, quality, timestamp
	fmt.Println(client.ReadItem("numeric.sin.int64"))

	// read all added tags
	fmt.Println(client.Read())
}

with the following output:

{91 192 2019-06-21 15:23:08 +0000 UTC}
map[numeric.sin.int64:{91 192 2019-06-21 15:23:08 +0000 UTC} numeric.saw.float:{-36.42 192 2019-06-21 15:23:08 +0000 UTC
}]

Applications

opc-cli

  • opc-cli is a command-line interface to work with OPC servers: list available OPC servers, browse OPC tags on server, and read/write OPC tags.

  • Install it with go install github.com/konimarti/opc/cmds/opc-cli

    • List OPC servers on a specific node:

      $ opc-cli.exe list localhost
      Found 3 server(s) on 'localhost':
      Graybox.Simulator.1
      INAT TcpIpH1 OPC Server
      Prosys.OPC.Simulation
      
    • Browse OPC tags (in sub-branch):

      $ opc-cli.exe browse localhost Graybox.Simulator.1 textual
      textual
         - textual.color
         - textual.number
         - textual.random
         - textual.weekday
      
    • Write to OPC tag:

      $ opc-cli.exe write localhost Graybox.Simulator.1 options.sinfreq 0.01
      
    • Read OPC tags:

      $ opc-cli.exe read localhost Graybox.Simulator.1 options.sinfreq numeric.sin.float
      map[options.sinfreq:{0.05 192 2019-06-21 15:26:02 +0000 UTC} numeric.sin.float:{22.916641 192 2019-06-21 15:26:02 +0000 UTC}]
      

OPCAPI

  • Application to expose OPC tags with a JSON REST API.

    • Install the app: go install github.com/konimarti/opc/cmds/opcapi

    • Create config file:

      [config]
      allow_write = false
      allow_add = true
      allow_remove = true
      
      [opc]
      server = "Graybox.Simulator"
      nodes = [ "localhost" ]
      tags = [ "numeric.sin.float", "numeric.saw.float" ]
      
      
    • Run app:

      $ opcapi.exe -conf api.conf -addr ":4444"
      
    • Access API:

      • Get tags:
        $ curl.exe -X GET localhost:4444/tags
        {"numeric.saw.float":-21.41,"numeric.sin.float":62.303356}
        
      • Add tag:
        $ curl.exe -X POST -d '["numeric.triangle.float"]' localhost:4444/tag
        {"result": "created"}
        
      • Remove tag:
        $ curl.exe -X DELETE localhost:4444/tag/numeric.triangle.float
        {"result": "removed"}
        

OPCFLUX

  • Application to write OPC data directly to InfluxDB.

    • Install the app: go install github.com/konimarti/opc/cmds/opcflux

    • Create InfluxDB database “test”

    • Create config file: Put OPC tags in []. This is required for the expression evaluation. Any calculation can be performed that can evaluated.

      ---
      server: "Graybox.Simulator"
      nodes: ["localhost", "127.0.0.1"]
      monitoring: ""
      influx:
       addr: "http://localhost:8086"
       database: test
       precision: s
      measurements: 
       numeric:
         - tags: {type: sin}
           fields: {float: "[numeric.sin.float]", int: "[numeric.sin.int32]"}
         - tags: {type: saw}
           fields: {float: "[numeric.saw.float]", int: "[numeric.saw.int32]"}
         - tags: {type: calculation}
           fields: {float: "[numeric.triangle.float] / [numeric.triangle.int32]"}
       textual:
         - tags: {type: color}
           fields: {text: "[textual.color]", brown: "[textual.color] == 'Brown'"}        
         - tags: {type: weekday}
           fields: {text: "[textual.weekday]"}        
      
    • Run app: opcflux.exe -conf influx.yml -rate 1s

Credits

This software package has been developed for and is in production at Kalkfabrik Netstal.

评论

暂无评论