45 lines
1.6 KiB
C++
45 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include "line_laser_modbus/protocol.hpp"
|
|
|
|
namespace line_laser_modbus {
|
|
|
|
struct StatusSnapshot {
|
|
WorkMode mode = WorkMode::StandbyReset;
|
|
DeviceState state = DeviceState::StandbyReady;
|
|
};
|
|
|
|
// HostClient 是上位机/主站侧协议封装。
|
|
//
|
|
// 设计说明:
|
|
// - 它不持有串口对象。生产代码可以把返回的字节发送到 UART、RS-485、
|
|
// TCP 隧道或测试桩,不需要改动协议逻辑。
|
|
// - 每个 make_* 方法都会返回包含 CRC 的完整 Modbus RTU 帧。
|
|
// - 每个 parse_* 方法都会校验从站地址和期望的载荷形状,避免调用方误收
|
|
// 其他请求对应的响应。
|
|
class HostClient {
|
|
public:
|
|
explicit HostClient(std::uint8_t slave_id = kDefaultSlaveId) noexcept;
|
|
|
|
[[nodiscard]] std::uint8_t slave_id() const noexcept;
|
|
|
|
[[nodiscard]] ByteVector make_read_status_request() const;
|
|
[[nodiscard]] ByteVector make_read_current_pose_request() const;
|
|
[[nodiscard]] ByteVector make_write_mode_request(WorkMode mode) const;
|
|
[[nodiscard]] ByteVector make_write_target_pose_request(const Pose6D& pose) const;
|
|
[[nodiscard]] ByteVector make_write_correction_request(const Pose6D& correction) const;
|
|
|
|
[[nodiscard]] std::optional<StatusSnapshot> parse_status_response(
|
|
const ByteVector& frame) const;
|
|
[[nodiscard]] std::optional<Pose6D> parse_current_pose_response(
|
|
const ByteVector& frame) const;
|
|
[[nodiscard]] bool parse_write_ack(const ByteVector& frame,
|
|
std::uint16_t expected_start_address,
|
|
std::uint16_t expected_quantity) const;
|
|
|
|
private:
|
|
std::uint8_t slave_id_;
|
|
};
|
|
|
|
} // 命名空间 line_laser_modbus
|