Files
DLD154Pro/BLE/DLD154Pro_Peripheral_28K/APP/include/rs485_modbus.h
T

57 lines
2.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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) */
#define MB_HR_SYSTEM_CMD 0x0012 /* 系统指令: 写 0x2C2C 复位 MCU */
#define MB_CMD_RESET_MCU 0x2C2C /* 复位 MCU 指令值 */
/* 主动上报间隔 (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
/* FC 0x03/0x04 读寄存器缓冲 (Modbus 标准: max qty = 0x007D = 125) */
#define MB_MAX_READ_REGS 125 /* 单次读取最大寄存器数 */
#define MB_MAX_DATA_BYTES 250 /* 125 × 2 字节 */
#define MB_RESP_BUF_SIZE (5 + MB_MAX_DATA_BYTES) /* addr+fc+bc+data+crc */
/*===========================================================================
* API
*===========================================================================*/
/* 初始化 Modbus 协议 (UART0 已在 rs485_init 中配置, 此处仅做协议状态初始化) */
void modbus_init(void);
/* 主循环轮询 — 帧接收 & 主动上报调度 (替代 rs485_poll) */
void modbus_poll(void);
#endif /* __RS485_MODBUS_H__ */