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
576 stars 191 forks source link

Incorrect timeout between messages #88

Closed ZaTeeman closed 1 year ago

ZaTeeman commented 1 year ago

First of all thank you for your work

I am using modbus RTU master mode to communicate with several slaves. boudrate: 115200 use DMA microcontroller: stm32f407

Sometimes communication with some devices is lost. As a result of searching for the reason, it was discovered that the library makes an insufficient period of silence between messages (for speed 115200, the standard provides for at least 1.75ms of silence) 08912e08-f3f9-4401-a043-93cd087c9d33

I send messages from different threads using "ModbusQuery(&ModbusMaster, modbus_t_struct)"

Tell me how to set the correct silence time between messages?

alejoseb commented 1 year ago

hi, can you explain what is the meaning of the trace in red? if that is the master, you have to add delays, (i.e., vtaskDelay) between calls to ModbusQuery.

ZaTeeman commented 1 year ago

yes, the red track is the master, the green track is the slave’s response.

Do I understand correctly that the "ModbusQuery" command is just adding a message to the queue for sending? The sending itself is made by the library when it is ready (the communication line is free, all the necessary time intervals have been met, etc.)

My "ModbusQuery" is called in different threads, so adding "vtaskDelay" is not always possible.

How can I modify the library code to automatically add a delay between messages?

alejoseb commented 1 year ago

You can add a delay just after getting a new telegram from the queue here:

https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/blob/861d511588915796447184e1e384c11becfb1466/MODBUS-LIB/Src/Modbus.c#L1058

You can make it a configurable parameter adding it to the modbus handler structure here: https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/blob/861d511588915796447184e1e384c11becfb1466/MODBUS-LIB/Inc/Modbus.h#L173

That would be a better option so you can have different configurations for different serial ports working at different baudrates. In the case of TCP that should not be needed so adding a conditional statement to verify that the interface is USART-based is also needed. It can also be auto-calculated, but FreeRTOS does not provide enough granularity for a perfect delay, which is not a big deal either.

You can consider to submit a PR to add that as a functionality, if you can verify it is working as needed.