| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // Modbus/TCP 服务端 —— 视觉侧做 Server,西门子 PLC 做 Client(MB_CLIENT 轮询)。
- //
- // 数据面:
- // FC4 读输入寄存器 → LatestSnapshot 的 12 个字(视觉 → PLC 的结果区)
- // FC3 读保持寄存器 → 指令区映像(4 个字,PLC 可回读自检)
- // FC6 写单个保持寄存器 / FC16 写多个保持寄存器 → 指令区(PLC → 视觉)
- //
- // 设计要点:serve_forever() 只做"读快照 + 写指令区",不参与推理,因此推理慢不会阻塞 PLC。
- // 跨平台:Linux/BSD 用 POSIX socket,Windows 用 winsock2(同一个类,内部 #ifdef)。
- #pragma once
- #include <atomic>
- #include <cstdint>
- #include <functional>
- #include <mutex>
- #include <string>
- #include <vector>
- #include "portcv/schema.hpp"
- #include "portcv/snapshot.hpp"
- namespace portcv {
- /// 服务端配置(字段与 Python 配置段 plc.modbus.* 对应)。
- struct ModbusServerConfig {
- std::string host = "0.0.0.0";
- uint16_t port = 1502; ///< 标准 502 在 Linux 需要 root,现场按 PLC 侧要求改
- uint8_t unit_id = 1;
- bool allow_unit_any = true; ///< 接受任意单元号(有些 PLC 用 255)
- uint16_t input_addr = 0; ///< 输入寄存器起始地址
- uint16_t holding_addr = 0; ///< 保持寄存器起始地址
- };
- /// 极简 Modbus/TCP 服务端(单线程 select,多客户端并发)。
- ///
- /// 典型现场:PROFINET 网关长期持有 Modbus 连接读结果,调试工具/备用 PLC
- /// 同时连进来读状态或写指令。旧实现 accept 后串行 handle_client,第二个
- /// 客户端会一直排队;这里改为 select 多路复用,单线程且无并发写寄存器问题。
- class ModbusServer {
- public:
- ModbusServer(const ModbusServerConfig& config, const LatestSnapshot* snapshot);
- ~ModbusServer();
- ModbusServer(const ModbusServer&) = delete;
- ModbusServer& operator=(const ModbusServer&) = delete;
- /// 绑定 + 监听。失败返回 false 并填写 error。
- bool start(std::string* error = nullptr);
- /// 实际绑定端口(port=0 时由内核分配,自检用)。
- uint16_t bound_port() const { return bound_port_; }
- /// 阻塞循环:接受连接并处理请求,直到 stop() 被调用。
- void serve_forever();
- /// 请求停止(可从其它线程调用;幂等)。
- /// 只置停止标志:serve_forever 最多 200 ms 内退出并关闭所有客户端连接,
- /// 监听 socket 由析构函数释放。调用方应 join 服务线程后再析构本对象
- /// (与 Python `stop()` → `shutdown()` → `server_close()` → `join` 同序)。
- void stop() { stop_.store(true, std::memory_order_release); }
- bool stopped() const { return stop_.load(std::memory_order_acquire); }
- /// 已处理的请求数(自检/日志用)。
- uint64_t served_requests() const { return served_.load(std::memory_order_acquire); }
- /// 指令区当前映像(4 个字)。
- std::vector<uint16_t> holding_image() const;
- /// PLC 写指令时的回调(在服务线程内触发,回调里不要做重活)。
- void set_command_handler(std::function<void(const PlcCommand&)> handler);
- /// 最近一次收到的指令。
- PlcCommand last_command() const;
- /// 处理一个完整请求帧,返回响应帧(空表示不应答/断连)。
- /// 与 Python 侧 `ModbusTcpServer.handle_frame` 同名同语义,公开给自检直接喂帧。
- std::vector<uint8_t> handle_frame(const uint8_t* data, std::size_t len);
- private:
- /// 从客户端读一次并处理缓冲区里所有完整帧;返回 false 表示应断开。
- bool service_client(long long client_fd, std::vector<uint8_t>& buffer);
- ModbusServerConfig cfg_;
- const LatestSnapshot* snapshot_;
- std::atomic<bool> stop_{false};
- std::atomic<uint64_t> served_{0};
- uint16_t bound_port_ = 0;
- long long listen_fd_ = -1;
- mutable std::mutex holding_mu_;
- std::vector<uint16_t> holding_ = {0, 0, 0, 0};
- mutable std::mutex command_mu_;
- PlcCommand last_command_{};
- std::function<void(const PlcCommand&)> command_handler_;
- };
- } // namespace portcv
|