diff --git a/components/freemodbus/port/port.h b/components/freemodbus/port/port.h index 3ff8e95477..84d3d3ff34 100644 --- a/components/freemodbus/port/port.h +++ b/components/freemodbus/port/port.h @@ -127,6 +127,9 @@ void vMBPortExitCritical(void); #define MB_PORT_CHECK_EVENT( event, mask ) ( event & mask ) #define MB_PORT_CLEAR_EVENT( event, mask ) do { event &= ~mask; } while(0) +#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \ + ((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE) + // Legacy Modbus logging function #if MB_TCP_DEBUG void vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, diff --git a/components/freemodbus/port/portserial.c b/components/freemodbus/port/portserial.c index 1d2c6bf54a..9fbf774c9c 100644 --- a/components/freemodbus/port/portserial.c +++ b/components/freemodbus/port/portserial.c @@ -184,7 +184,6 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity) { esp_err_t xErr = ESP_OK; - MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure."); // Set communication port number ucUartNumber = ucPORT; // Configure serial communication parameters @@ -200,6 +199,9 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, case MB_PAR_EVEN: ucParity = UART_PARITY_EVEN; break; + default: + ESP_LOGE(TAG, "Incorrect parity option: %d", eParity); + return FALSE; } switch(ucDataBits){ case 5: diff --git a/components/freemodbus/port/portserial_m.c b/components/freemodbus/port/portserial_m.c index 65346079f7..023bcd05f8 100644 --- a/components/freemodbus/port/portserial_m.c +++ b/components/freemodbus/port/portserial_m.c @@ -180,7 +180,6 @@ static void vUartTask(void* pvParameters) BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) { esp_err_t xErr = ESP_OK; - MB_PORT_CHECK((eParity <= MB_PAR_EVEN), FALSE, "mb serial set parity failure."); // Set communication port number ucUartNumber = ucPORT; // Configure serial communication parameters @@ -196,6 +195,9 @@ BOOL xMBMasterPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, case MB_PAR_EVEN: ucParity = UART_PARITY_EVEN; break; + default: + ESP_LOGE(TAG, "Incorrect parity option: %d", eParity); + return FALSE; } switch(ucDataBits){ case 5: diff --git a/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c b/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c index 5f65c52cfe..426a45532b 100644 --- a/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c +++ b/components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c @@ -83,7 +83,7 @@ static esp_err_t mbc_serial_master_setup(void* comm_info) (uint32_t)comm_info_ptr->mode); MB_MASTER_CHECK((comm_info_ptr->port <= UART_NUM_MAX), ESP_ERR_INVALID_ARG, "mb wrong port to set = (0x%x).", (uint32_t)comm_info_ptr->port); - MB_MASTER_CHECK((comm_info_ptr->parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG, + MB_MASTER_CHECK((comm_info_ptr->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG, "mb wrong parity option = (0x%x).", (uint32_t)comm_info_ptr->parity); // Save the communication options mbm_opts->mbm_comm = *(mb_communication_info_t*)comm_info_ptr; @@ -102,7 +102,9 @@ static esp_err_t mbc_serial_master_start(void) // Initialize Modbus stack using mbcontroller parameters status = eMBMasterSerialInit((eMBMode)comm_info->mode, (UCHAR)comm_info->port, - (ULONG)comm_info->baudrate, (eMBParity)comm_info->parity); + (ULONG)comm_info->baudrate, + MB_PORT_PARITY_GET(comm_info->parity)); + MB_MASTER_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack initialization failure, eMBInit() returns (0x%x).", status); status = eMBMasterEnable(); diff --git a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c index 8c6dbff428..4e6db38f54 100644 --- a/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c +++ b/components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c @@ -75,7 +75,7 @@ static esp_err_t mbc_serial_slave_setup(void* comm_info) (uint32_t)comm_settings->slave_addr); MB_SLAVE_CHECK((comm_settings->port < UART_NUM_MAX), ESP_ERR_INVALID_ARG, "mb wrong port to set = (0x%x).", (uint32_t)comm_settings->port); - MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_EVEN), ESP_ERR_INVALID_ARG, + MB_SLAVE_CHECK((comm_settings->parity <= UART_PARITY_ODD), ESP_ERR_INVALID_ARG, "mb wrong parity option = (0x%x).", (uint32_t)comm_settings->parity); // Set communication options of the controller @@ -91,12 +91,15 @@ static esp_err_t mbc_serial_slave_start(void) "Slave interface is not correctly initialized."); mb_slave_options_t* mbs_opts = &mbs_interface_ptr->opts; eMBErrorCode status = MB_EIO; + const mb_communication_info_t* comm_info = (mb_communication_info_t*)&mbs_opts->mbs_comm; + // Initialize Modbus stack using mbcontroller parameters - status = eMBInit((eMBMode)mbs_opts->mbs_comm.mode, - (UCHAR)mbs_opts->mbs_comm.slave_addr, - (UCHAR)mbs_opts->mbs_comm.port, - (ULONG)mbs_opts->mbs_comm.baudrate, - (eMBParity)mbs_opts->mbs_comm.parity); + status = eMBInit((eMBMode)comm_info->mode, + (UCHAR)comm_info->slave_addr, + (UCHAR)comm_info->port, + (ULONG)comm_info->baudrate, + MB_PORT_PARITY_GET(comm_info->parity)); + MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE, "mb stack initialization failure, eMBInit() returns (0x%x).", status); status = eMBEnable();