W5500 TCP Client CONNECT problem


We are using the W5500 (W550io board) with a STM32f417.
Until now, for testing purpose, we were working with a Terminal on PC (as a TCP Client) and the Wiznet module configured to be the TCP Server.
No problem in this case.

Now, we would like to use the Wiznet as a TCP Client. I did an initialization as it seems to be done on the datasheet but when I go to the step where I send the CONNECT Command, I always get SOCK_SYNSENT and then SOCK_CLOSED. I never get SOCK_ESTABLISHED.

I will put my initialization code here, maybe you will find my problem. Note that the WriteRegister function are working well because it works with the TCP Server configuration.

[code]// TCP Client
void vWIZNET_InitDeviceClient(void)
uint8_t __u8_SocketStatus = 0xFF;

uint8_t __au8_DataToWrite[4] = {0xC0, 0xA8, 0x01, 0x01};
vWIZNET_WriteRegisterValue(SOCKET_REGISTER_DIPR, SOCKET_0_REGISTER_BLOCK, __au8_DataToWrite, 4); // Set Destination IP address

__au8_DataToWrite[0] = 0x79;
__au8_DataToWrite[1] = 0x18;
vWIZNET_WriteRegisterValue(SOCKET_REGISTER_DPORT, SOCKET_0_REGISTER_BLOCK, __au8_DataToWrite, 2); // Set Destination port 31000

__au8_DataToWrite[0] = 0x01;
vWIZNET_WriteRegisterValue(SOCKET_REGISTER_MR, SOCKET_0_REGISTER_BLOCK, __au8_DataToWrite, 1); // Set TCP protocol

__au8_DataToWrite[0] = OPEN_COMMAND; //0x01
vWIZNET_WriteRegisterValue(SOCKET_REGISTER_CR, SOCKET_0_REGISTER_BLOCK, __au8_DataToWrite, 1); // Open command

vWIZNET_ReadRegisterValue(SOCKET_REGISTER_SR, SOCKET_0_REGISTER_BLOCK, &__u8_SocketStatus, 1); // Get socket status register
while(__u8_SocketStatus != 0x13); // SOCK_INIT

__au8_DataToWrite[0] = CONNECT_COMMAND; // 0x04
vWIZNET_WriteRegisterValue(SOCKET_REGISTER_CR, SOCKET_0_REGISTER_BLOCK, __au8_DataToWrite, 1); // Connect command

while(__u8_SocketStatus != 0x17) // SOCK_ESTABLISHED
vWIZNET_ReadRegisterValue(SOCKET_REGISTER_SR, SOCKET_0_REGISTER_BLOCK, &__u8_SocketStatus, 1); // Get socket status register

Thank you for your help,

More precisions:

I am using ezTerm or Hercules as TCP Server, listening the Port 31000.

I was thinking for a moment that it could be the Windows firewall which was blocking the port access but the OPEN_COMMAND works well.

I just cannot connect to the PC and I don’t see why, I always go to Time Out.

I also tried adding the subnet mask register but nothing changed.

Dear Zarck,

Would you send the packet capture ?
Your code doesn’t seem to have any problem.


Thank you for your answer.

Here are the captures. Note that it stays infinitely in the last while loop because it is never equal to 0x17.
These captures are done with a terminal open as Server and listening on the Port 31000 configured in the code above.

I did 3 captures to clearly see the packets.

Thank you for your the spi packet…

But, we want to Ethernet packet capture.
By using 'WireShark", you can capture the Ethernet packet.

Please, attach Ethernet packet capture.

Ok Sorry.

Here is a screenshot of wireshark. I think there is definitely a problem there.
I just added one code line which define the Source Port at 31001 in order to clearly see it on wireshark.

Dear Zarck,

As you know, PC program do not send SYN-ACK corresponding to SYN packet from W5500.

Please, check security setting or Socket program.

  • Check setting of firewall or vaccine program
  • Use another socket program.
  • Change port number


Hi all,

