Olimex Support Forum

Microcontrollers => PIC => Topic started by: ALFREDOSKY on August 22, 2014, 09:26:45 PM

Title: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on August 22, 2014, 09:26:45 PM
Hi all, i tried to implement tcp socket connection demo from microchip running GenericTCPClient() routine, this one connect to www.google.com server and print the response in hyperterminal through usart. the routine always break in if(!TCPIsConnected(MySocket)), it means, always TRUE.

void GenericTCPClient(void)
{
BYTE i;
WORD w;
BYTE vBuffer[9];
static DWORD Timer;
static TCP_SOCKET MySocket = INVALID_SOCKET;
static enum             _GenericTCPExampleState
{
            SM_HOME = 0,
            SM_SOCKET_OBTAINED,
            #if defined(STACK_USE_SSL_CLIENT)
               SM_START_SSL,
            #endif
            SM_PROCESS_RESPONSE,
            SM_DISCONNECT,
            SM_DONE
} GenericTCPExampleState = SM_DONE;

        //StatLEDSet(3,TRUE);

switch(GenericTCPExampleState)
{
case SM_HOME:
// Connect a socket to the remote TCP server
//MySocket = TCPOpen((DWORD)(PTR_BASE)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
                        MySocket = TCPOpen((DWORD)&ServerName[0], TCP_OPEN_RAM_HOST, ServerPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
                       
// Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available
// If this ever happens, you need to go add one to TCPIPConfig.h
if(MySocket == INVALID_SOCKET)
break;

#if defined(STACK_USE_UART)
//putrsUART((ROM char*)"\r\n\r\nConnecting using Microchip TCP API...\r\n");
#endif

GenericTCPExampleState++;
Timer = TickGet();                       
break;

case SM_SOCKET_OBTAINED:                       
// Wait for the remote server to accept our connection request
if(!TCPIsConnected(MySocket))
{
// Time out if too much time is spent in this state
if(TickGet()-Timer > 5*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState--;
}
break;
}                       
Timer = TickGet();

    #if defined (STACK_USE_SSL_CLIENT)
            if(!TCPStartSSLClient(MySocket,(void *)"thishost"))
                break;
GenericTCPExampleState++;
break;

        case SM_START_SSL:
            if (TCPSSLIsHandshaking(MySocket))
            {
if(TickGet()-Timer > 10*TICK_SECOND)
{
// Close the socket so it can be used by other modules
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState=SM_HOME;
}
                break;
            }
    #endif

// Make certain the socket can be written to
if(TCPIsPutReady(MySocket) < 125u)
break;

// Place the application protocol data into the transmit buffer.  For this example, we are connected to an HTTP server, so we'll send an HTTP GET request.
TCPPutROMString(MySocket, (ROM BYTE*)"GET ");
TCPPutROMString(MySocket, RemoteURL);                       
TCPPutROMString(MySocket, (ROM BYTE*)" HTTP/1.0\r\nHost: ");
TCPPutString(MySocket, ServerName);
TCPPutROMString(MySocket, (ROM BYTE*)"\r\nConnection: close\r\n\r\n");

// Send the packet
TCPFlush(MySocket);                       
GenericTCPExampleState++;
break;

case SM_PROCESS_RESPONSE:
// Check to see if the remote node has disconnected from us or sent us any application data
// If application data is available, write it to the UART
if(!TCPIsConnected(MySocket))
{
GenericTCPExampleState = SM_DISCONNECT;
// Do not break;  We might still have data in the TCP RX FIFO waiting for us                               
}
// Get count of RX bytes waiting
w = TCPIsGetReady(MySocket);

// Obtian and print the server reply
i = sizeof(vBuffer)-1;
vBuffer[i] = '\0';
while(w)
{
if(w < i)
{
i = w;
vBuffer[i] = '\0';
}
w -= TCPGetArray(MySocket, vBuffer, i);
#if defined(STACK_USE_UART)
//linea comentada ALF putsUART((char*)vBuffer);
                                UartCommPrintSafe(vBuffer); //linea agregada ALF

                                #endif

// putsUART is a blocking call which will slow down the rest of the stack
// if we shovel the whole TCP RX FIFO into the serial port all at once.
// Therefore, let's break out after only one chunk most of the time.  The
// only exception is when the remote node disconncets from us and we need to
// use up all the data before changing states.
if(GenericTCPExampleState == SM_PROCESS_RESPONSE)
break;
}

break;

case SM_DISCONNECT:
// Close the socket so it can be used by other modules
// For this application, we wish to stay connected, but this state will still get entered if the remote server decides to disconnect
TCPDisconnect(MySocket);
MySocket = INVALID_SOCKET;
GenericTCPExampleState = SM_DONE;
break;

case SM_DONE:
                        if(StatLEDGet(3) == TRUE)  {                           
                            GenericTCPExampleState = SM_HOME;
                        }
                                                   
/*StatLEDSet(3,TRUE);             //ENCIENDE LED3
                        // Do nothing unless the user pushes BUTTON1 and wants to restart the whole connection/download process
if(BUTTON1_IO == 0u){
GenericTCPExampleState = SM_HOME;
                               // StatLEDSet(3,TRUE);
                                PORTClearBits(IOPORT_D, BIT_2); //APAGA LED3
                                //vTaskDelay(2000);
                        }*/
break;
}
}
 

I read a lot to find the error but nothing seems help. any suggestion would be helpful.
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on August 25, 2014, 08:51:40 PM
Well i read something about TCPIP stack version, some guys with the same problem solved it moving to previous version of stack. I will try, but i see TCPIP stack_lib folder that contains TCP_lib.x proyect, i think this was done with the lastest version of stack, how can i do a similar proyect with older version, how I can do to build and run correctly?  :(
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on August 27, 2014, 06:46:53 PM
I answer to myself, i have tried using GenericTCPServer() routine to implement client-server comunication, Olimex board as server and my PC as client, i have an app running on my PC that allow me connect with tcp sockets, this app was written in java. This configuration works with no problem, the demo from microchip returns upper case eco from character sent by client, and as i said, this works fine  ;D , but not the case when using GenericTCPClient() (Olimex board as Client an PC as Server), and i do not why!!! all seems ok related with TCPIPConfig.h file configuration, please any help would be nice!  :-\
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ejphendriks@outlook.com on August 28, 2014, 12:31:59 PM
Hi ALFRADOSKY,

I read you post and I think we want to create a similar sort of project.

What I want to create is a bare TCP/IP stack that bridges transparently to UART (as a first step).

Only difference is that I am using the PIC-WEB-BOX instead of the PIC32

Unfortunately as a newbie in this area I can't be of much help to you but I will be following your updates with much interest. Please keep talking to yourself ;)

Regards,
Erwin
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on August 29, 2014, 06:25:20 PM
Hi Erwin, i'm a newbie too, and it's a long learning curve for me. All this things i didn´t know it, i didn't write in c/c++ code before, I wrote in assembler code for some apps in 8 bits and 16 bits microchip microcontroller; i try to apply all the logic that can be to understand stack, uart, i2c, pwm, etc writen in c;struct, union and pointers things i still learning. This board(PIC32 MAXI WEB) came with FreeRTOS, and i had to learn it  8). For your app, i think there must be some demo for that; get all the possible documents, ask Olimex and Microchip forums and simultaneously performs tests, reviewed every thing, every line of code, and achieved it that your application will run properly.    ;D
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on September 09, 2014, 06:50:29 PM
Well, contiuning talking to mysef i have a duty to inform, GenericTCPClient() finally works, the problem was solved by freebsd, a guy from microchip forum, all thanks to him. the problem was the buffer for tx and rx, them was small(even now, i don´t know why  :o ) i have to increase both buffer size . All works fine now! i hope this helps someone.  8)
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ejphendriks@outlook.com on September 18, 2014, 10:13:39 PM
That is Great news! Because still have some issues with Olimex release as well.
I rather switch to a vanilla microchip tcp stack that comes with more active support.
Can you share that stack with me please?

Ejphendriks@outlook.com
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ejphendriks@outlook.com on September 18, 2014, 10:15:50 PM
Or did you just take the standard MLA TCP stack ?

And how much did you increase the buffer size?


Thnx in advance
Title: Re: TCP sockets PIC32 MAXI WEB
Post by: ALFREDOSKY on September 19, 2014, 08:13:50 PM
Hello Erwin, PIC32 MAXI WEB came with the last TCPIP stack(i guess) the version is v5.42.02, this one was integrated in FreeRTOS task, in other words, this card came with that stack, i didn't download any libraries. I increased a lot both buffers

before, i got this:
{TCP_PURPOSE_GENERIC_TCP_CLIENT, TCP_PIC_RAM, 200, 100},

after, i do this:
{TCP_PURPOSE_GENERIC_TCP_CLIENT, TCP_PIC_RAM, 700, 700},

i just said i don't understand why CLIENT is need that amount of bytes...and i still didn't.