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:
@@ -36,6 +36,11 @@
|
|||||||
* addr(1) + fc(1) + byte_cnt(1) + data(38) + crc(2) = 43 */
|
* addr(1) + fc(1) + byte_cnt(1) + data(38) + crc(2) = 43 */
|
||||||
#define MB_FULL_DATA_FRAME_LEN 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
|
* API
|
||||||
*===========================================================================*/
|
*===========================================================================*/
|
||||||
|
|||||||
@@ -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 start_addr = ((uint16_t)rx[2] << 8) | rx[3];
|
||||||
uint16_t quantity = ((uint16_t)rx[4] << 8) | rx[5];
|
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);
|
send_exception(0x04, 0x03);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t data_buf[250];
|
uint8_t data_buf[MB_MAX_DATA_BYTES];
|
||||||
uint16_t reg_addr;
|
uint16_t reg_addr;
|
||||||
|
|
||||||
for (uint16_t i = 0; i < quantity; i++) {
|
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 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;
|
uint16_t crc;
|
||||||
|
|
||||||
buf[0] = g_rs485_id & 0xFF;
|
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 start_addr = ((uint16_t)rx[2] << 8) | rx[3];
|
||||||
uint16_t quantity = ((uint16_t)rx[4] << 8) | rx[5];
|
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);
|
send_exception(0x03, 0x03);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t data_buf[250];
|
uint8_t data_buf[MB_MAX_DATA_BYTES];
|
||||||
|
|
||||||
for (uint16_t i = 0; i < quantity; i++) {
|
for (uint16_t i = 0; i < quantity; i++) {
|
||||||
uint16_t reg_addr = start_addr + 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 byte_cnt = (uint8_t)(quantity * 2);
|
||||||
uint8_t buf[5 + 250];
|
uint8_t buf[MB_RESP_BUF_SIZE];
|
||||||
uint16_t crc;
|
uint16_t crc;
|
||||||
|
|
||||||
buf[0] = g_rs485_id & 0xFF;
|
buf[0] = g_rs485_id & 0xFF;
|
||||||
|
|||||||
Reference in New Issue
Block a user