feat: Modbus RTU V1.6 私有协议编码实现

新增:
- rs485_modbus.h/c — 完整 Modbus RTU 从机实现 (516行)
  · FC 0x02 读离散输入 (9-bit 位图: car_state/线圈/光耦/继电器)
  · FC 0x03 读保持寄存器 (含 HR 0x0010/0x0011)
  · FC 0x04 读输入寄存器 (基础 IR + 全数据块 0x0100-0x0112)
  · FC 0x06/0x10 写寄存器 (含值域校验+只读拦截)
  · FC 0x11 Report Slave ID
  · CRC16 查表 (poly=0xA001)
  · 全数据块组装 (19 reg = 38B big-endian)
  · 主动上报 3 级速率调度 (事件立即/活动150ms/空闲800ms)
  · 车间距/通过时间测量 (mstick/2=10ms单位, relay边沿触发)
  · 光耦状态实时读取 (GPIOB_ReadPortPin)
  · Holding Register 写穿透: Fusion Mode 同步 direction_mode,
    Hold Time 同步 g_hold_time, Freq Level 同步 g_loop_cng_unit

改造:
- rs485_700bs.h/c: 暴露 rs485_send() + rx缓冲(_rx_buf/_rx_len/_rx_last_tick) 供Modbus共用
- peripheral_main.c: g_fm_700bs_disable 分派 700BS/Modbus; modbus_init() 随 rs485_init 调用
This commit is contained in:
wangfq
2026-07-30 12:02:24 +08:00
parent df0585162b
commit e32f3892fe
5 changed files with 892 additions and 6 deletions
@@ -0,0 +1,49 @@
/*
* rs485_modbus.h
* DLD154Pro RS485 Modbus RTU 从机协议 — V1.6 私有扩展
*
* 物理层: RS485 半双工 (共享 UART0, 与 700BS 互斥)
* 帧格式: ADDR(1) + FC(1) + DATA(N) + CRC16(2)
* 主动上报: 3 级速率 (事件立即 / 活动 150ms / 空闲 800ms), FC 0x04 响应格式
*
* 2026-07-30 初始实现
*/
#ifndef __RS485_MODBUS_H__
#define __RS485_MODBUS_H__
#include <stdint.h>
/*===========================================================================
* 协议常量
*===========================================================================*/
/* 寄存器地址范围 */
#define MB_IR_FULL_BLOCK_START 0x0100 /* 全数据块起始地址 */
#define MB_IR_FULL_BLOCK_COUNT 19 /* 全数据块寄存器数 (0x01000x0112) */
#define MB_HR_AUTO_RPT_ENABLE 0x0010 /* 主动上报使能 (0=关, 1=开) */
#define MB_HR_ACTIVE_THRESHOLD 0x0011 /* 活动状态阈值 (|Variation|≥N) */
/* 主动上报间隔 (mstick() 5ms/tick) */
#define MB_RPT_IDLE_MS 160 /* 空闲: 800ms / 5ms = 160 tick */
#define MB_RPT_ACTIVE_MS 30 /* 活动: 150ms / 5ms = 30 tick */
#define MB_RPT_ACTIVE_HYST 2 /* 降速迟滞: 连续 N 次低于阈值 */
/* 帧超时: t3.5 = 3.5 × 11 / 9600 ≈ 4.0ms, 取 2 tick (10ms) */
#define MB_FRAME_TIMEOUT_TICK 2
/* 全数据块 CRC 后的帧总长:
* addr(1) + fc(1) + byte_cnt(1) + data(38) + crc(2) = 43 */
#define MB_FULL_DATA_FRAME_LEN 43
/*===========================================================================
* API
*===========================================================================*/
/* 初始化 Modbus 协议 (UART0 已在 rs485_init 中配置, 此处仅做协议状态初始化) */
void modbus_init(void);
/* 主循环轮询 — 帧接收 & 主动上报调度 (替代 rs485_poll) */
void modbus_poll(void);
#endif /* __RS485_MODBUS_H__ */