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

Using Modbus LIB with USB CDC #11

Closed parus247 closed 3 years ago

parus247 commented 3 years ago

Hello

Could you possibly explain how to, or where to read, or add some functional to implement not only uart port but also USB CDC ? Can't understand how to do it proper way.

Thank you

alejoseb commented 3 years ago

Hi, You should start analyzing the code of the 2 callbacks for USART interrupts. Specifically, the line https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/blob/3f4db2cf54dafe07fc9435ab086647fe5287db7d/MODBUS-LIB/Src/UARTCallback.c#L70 pushes the received bytes into a queue that is processed by the Modbus protocol handler, if you can replace this functionality with the USB CDC that should be an starting point to receive Modbus telegrams. Also notice that Modbus uses tiemouts to determine the end of a telegram, so you should figure out how to replace it or reuse the timer that is already there.

The TX callback https://github.com/alejoseb/Modbus-STM32-HAL-FreeRTOS/blob/3f4db2cf54dafe07fc9435ab086647fe5287db7d/MODBUS-LIB/Src/UARTCallback.c#L24 is simpler, it notifies the Modbus task that the transmission of the modbus telegram have finished.

Besides, the library is designed to manage the CS (chip select) of RS485 transceivers, this may not be necessary for USB CDC so you should also take care of that.

If you think your changes can be merged in the master branch you can open a pull request. Also let me know if you are working with any specific board so I can validate your changes.

alejoseb commented 3 years ago

Hey, I implemented the support for USB- CDC, there is a new example... enjoy it.