From df6e38f118d6cf6a6e321ffde9c2efabf935c813 Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 30 Jul 2026 15:28:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Modbus=20=E7=BC=93=E5=86=B2?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E7=94=A8=E5=AE=8F=E6=9B=BF=E6=8D=A2=E7=A1=AC?= =?UTF-8?q?=E7=BC=96=E7=A0=81=20250?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - rs485_modbus.h: +MB_MAX_READ_REGS(125) / MB_MAX_DATA_BYTES(250) / MB_RESP_BUF_SIZE(255) - rs485_modbus.c: data_buf[250]→data_buf[MB_MAX_DATA_BYTES] buf[5+250] →buf[MB_RESP_BUF_SIZE] quantity>125→quantity>MB_MAX_READ_REGS --- .../APP/include/rs485_modbus.h | 5 +++++ BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/BLE/DLD154Pro_Peripheral_28K/APP/include/rs485_modbus.h b/BLE/DLD154Pro_Peripheral_28K/APP/include/rs485_modbus.h index b726477..5848e9f 100644 --- a/BLE/DLD154Pro_Peripheral_28K/APP/include/rs485_modbus.h +++ b/BLE/DLD154Pro_Peripheral_28K/APP/include/rs485_modbus.h @@ -36,6 +36,11 @@ * 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 *===========================================================================*/ diff --git a/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c b/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c index 0d4ec5a..aa9a222 100644 --- a/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c +++ b/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c @@ -463,12 +463,12 @@ static void handle_read_input_regs(const uint8_t *rx, uint8_t rx_len) uint16_t start_addr = ((uint16_t)rx[2] << 8) | rx[3]; uint16_t quantity = ((uint16_t)rx[4] << 8) | rx[5]; - if (quantity == 0 || quantity > 125) { + if (quantity == 0 || quantity > MB_MAX_READ_REGS) { send_exception(0x04, 0x03); return; } - uint8_t data_buf[250]; + uint8_t data_buf[MB_MAX_DATA_BYTES]; uint16_t reg_addr; for (uint16_t i = 0; i < quantity; i++) { @@ -483,7 +483,7 @@ static void handle_read_input_regs(const uint8_t *rx, uint8_t rx_len) } uint8_t byte_cnt = (uint8_t)(quantity * 2); - uint8_t buf[5 + 250]; /* addr + fc + bc + data + crc */ + uint8_t buf[MB_RESP_BUF_SIZE]; uint16_t crc; buf[0] = g_rs485_id & 0xFF; @@ -543,12 +543,12 @@ static void handle_read_holding_regs(const uint8_t *rx, uint8_t rx_len) uint16_t start_addr = ((uint16_t)rx[2] << 8) | rx[3]; uint16_t quantity = ((uint16_t)rx[4] << 8) | rx[5]; - if (quantity == 0 || quantity > 125) { + if (quantity == 0 || quantity > MB_MAX_READ_REGS) { send_exception(0x03, 0x03); return; } - uint8_t data_buf[250]; + uint8_t data_buf[MB_MAX_DATA_BYTES]; for (uint16_t i = 0; i < quantity; i++) { uint16_t reg_addr = start_addr + i; @@ -562,7 +562,7 @@ static void handle_read_holding_regs(const uint8_t *rx, uint8_t rx_len) } uint8_t byte_cnt = (uint8_t)(quantity * 2); - uint8_t buf[5 + 250]; + uint8_t buf[MB_RESP_BUF_SIZE]; uint16_t crc; buf[0] = g_rs485_id & 0xFF;