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;