• AVR Freaks

Helpful ReplyTCP/IP COMMUNICATION ERROR (PIC32MZ EC STARTER KIT)

Author
sbanisetty
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2014/06/16 13:40:55
  • Location: 0
  • Status: offline
2015/03/31 13:42:15 (permalink)
0

TCP/IP COMMUNICATION ERROR (PIC32MZ EC STARTER KIT)

Hello everyone,
 
Tools being used:
PIC32 MZ EC STARTER KIT
HARMONY V1.03.01
MPLAB X IDE V2.30
xC32 V1.34
 
I am developing a Ethernet based application to read sensor data and communicate over Ethernet. Below is the piece of code which transmits the sensor data on the socket. The communication works fine for sometime but later, the communication stops and the PC is struck in a loop which is shown here.
 
while(wMaxPut <= 0)
{
bool flush_status = TCPIP_TCP_Flush(appData.socket);
wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
}
 
 
The above loop is just holding the processor if there is no space to transmit additional data. It then frees the TX buffer by flushing it (expected to do) but however, The function TCPIP_TCP_Flush(app.Data.socket) is returning false which means the flush operation was not performed. So, wMaxPut is zero always and hence, PC can't come out of the loop.
 
The full code for the APP_TCP_SERVING_CONNECTION is shown below.
 
case APP_TCPIP_SERVING_CONNECTION:
        {
            if (!TCPIP_TCP_IsConnected(appData.socket))
            {
                appData.state = APP_TCPIP_CLOSING_CONNECTION;
#ifdef _DEBUG
                WriteString("Connection was closed\r\n");
#endif
                SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
                break;
            }
            int16_t wMaxGet, wMaxPut, wCurrentChunk, test_size;
            uint16_t w, w2;
            uint8_t AppBuffer[32];
            // Figure out how many bytes have been received and how many we can transmit.
            wMaxGet = TCPIP_TCP_GetIsReady(appData.socket); // Get TCP RX FIFO byte count
            wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
            test_size = sizeof(test);
            if (init == true)
            {
                TCPIP_TCP_ArrayPut(appData.socket, test, test_size);
                TCPIP_TCP_Flush(appData.socket);
                init = false;
            }

             //sending 8bit at a time
                        for(j=0;j<4;++j)
                        {
                             temp = rxData[dataIndex]&0xff;
                             itoa(buff,temp,10);
                             //COMMENTED OUT BECAUSE THE PC IS STRUCK IN THIS LOOP. WHEN I COMMENTED AND DEBUGGED, THE COMPUTER HUNGS UP IN DEBUG MODE
                             wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
                             while(wMaxPut <= 0)
                             {
                                 bool flush_status = TCPIP_TCP_Flush(appData.socket);
                                 wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
                                 //SYS_CONSOLE_MESSAGE("I'm full \r\n");

                             }
                             //TCPIP_TCP_Put (appData.socket, buff);
                             TCPIP_TCP_ArrayPut(appData.socket, buff, 4);
                             TCPIP_TCP_Flush(appData.socket);
                             rxData[dataIndex]>>=8;
                             if (j == 3)
                                 break;
                             TCPIP_TCP_Put (appData.socket, ',');
                             //TCPIP_TCP_ArrayPut(appData.socket, ",", 1);
                             TCPIP_TCP_Flush(appData.socket);
                        }
                        TCPIP_TCP_Put (appData.socket, ';');
                        //TCPIP_TCP_ArrayPut(appData.socket, ";", 1);
                        TCPIP_TCP_Flush(appData.socket);
                        //SYS_CONSOLE_MESSAGE("I'm reading data, but couldn't print! am sorry!:( \r\n");

                    dataIndex++;
                if (dataIndex == BUFFER_SIZE)
                {
                    #ifdef _DEBUG
                    WriteString("Done printing the complete buffer \r\n");
                    #endif
                SYS_CONSOLE_MESSAGE("done printing complete buffer \r\n");
                appData.state = APP_BUFFER_ACCUMULATION;
                dataIndex = 0;
                }

            }
        
        break;

 
When the PC is struck in the while loop, the output stops abruptly (communication stops) as shown below. I have circled it please find it.

When I remove the loop, the result is as shown below. The communication halts for few micro seconds and starts again and the result is some data is lost. (as indicated by the circle below). It should be .....;0,0,0,0;...... but it is ....;0,00,0,0,0;.....

 
I tried my best to explain the situation. If you need any additional information to resolve the issue please let me know. Any suggestions is appreciated. This issue is blowing my mind for the last few weeks. 
 
Thanks,
Santosh.
 
 
 

Attached Image(s)

#1
rainad
Moderator
  • Total Posts : 1157
  • Reward points : 0
  • Joined: 2009/05/01 13:39:25
  • Location: 0
  • Status: offline
Re: TCP/IP COMMUNICATION ERROR (PIC32MZ EC STARTER KIT) 2015/04/08 15:31:21 (permalink) ☄ Helpfulby sbanisetty 2015/04/09 06:49:20
4 (2)
Not sure I understand what the code:
while(wMaxPut <= 0)
{
    bool flush_status = TCPIP_TCP_Flush(appData.socket);
    wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
}
tries to achieve.
The TCPIP_TCP_PutIsReady() always returns >=0, not a negative value.
If it returns 0, it means that it cannot allocate another TX packet for TCP, probably there's no more free memory.
If the TCPIP_TCP_Flush() returns 0 it means that either it cannot find memory to send the data or there's nothing that could be sent.
- Anyway, TCPIP_TCP_Flush() should be called sparingly, only in special conditions.
For example when the application knows that it has ended the data for that socket, it's about to close the socket and it does not make sense to leave the socket waiting for data.
Calling TCPIP_TCP_Flush() interferes with the TCP state machine and severly degrades the performance.
The normal operation is to put data into the TCP socket using TCPIP_TCP_ArrayPut () and the TCP will take care of it in the most efficient way.
- Another thing is that this kind of tight loop is not the right approach.
For TCP to do it's job, packets to be sent and returned to the stack, the stack state machine has to run.
So try to take a non-blocking approach here: if there's not enough space in the TX buffer for you to put your data, return and try again next time.
- Finally, it seems that the code doesn't check for the return value of the TCPIP_TCP_ArrayPut().
There's no guarantee that the whole data went through.
This is how TCP is supposed to work, it has a limited buffer.
This may explain the loss of data, especially if you try to send big chunks of data.
#2
Jump to:
© 2019 APG vNext Commercial Version 4.5