refactor: Modbus 缓冲大小用宏替换硬编码 250

- 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
This commit is contained in:
wangfq
2026-07-30 15:28:11 +08:00
parent c7176b93f2
commit df6e38f118
2 changed files with 11 additions and 6 deletions
@@ -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;