196 lines
6.7 KiB
Markdown
196 lines
6.7 KiB
Markdown
# SoftBus OPC
|
||
|
||
SoftBus OPC 是 SoftBus 工业数据总线的 **Phase 1 原型**:边缘代理采集现场数据,守护进程经协议插件解析后映射到 OPC UA 地址空间,供北向客户端读写与控制。
|
||
|
||
更完整的架构与模块说明见 [docs/README.md](docs/README.md)。
|
||
|
||
## 环境要求
|
||
|
||
| 依赖 | 说明 |
|
||
|------|------|
|
||
| CMake ≥ 3.16 | 构建系统 |
|
||
| C++17 编译器 | GCC / Clang / MSVC |
|
||
| Git | 首次配置时通过 FetchContent 拉取 nlohmann_json、asio |
|
||
| Python 3 | open62541 构建时代码生成(系统自带即可) |
|
||
| pkg-config(Linux,可选) | 用于检测 libdbus-1 |
|
||
| libdbus-1(Linux,可选) | 未安装时 DBus IPC 自动禁用 |
|
||
| libudev(Linux,可选) | 未安装时 udev 设备监控使用 stub |
|
||
| libzmq(可选) | 仅当开启 `SOFTBUS_WITH_ZMQ=ON` 时需要 |
|
||
|
||
- **open62541 v1.4.6**:已 vendor 至 `third_party/open62541/`,离线可直接构建 OPC UA
|
||
- **nlohmann_json、standalone asio**:首次配置时由 CMake FetchContent 自动下载
|
||
|
||
## 构建
|
||
|
||
在项目根目录执行:
|
||
|
||
```bash
|
||
cmake -B build -DSOFTBUS_WITH_OPCUA=ON
|
||
cmake --build build -j$(nproc)
|
||
```
|
||
|
||
常用 CMake 选项:
|
||
|
||
| 选项 | 默认值 | 说明 |
|
||
|------|--------|------|
|
||
| `SOFTBUS_WITH_OPCUA` | `ON` | 启用 OPC UA 服务端(open62541) |
|
||
| `SOFTBUS_WITH_ZMQ` | `OFF` | 启用 ZeroMQ 传输 |
|
||
|
||
构建产物位于:
|
||
|
||
```
|
||
build/bin/softbus_daemon # 守护进程(编排 + OPC UA)
|
||
build/bin/softbus_edge # 边缘代理(Modbus / CAN / Mock)
|
||
build/lib/protocol_modbus.so # Modbus 协议插件
|
||
build/lib/protocol_canopen.so # CANopen 协议插件
|
||
```
|
||
|
||
### 使用 Qt Creator
|
||
|
||
项目已包含 `CMakeLists.txt.user`,可直接用 Qt Creator 打开根目录 `CMakeLists.txt`,选择 **Desktop Qt 6.9.2**(或其他已配置的 Kit)进行构建。构建目录默认为 `build/Desktop_Qt_6_9_2-Debug/`,可执行文件在其中的 `bin/` 子目录。
|
||
|
||
## 运行
|
||
|
||
**请在项目根目录下启动**(`softbus_OPC/`),以便正确解析 `config/` 下的相对路径,并加载 `config/lib/` 或 `build/lib/` 中的协议插件。
|
||
|
||
### 启动前检查(可选)
|
||
|
||
若上次异常退出,可能残留进程占用端口:
|
||
|
||
```bash
|
||
pgrep -a softbus
|
||
ss -tlnp | grep -E ':9000|:4840'
|
||
# 如有残留:
|
||
pkill -f softbus_daemon
|
||
pkill -f softbus_edge
|
||
```
|
||
|
||
使用 EMUC SocketCAN 时,确认 `emuccan0` 已就绪(`ip link show emuccan0`)。
|
||
|
||
### 1. 启动守护进程(终端 1)
|
||
|
||
```bash
|
||
./build/bin/softbus_daemon \
|
||
--config config/daemon/daemon_profile.json \
|
||
--logical-points config/daemon/logical_points.json \
|
||
--bindings-global config/daemon/bindings_global.json
|
||
```
|
||
|
||
守护进程参数:
|
||
|
||
| 参数 | 默认值 | 说明 |
|
||
|------|--------|------|
|
||
| `--config <path>` | `config/daemon/daemon_profile.json` | 守护进程运行配置(OPC UA / uplink / 插件) |
|
||
| `--logical-points <path>` | `config/daemon/logical_points.json` | 逻辑点表(OPC UA 地址空间语义) |
|
||
| `--bindings-global <path>` | `config/daemon/bindings_global.json` | 全局绑定(物理点 ↔ 逻辑点 ↔ edgeId) |
|
||
| `--point-table <path>` | — | **兼容旧参数**,等同 `--logical-points` |
|
||
| `--self-test` | — | 启动后自动下发一条写值测试命令 |
|
||
|
||
启动成功后:
|
||
|
||
- **TCP 9000**:监听 edge 上行(`uplink.listenPort`)
|
||
- **OPC UA 4840**:暴露北向地址空间(`opcua.port`)
|
||
|
||
### 2. 启动边缘代理(终端 2)
|
||
|
||
连接真实设备(Modbus 串口/TCP、EMUC CAN 等,配置见 `config/edge/`):
|
||
|
||
```bash
|
||
./build/bin/softbus_edge --config config/edge/edge.json
|
||
```
|
||
|
||
无现场设备时,可用 Mock 模式仅验证 uplink / daemon 链路:
|
||
|
||
```bash
|
||
./build/bin/softbus_edge --config config/edge/edge.json --mock
|
||
```
|
||
|
||
边缘代理参数:
|
||
|
||
| 参数 | 默认值 | 说明 |
|
||
|------|--------|------|
|
||
| `--config <path>` | `config/edge/edge.json` | edge 主配置(edgeId、daemon 地址、物理层文件路径) |
|
||
| `--mock` | — | 不打开物理通道,周期性上报模拟数据 |
|
||
| `--object-ref <ref>` | `DemoSite/.../MotorTemperature` | 仅 `--mock` 时使用的模拟点位 |
|
||
|
||
`edge.json` 中的相对路径(如 `physical_channels.json`)相对于 `config/edge/` 目录解析。
|
||
|
||
### 3. 连接 OPC UA 客户端
|
||
|
||
```
|
||
opc.tcp://localhost:4840
|
||
```
|
||
|
||
应用名称默认为 `SoftBusDaemon`(见 `config/daemon/daemon_profile.json`)。
|
||
|
||
### 典型联调流程
|
||
|
||
```bash
|
||
# 终端 1:daemon
|
||
./build/bin/softbus_daemon \
|
||
--config config/daemon/daemon_profile.json \
|
||
--logical-points config/daemon/logical_points.json \
|
||
--bindings-global config/daemon/bindings_global.json
|
||
|
||
# 终端 2:edge(真实采集)
|
||
./build/bin/softbus_edge --config config/edge/edge.json
|
||
|
||
# 或:edge Mock(无硬件)
|
||
./build/bin/softbus_edge --config config/edge/edge.json --mock
|
||
```
|
||
|
||
daemon 先启、edge 后启。edge 断连后会每 5s 自动重连 daemon(`uplinkReconnectPeriodMs`)。
|
||
|
||
按 `Ctrl+C` 分别停止各进程。
|
||
|
||
## 配置说明
|
||
|
||
`config/daemon/daemon_profile.json` 控制守护进程行为:
|
||
|
||
```json
|
||
{
|
||
"opcua": { "port": 4840, "applicationName": "SoftBusDaemon" },
|
||
"uplink": { "listenHost": "127.0.0.1", "listenPort": 9000 },
|
||
"plugins": { "modbus": "protocol_modbus", "canopen": "protocol_canopen" }
|
||
}
|
||
```
|
||
|
||
当前推荐配置组合:
|
||
|
||
| 文件 | 作用 |
|
||
|------|------|
|
||
| `config/daemon/logical_points.json` | 逻辑点与 OPC UA 节点定义 |
|
||
| `config/daemon/bindings_global.json` | 物理点绑定与 edge 路由 |
|
||
| `config/edge/physical_channels.json` | 物理通道(串口 / TCP / `emuccan0` 等) |
|
||
| `config/edge/physical_devices.json` | 物理设备与采集点 |
|
||
| `config/edge/bindings_local.json` | edge 本地绑定(下行写备用解析) |
|
||
| `config/daemon/point_table.json` | **遗留**点表,仅兼容旧启动方式 |
|
||
|
||
更完整的 edge 物理层说明见 [docs/softbus_edge.md](docs/softbus_edge.md)。
|
||
|
||
## 项目结构
|
||
|
||
```
|
||
softbus_OPC/
|
||
├── CMakeLists.txt # 顶层 CMake
|
||
├── cmake/ # 平台检测、第三方依赖、打包辅助
|
||
├── third_party/ # 固定版本第三方源码(open62541)
|
||
├── config/ # 运行时配置与对象模型
|
||
├── docs/ # 详细架构与模块文档
|
||
└── src/
|
||
├── softbus_core/ # 共享内核(模型、插件 ABI、日志)
|
||
├── softbus_api/ # ExecuteCommand 控制面契约
|
||
├── softbus_transport/ # TCP 上行传输
|
||
├── softbus_opcua/ # OPC UA 服务端
|
||
├── softbus_daemon/ # 守护进程
|
||
├── softbus_edge/ # 边缘代理
|
||
└── plugins/ # Modbus / CANopen 协议插件
|
||
```
|
||
|
||
## 文档
|
||
|
||
| 文档 | 内容 |
|
||
|------|------|
|
||
| [docs/README.md](docs/README.md) | 文档索引与快速开始 |
|
||
| [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) | 分层架构与模块依赖 |
|