cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 LWIP MODBUS TCP

UAlti.1
Associate

Hi 

I work with stm32f4 modbus tcp /ip.

When I connect the ethernet cable first and then power the stm32f4, my code works good

But, when I first power the stm32f4 and then connect the ethernet cable, I cannot even ping it.

 

How I can solve it?

 

Thanks

4 REPLIES 4
Bob S
Principal

There have been several posts about this.  I don't know about the "new" style ST Ethernet drivers, but the "old' style (Cube F4 1.26.1 and earlier) didn't handle link up/down properly.

See https://community.st.com/t5/stm32-mcus-embedded-software/ethernet-does-not-work-if-uc-starts-with-the-cable-disconnected/m-p/312315#M22046  among others.  Search for things like "cable disconnected".

UAlti.1
Associate

Hi Bob

Thanks for answer 

Now, I can ping but modbus tcp isn't work.

When I connect the ethernet cable first and then power the stm32f4, my code works good

But, when I first power the stm32f4 and then connect the ethernet cable, modbus tcp doesn't work

Is there a way to tell if the cable is connected? Maybe I check this.

Do you have any solution?

Thanks.

 

Bob S
Principal

If the cable works when it is plugged in before powering on, then the issue (most likely) is not the cable.  If ping works but TCP connections do not, then there is some issue with the "link up" code that runs when the cable is connected.

But first - what EXACTLY so you mean "modbus tcp doesn't work"?  Do you get the initial SYN handshake (use wireshark if the other end of the TCP connection is a PC).

I am not that familiar with modbus.  Is your device the master or slave (maybe server or client are better terms)?  In other words does your code try to initiate TCP connections to other devices?  Or does it listen for connection requests from other devices?

To manually check if the cable is connected and the link has been established (i.e. auto-negotiate has finished) you can read the PHY BSR register like the code in ethernetif_set_link() is supposed to do (at least in the older ST Ethernet driver, not sure about the new/re-written driver).

Sebastiaan
Senior

Where is your modbus service started? From within MX_LWIP_Init or do you start it manually after MX_LWIP_Init?

I've used DHCP (client) in the past, and also had the problem that it didn't work if the ethernet cable was not yet connected at startup. That turned out to be a check in the DHCP initialization.

The simple "fix" for this issue is to execute the code " netif_set_up(&gnetif); netif_set_up(&gnetif);" immediately after MX_LWIP_Init. It's not the clean way to do, but it was required to get DHCP working. Maybe you're facing a similar issue?