I would like to know if you have already solved this problem, I’m getting into the same trouble and I can’t go with a solution. In fact I’m following exactly the same steps as Zack did, I’ve found the way to use the chip as a server and it worked very well but when I configure it as a client it doesn’t change to ESTABLISHED state after executes the CONNECT command. Another weird situation is that when I write and then read the Sn_dport0 and Sn_dport1 registers, all I get is zero but in the datasheet these registers seems to have both W/R permissions so I don’t know what can be causing this behavior. Can you help me please?

Thanks in advance,
Juan Esteban

Another weird situation is that when I write and then read the Sn_dport0 and Sn_dport1 registers, all I get is zero

It is normal behavior. You will be able to read correct destination port number when connection will get established.

it doesn’t change to ESTABLISHED state

Which state it is stuck in? I recommend you to try connecting to PC with Wireshark installed, and capture packet information when W5500 tries to connect to this PC, and see the packet exchange.

When connection is being established, there’s packet exchange in both directions, W5500 requesting connect and PC replying back with approval or denial. You should see this “network dialogue” between devices, and see if it is wrong.


It sticks at SOCK_INIT state, it’s like if it never executes the CONNECT command. After a couple of seconds the timeout event is generated and it returns to SOCK_CLOSED state.[quote=“Eugeny, post:9, topic:964”]
I recommend you to try connecting to PC with Wireshark installed, and capture packet information when W5500 tries to connect to this PC, and see the packet exchange. You can also see the internal state transitions in the uart log window.
I’ve already done that but I don’t get any package from Wiz5500. In the following images, the first one shows the package interaction using wiz5500 as TCP client.

The second one shows the package interaction when Wiz5500 is configured as TCP server, which is completely functional.

This is the bascom code snippet i’m using to do this:

Dim server as byte : server = 0
Select Case socket_n(1)      
    Case SET_TCP_MODE :
        Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_mr , Sn_mr_tcp)    'Set TCP Mode Command'
    Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_port0 , &H13)        'Sets source port number high byte <port>=5000'
    Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_port1 , &H88)        'Sets source port number low byte  <port>=5000'
    Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_cr , Sn_cr_open)   'Excecutes OPEN command'
    socket_n(1) = SOCK_OPEN

Case SOCK_OPEN :                                                                          
    eth_read = Wiz5500_readvalue(W5500_S0_reg, W5500_ sn_sr)    'Is Channel Opened?'
    If eth_read = Sock_init Then                                'If server mode'
        If Server = 1  Then
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_cr , Sn_cr_listen)    'Excecutes LISTEN command'
            socket_n(1) = SOCK_LISTENING
        Else                                                                                                           'If client mode'
            'Server IP address'
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dipr0 , 192)
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dipr1 , 168)
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dipr2 , 1)
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dipr3 , 51)

            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dport0 , &H1B)    'Sets destination port number <port>=7000'
            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_dport1 , &H58)

            Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_cr, Sn_cr_connect)    'Executes CONNECT command'
            socket_n(1) = HANDSHAKE
        End If
    End If

    eth_read = Wiz5500_readvalue(W5500_S0_reg, W5500_sn_sr)
    If eth_read = Sock_listen Then
        socket_n(1) = HANDSHAKE
    End If

‘-------- ESTABLISHMENT ---------’
Case HANDSHAKE: ‘Establishment’
eth_read = Wiz5500_readvalue(W5500_S0_reg, W5500_sn_sr)
If eth_read = Sock_established Then
socket_n(1) = HANDSHAKE_ACK
End If

eth_read = Wiz5500_readvalue(W5500_S0_reg, W5500_sn_ir)
value = eth_read AND Sn_ir_con
If value = Sn_ir_con Then
Call Wiz5500_writevalue(W5500_S0_reg, W5500_sn_ir , Sn_ir_con) ‘Clear connect flag’
socket_n(1) = RECEIVE_DATA
End If
End Select

Sorry for the tabulation, I couldn’t get it to be organized.
Thank you for your help.


I have a question about your first screenshot. Dst port is 0, right?
If the current server is open on port 7000, it may be necessary to verify that the 7000 is properly applied to the register.

