0
votes

I am trying to establish TCP communication between my PC and LWIP MCU. LWIP UDP and ICMP seems that work correctly. Note that LWIP works as TCP sender and PC-Python socket based script as receiver. The first cycle connection accept seems to be acknowledged correctly. However, during TCP packet sending it seems like the PC ACK are not correct - LWIP retransmits the packet.

LWIP code sender's side looks like this (obvious LWIP_Process function to handle received packets and refresh time, the initialization of MCU is not shown....)

static char first_pld[32] = {"Thank you people"};
/* -----------Set network id ----------*/
IP4_ADDR(&ipaddr, 192u, 168u, 100u, 2u);
IP4_ADDR(&ipaddr_pc, 192u, 168u, 100u, 10u);
/* -----------Force ARP discover routine ----------*/
LWIP_arp_query(&ipaddr_pc);
LWIP_Process();
/* -----------Start TCP communication ----------*/
MyTCP = tcp_new();
LWIP_Process(); 
tcp_bind(MyTCP, &ipaddr, 504);
LWIP_Process();
tcp_connect(MyTCP, &ipaddr_pc, 504, MyConnectedFn);
LWIP_Process();
tcp_sent(MyTCP, MySentFn);
LWIP_Process();
printf("TCP available to send: %04d\r\n", tcp_sndbuf(MyTCP));
LWIP_Process();
tcp_write(MyTCP, first_pld, strlen(first_pld), 0);
LWIP_Process();
tcp_output(MyTCP);
LWIP_Process();
while (1) {
    LWIP_Process();
    }
}

Python receiver is:

import socket
import sys
import time


buff_size = 128

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
modbus_port = 504
this_addres = (b'192.168.100.10', modbus_port)
sock.bind(this_addres)


# Listen for incoming connections
sock.listen(1)

connection_status = 0
while connection_status == 0:
        # Wait for a connection
        print('waiting for a connection')
        connection, client_address = sock.accept()
        connection_status =  1
print("Connected to", client_address)

while 1:
        payload = connection.recv(buff_size)
        print(payload)

The wireshark log shows ARP session, accepting the TCP communication, but TCP packet send does not look correct - LWIP re-sends..

Wireshark Log Picture https://ibb.co/Q9fbxZ9

Any helpful tip please ? Currently I am trying to find similar solved trouble..

1

1 Answers

0
votes

I found the solution how to fix that:

Be careful when creating LWIP netinterface.c/h files.

Because I wanted to omptimize length of passed buffers (of frames) I created function to schrink arrays including (sometimes) huge amount of '\0' characters. I did this because my MAC PHY device in receive mode cannot return array of packet smaller than 60 bytes, so for example ARP frame (normally length 42) is filled with nulles in the end.

However, '\0' may be also value of the packet in the end (especially when TCP ACK). I automatically threw this nulls and LWIP could not handle this as valid packet. So now, I pass whole array of packets and let LWIP handle it on its own...