STM32 Freemodbus TCP Communication

Source: Internet
Author: User


These two days have been debugging Stm32f4 TCP, and want to apply to Freemodbus TCP, so that my device can integrate network control.






Because now most of the use of STM32 should use ST dedicated configuration software STM32 Cubemx to carry out the initial project of the program, there is a benefit, is to speed up the development progress, I am also here in this software development, I just to write down their use of the process, easy to view later, May be a bit messy to write.






1, STM32 Cubemx set up the project, the content includes ETH,FREERTOS,LWIP



2, only need to configure LWIP as static address, this is because my device is not convenient to use dynamic address, you can also do not use DHCP







3, Generate KEIL5 project, then add Freemodbus file, make changes.










Here the main is to modify porttcp.c this file, and in port.h add a macro definition



/* Critical section Management. */
extern void vportentercritical (void);
extern void vportexitcritical (void);


#define Enter_critical_section () vportentercritical ()
#define Exit_critical_section () vportexitcritical ()






There is also a TCP mode configured below the Mbconfig.h



/*! \brief If Modbus ASCII support is enabled. */
#define MB_ASCII_ENABLED (0)
/*! \brief If Modbus RTU support is enabled. */
#define MB_RTU_ENABLED (0)
/*! \brief If Modbus TCP support is enabled. */
#define MB_TCP_ENABLED (1)






4, the main program to add a response Freemodbus TCP can be.



Osthreaddef (Modbuspolltask, Embpolltask, Osprioritynormal, 0, 128);
Osthreadcreate (Osthread (modbuspolltask), NULL);



Of course, before creating a task, start with Freemodbus TCP, and we can initialize it directly in the generated Startdefaulttask task. Mx_lwip_init (); Generates a task that will receive the packet and answer the ARP and ping. This is the software generated automatically, so we have to do a little work.



/* Startdefaulttask function */
void Startdefaulttask (void const * argument)
{
Mx_lwip_init ();
/* USER CODE BEGIN 5 */
Embtcpinit (0);
Embenable ();
/* Infinite Loop */
for (;;)
{
Osdelay (1);
}
/* USER CODE END 5 */
}






To this our Freemodbus TCP will be able to, through the Ping and Modbus poll connection test is not a problem.















Of course, there will be a little bit of a problem, but I am sure that we can solve their own. Look at the details of the original code experience, I am also a beginner.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.