| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- // Modbus/TCP 报文编解码 —— 与 Python 侧 portcv/plc/modbus_pdu.py 行为一致。
- //
- // 支持的 PDU 功能码:
- // 0x03 读保持寄存器(PLC → 视觉的指令区)
- // 0x04 读输入寄存器(视觉 → PLC 的结果区,本项目的核心数据)
- // 0x06 写单个保持寄存器
- // 0x10 写多个保持寄存器
- //
- // MBAP 头:事务号(2) 协议号(2)=0 长度(2) 单元号(1);length 含单元号 + PDU。
- // 注意:Modbus/TCP 的 MBAP 报文本身没有 CRC(CRC 只用于串行 RTU)。
- #pragma once
- #include <cstddef>
- #include <cstdint>
- #include <stdexcept>
- #include <string>
- #include <vector>
- #include "portcv/schema.hpp"
- namespace portcv {
- constexpr int MBAP_HEADER_LEN = 7;
- enum : uint8_t {
- FC_READ_COILS = 0x01,
- FC_READ_DISCRETE_INPUTS = 0x02,
- FC_READ_HOLDING_REGISTERS = 0x03,
- FC_READ_INPUT_REGISTERS = 0x04,
- FC_WRITE_SINGLE_COIL = 0x05,
- FC_WRITE_SINGLE_REGISTER = 0x06,
- FC_WRITE_MULTIPLE_REGISTERS = 0x10,
- };
- enum : uint8_t {
- EXC_ILLEGAL_FUNCTION = 0x01,
- EXC_ILLEGAL_DATA_ADDRESS = 0x02,
- EXC_ILLEGAL_DATA_VALUE = 0x03,
- EXC_SERVER_DEVICE_FAILURE = 0x04,
- EXC_ACKNOWLEDGE = 0x05,
- EXC_SERVER_DEVICE_BUSY = 0x06,
- };
- inline const char* exception_text(uint8_t code) {
- switch (code) {
- case EXC_ILLEGAL_FUNCTION: return "非法功能码";
- case EXC_ILLEGAL_DATA_ADDRESS: return "非法数据地址";
- case EXC_ILLEGAL_DATA_VALUE: return "非法数据值";
- case EXC_SERVER_DEVICE_FAILURE: return "从站设备故障";
- case EXC_ACKNOWLEDGE: return "确认(正在处理)";
- case EXC_SERVER_DEVICE_BUSY: return "设备忙";
- default: return "未知";
- }
- }
- /// 解析后的 MBAP + PDU。
- struct MbapFrame {
- uint16_t tx_id = 0;
- uint16_t proto_id = 0;
- uint16_t length = 0;
- uint8_t unit_id = 0;
- std::vector<uint8_t> pdu;
- };
- /// 解析 MBAP + PDU;长度不足或 PDU 不完整会抛异常(调用方应回错误帧或直接断连)。
- inline MbapFrame parse_mbap(const uint8_t* data, std::size_t len) {
- if (len < static_cast<std::size_t>(MBAP_HEADER_LEN)) {
- throw std::invalid_argument("报文长度不足");
- }
- MbapFrame f;
- f.tx_id = static_cast<uint16_t>((static_cast<uint16_t>(data[0]) << 8) | data[1]);
- f.proto_id = static_cast<uint16_t>((static_cast<uint16_t>(data[2]) << 8) | data[3]);
- f.length = static_cast<uint16_t>((static_cast<uint16_t>(data[4]) << 8) | data[5]);
- f.unit_id = data[6];
- f.pdu.assign(data + MBAP_HEADER_LEN, data + len);
- // 允许 PDU 多带字节(部分网关如此),按 length-1 截断(与 Python 一致)。
- const std::size_t want = f.length > 0 ? static_cast<std::size_t>(f.length - 1) : 0;
- if (f.pdu.size() > want) { f.pdu.resize(want); }
- return f;
- }
- /// MBAP 封装。
- inline std::vector<uint8_t> build_frame(uint16_t tx_id, uint8_t unit_id,
- const std::vector<uint8_t>& pdu) {
- std::vector<uint8_t> out;
- out.reserve(pdu.size() + MBAP_HEADER_LEN);
- out.push_back(static_cast<uint8_t>((tx_id >> 8) & 0xFF));
- out.push_back(static_cast<uint8_t>(tx_id & 0xFF));
- out.push_back(0); // 协议号 = 0
- out.push_back(0);
- const uint16_t length = static_cast<uint16_t>(pdu.size() + 1);
- out.push_back(static_cast<uint8_t>((length >> 8) & 0xFF));
- out.push_back(static_cast<uint8_t>(length & 0xFF));
- out.push_back(unit_id);
- out.insert(out.end(), pdu.begin(), pdu.end());
- return out;
- }
- /// 构造读类/写单个寄存器请求(探测工具与自检用)。
- inline std::vector<uint8_t> build_request(uint16_t tx_id, uint8_t unit_id, uint8_t function,
- uint16_t address, uint16_t quantity = 1,
- uint16_t single_value = 0) {
- std::vector<uint8_t> pdu;
- switch (function) {
- case FC_READ_HOLDING_REGISTERS:
- case FC_READ_INPUT_REGISTERS:
- case FC_READ_COILS:
- case FC_READ_DISCRETE_INPUTS:
- pdu = {function, static_cast<uint8_t>((address >> 8) & 0xFF),
- static_cast<uint8_t>(address & 0xFF),
- static_cast<uint8_t>((quantity >> 8) & 0xFF),
- static_cast<uint8_t>(quantity & 0xFF)};
- break;
- case FC_WRITE_SINGLE_REGISTER:
- pdu = {function, static_cast<uint8_t>((address >> 8) & 0xFF),
- static_cast<uint8_t>(address & 0xFF),
- static_cast<uint8_t>((single_value >> 8) & 0xFF),
- static_cast<uint8_t>(single_value & 0xFF)};
- break;
- default:
- throw std::invalid_argument("未支持的请求功能码");
- }
- return build_frame(tx_id, unit_id, pdu);
- }
- /// 构造 FC16 写多个保持寄存器请求。
- inline std::vector<uint8_t> build_write_multiple_request(uint16_t tx_id, uint8_t unit_id,
- uint16_t address,
- const std::vector<uint16_t>& values) {
- std::vector<uint8_t> pdu = {FC_WRITE_MULTIPLE_REGISTERS,
- static_cast<uint8_t>((address >> 8) & 0xFF),
- static_cast<uint8_t>(address & 0xFF),
- static_cast<uint8_t>((values.size() >> 8) & 0xFF),
- static_cast<uint8_t>(values.size() & 0xFF),
- static_cast<uint8_t>((values.size() * 2) & 0xFF)};
- for (uint16_t w : values) {
- pdu.push_back(static_cast<uint8_t>((w >> 8) & 0xFF));
- pdu.push_back(static_cast<uint8_t>(w & 0xFF));
- }
- return build_frame(tx_id, unit_id, pdu);
- }
- /// 异常响应(功能码最高位置 1)。
- inline std::vector<uint8_t> build_exception_response(uint16_t tx_id, uint8_t unit_id,
- uint8_t function, uint8_t code) {
- std::vector<uint8_t> pdu = {static_cast<uint8_t>(function | 0x80), code};
- return build_frame(tx_id, unit_id, pdu);
- }
- /// 读类响应 PDU(功能码 + 字节数 + 大端字)。
- inline std::vector<uint8_t> build_read_response_pdu(uint8_t function,
- const std::vector<uint16_t>& words) {
- std::vector<uint8_t> pdu;
- pdu.push_back(function);
- pdu.push_back(static_cast<uint8_t>((words.size() * 2) & 0xFF));
- const std::vector<uint8_t> body = words_to_bytes(words);
- pdu.insert(pdu.end(), body.begin(), body.end());
- return pdu;
- }
- /// 解析 0x03/0x04 响应 PDU → 寄存器列表;异常响应抛 std::runtime_error(与 Python 一致)。
- inline std::vector<uint16_t> parse_read_response(const std::vector<uint8_t>& pdu) {
- if (pdu.empty()) { throw std::invalid_argument("空 PDU"); }
- const uint8_t function = pdu[0];
- if (function & 0x80) {
- const uint8_t code = pdu.size() > 1 ? pdu[1] : 0;
- throw std::runtime_error(std::string("Modbus 异常响应: ") + exception_text(code));
- }
- if (function != FC_READ_HOLDING_REGISTERS && function != FC_READ_INPUT_REGISTERS) {
- throw std::invalid_argument("不是读寄存器响应");
- }
- const std::size_t byte_count = pdu.size() > 1 ? pdu[1] : 0;
- const std::size_t avail = pdu.size() > 2 ? pdu.size() - 2 : 0;
- const std::size_t n = byte_count < avail ? byte_count : avail;
- return bytes_to_words(pdu.data() + 2, n);
- }
- } // namespace portcv
|