And check your server’s firewall.
There is no problem if the module is a server, but when the module connects to the server as a client, it can be blocked by the server side firewall.

DIPR (destination IP address register) is located in S0_reg block, not in common register block.

Comment seems to be wrong, you set destination port.

1 Like

Hi Kei,

Yes both the Dst Port and the Dst IP registers keep as 0 even if I write them with another values, some posts before Eugeny said that it is a normal behavior since these registers can only be read after getting the ESTABLISHED state but I’ve couldn’t find that specification for these registers in the datasheet. I’m ensuring that Wiz5500 is configured in TCP mode before setting the Dst port and IP regs as suggest the datasheet.

I had already ensured that the firewall is not blocking the connection using another laptop as TCP client to connect to my pc which was working as a server and it worked without any problem.

Thank you Eugeny I hadn’t noticed that mistake, unfortunately it doesn’t solve my problem it keeps sticking at the same state, getting the timeout event and showing nothing from Wiz5500 on the wireshark terminal. The only thing that have changed is that now Dst IP reg keeps as 0 even after writing another value on it just in the same way that happens with Dst port reg.

Perform the following already existing code

before you open the socket, in Case SET_TCP_MODE : block (checking for server being 0).
In Case SOCK_OPEN : block you just issue CONNECT command.

No, it is not the problem, in the real version I have some prints into conditionals in order to be aware whenever it reaches the condition, also I have put those instruction within an infinite loop, but the result is always the same, I can’t read the values written on Dst port and IP regs. I tried to read those regs after ESTABLISMENT state when W5500 is working as TCP server and I got my laptop IP and port so the read/write operations over those registers are working good too.

You should not do it, and it will not give you proper read until connection is established. It is this way by design, it is not a bug.

I am not sure what you mean, but you should NOT write anything to W5500 in infinite loop.

I ask you please set DIPR and DPORT values before you issue OPEN command. Currently you set DIPR and DPORT after you OPENing socket (and before you perform CONNECT command).

I’ve just did it but nothing changed :confused:.

Sorry I know I should not do such a thing but I’m a little bit desperate.

Hm… this is strange.

Let’s review everything again, thread is relatively long and code is kind of messy in it, so:

  1. you power device on (hardware reset), and then perform software reset writing bit 7 set to MR, and then wait until MR register clears to 0;
  2. you set up SIPR, SUBR, SHAR (common register block). I do not talk about interrupt registers as they are implementation dependent and should not affect socket initialization process (unless your application misuses it somehow);
  3. I never worked with no gateway configurations, I think you can leave GAR uninitialized;
  4. then you set S0_MR into value of 1. Also assume you set socket memory allocation properly so that socket is having TX and RX buffer space (Sn_RXBUF_SIZE and Sn_TXBUF_SIZE, or you leave them default);
  5. You preset DIPR, SPORT and DPORT (socket 0 registers);
  6. you issue OPEN command into S0_CR, and wait until this register clears to 0. SR should become 0x13 (or 0x00 if there’s socket open error);
  7. you issue CONNECT command; and wait until S0_CR clears. After this, in some time, SR becomes either 0x17 (connected/established), or something else. As I understand currently you have flow stuck at 0x13 at this point, right?

If it is the case and you are stuck at 0x13 after you issue CONNECT command, there’s some problem in there, because after 0x13 it should change to 0x14-0x15 (SOCK_SYNSENT) - 0x16 (SOCK_SYNRECV), and pending 0x13 means that chip is not able to even send SYN packet for some reason.

Please check your application to be compliant to my plan above, and let us know.

1 Like


I think I’ve found the solution, it was nothing related with the steps I was performing but with a time constrain. It seems that it’s necessary to wait a minimum time of 800ms (for client mode) between the execution of OPEN and CONNECT commands, only in this way the Wiz5500 could reach the ESTABLISHED state and achieve de connection with server. I don’t know why is this but I found it while I was rewriting the code out the state machine fashion putting delays between all the steps, once it works I took the delays out one by one finding out that this one between OPEN and CONNECT commands was the only one which keeps the functionality.

Thank you so much for your help.
Best regards.

1 Like