Bladeren bron

freemodbus: fix mb controller parity propagation issues

aleks 5 jaren geleden
bovenliggende
commit
0a36f36016

+ 3 - 0
components/freemodbus/port/port.h

@@ -20,6 +20,7 @@
 #include "freertos/xtensa_api.h"
 #include "freertos/portmacro.h"
 #include "esp_log.h"                // for ESP_LOGE macro
+#include "mbconfig.h"
 
 #define INLINE                      inline
 #define PR_BEGIN_EXTERN_C           extern "C" {
@@ -64,6 +65,8 @@ void vMBPortExitCritical( );
 
 #define EXIT_CRITICAL_SECTION( )  { vMBPortExitCritical(); \
                                     ESP_LOGD(MB_PORT_TAG,"%s: Port exit critical", __func__); }
+#define MB_PORT_PARITY_GET(parity) ((parity != UART_PARITY_DISABLE) ? \
+                                        ((parity == UART_PARITY_ODD) ? MB_PAR_ODD : MB_PAR_EVEN) : MB_PAR_NONE)
 
 #ifdef __cplusplus
 PR_END_EXTERN_C

+ 3 - 1
components/freemodbus/port/portserial.c

@@ -198,7 +198,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
@@ -214,6 +213,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:

+ 3 - 1
components/freemodbus/port/portserial_m.c

@@ -193,7 +193,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
@@ -209,6 +208,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:

+ 3 - 2
components/freemodbus/serial_master/modbus_controller/mbc_serial_master.c

@@ -85,7 +85,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;
@@ -104,7 +104,8 @@ static esp_err_t mbc_serial_master_start(void)
 
     // Initialize Modbus stack using mbcontroller parameters
     status = eMBMasterInit((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();

+ 2 - 2
components/freemodbus/serial_slave/modbus_controller/mbc_serial_slave.c

@@ -71,7 +71,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
@@ -92,7 +92,7 @@ static esp_err_t mbc_serial_slave_start(void)
                          (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);
+                         MB_PORT_PARITY_GET(mbs_opts->mbs_comm.parity));
     MB_SLAVE_CHECK((status == MB_ENOERR), ESP_ERR_INVALID_STATE,
             "mb stack initialization failure, eMBInit() returns (0x%x).", status);
     status = eMBEnable();