alejoseb / Modbus-STM32-HAL-FreeRTOS

Modbus TCP and RTU, Master and Slave for STM32 using Cube HAL and FreeRTOS
GNU Lesser General Public License v2.1
539 stars 183 forks source link

Receiving UART data from a UART port different than the Modbus port causes Modbus to stop working #42

Closed emptor8 closed 2 years ago

emptor8 commented 2 years ago

Hi there,

Thanks for this great library! I'm seeing the following problem:

Do you know what I'm missing here?

Note: I added conditional checks in UARTCallback.c to only run the code logic in the callbacks for the &huart1 instance. If the &huart2 instance is detected, the callbacks do nothing.

alejoseb commented 2 years ago

Hi, If you have a complete example to reproduce your issue I can take a look. There could be multiple reasons for this issue. For example, If the task where you are reading from serial has higher priority than the modbus task and you are using a blocking operation, like the function in your comment, then is possible that the modbus task is not being executed by the FreeRTOS scheduler. It should be better if you use a non-blocking reading method, and a notification from the callback to the task when data has arrived.

emptor8 commented 2 years ago

Attached is a complete example that demonstrates the issue I'm having.

Things work fine if I comment out HAL_UART_Receive() -- I can still transmit UART data on huart5 and run the Modbus on huart4 at the same time.

However, with HAL_UART_Receive() on huart5, it prevents Modbus from running on huart4. The LED is still being toggled in debugTask2 though, so it seems like the CPU isn't being held up by HAL_UART_Receive()? I've also made debugTask1 and debugTask2 lower priority than the Modbus slave task but this does not help.

modbus_test_12.22.21_v1.zip

emptor8 commented 2 years ago

Hey @alejoseb, did you get a chance to reproduce this?

alejoseb commented 2 years ago

I cannot reproduce your issue since I don't have your board, and you did not include the modified version of the modbus library in the zip it is only a link. I quickly checked the code though. I saw that you use portMAX_DELAY for waiting for a character, that is wrong. portMAX_DELAY is a freertos macro, it is not meant to work with the HAL library, I don't know what could be the effect of using that macro. Besides, you should use interrupt mode to receive the character no the blocking mode. The blocking mode does not yield. Another option could be to use the blocking mode but with a few ms timeout and check the returned value of the HAL function. HAL_OK should mean that you received a character.

I am closing this issue since it is not related to the library.