About TCP Client


#1

Hi all,

In my fastrack (firmware 6.55,open at v3.10) I want to realize a TCP client.

In OPEAN AT Sample I have read about FTP and HTTP_Server but I do not find samples that can help me.

You can help me?


#2

Hey man,

Try this code…(sorry about the comments, they are all in portuguese) and do not forget to change the parameters at Config function !!!

Good luck !!!

Gus

/**************************************************************************
	PI Componentes - Gustavo Nunes
***************************************************************************/

/**************************************************************************
	File         : Appli.c
---------------------------------------------------------------------------
	Data Criacao : 20-Out-2005

	Ult. Modif.  : 29-Out-2005

	Descricao    : Abre conexao TCP
***************************************************************************/

#include "adl_global.h"

#include "ed_gprs.h"
#include "ed_dialup.h"
#include "ed_smtp.h"
#include "ed_msgcodes.h"

#include "ed_socket.h" // For "TCP Socket" functions

#include "ed_common.h" // For "ed_Init" function

/***************************************************************************
	Mandatory variables
----------------------------------------------------------------------------
	wm_apmCustomStack
	wm_apmCustomStackSize
----------------------------------------------------------------------------
****************************************************************************/

u32 wm_apmCustomStack [ 256 ];
const u16 wm_apmCustomStackSize = sizeof ( wm_apmCustomStack );


/***************************************************************************
	Global variables
----------------------------------------------------------------------------
e_gprsSetupParams_t		: Parameters for 
ed_SocketSetupParams_t	: Parameters for 
----------------------------------------------------------------------------
****************************************************************************/

ed_gprsSetupParams_t	ParamsGPRS;	//Parametros para conexao GPRS

ed_SocketSetupParams_t  ParamsSocket; // Parametros para Socket TCP

int a=0;
/***************************************************************************
functions
****************************************************************************/

void Config (){

	// GPRS configuration

	ParamsGPRS.Cid = 1; //Configura canal 1 de conexao (4 canais estao disponiveis)
	ParamsGPRS.Mode = 1; //Configura o modem para conexao GPRS

	wm_strcpy(ParamsGPRS.ApnServ,"tim.br");  //APN da servidora TIM

	wm_strcpy(ParamsGPRS.ApnUn,"tim");
	wm_strcpy(ParamsGPRS.ApnPw,"tim");

	// Socket TCP configuration

	ParamsSocket.TcpPort = 3000;

	wm_strcpy(ParamsSocket.TcpServ,"200.234.31.71");

}

void HelloWorld_TimerHandler ( u8 ID )
{
	char Saida[10];

    /* Hello World */
    //TRACE (( 1, "Embedded : Hello World" ));
    //adl_atSendResponse ( ADL_AT_UNS, "\r\nHello World from Open-AT\r\n" );

	//wm_strcpy(Saida,"Teste");

	a++;

	wm_sprintf(Saida,"%d", a);

	ed_SendDataExt(Saida,wm_strlen(Saida),FALSE,ED_ID_TCPSOCKET_1);
}

void DataTXHandler (u16 MaxLen, TeDHandle id){
	char msg[40];

	wm_sprintf(msg,"TX Handler: %d\n\r", a);
	adl_atSendResponse ( ADL_AT_UNS, msg);

}

u16 DataRXHandler (u16 DataLen, u8 * Data, TeDHandle id){

	char msg[20];

	char Saida[10];

	adl_atSendResponse ( ADL_AT_UNS, "RX Handler: ");

	wm_sprintf(msg,"%c", *Data);

	adl_atSendResponse ( ADL_AT_UNS, msg);

	adl_atSendResponse ( ADL_AT_UNS, "\n\r");

	if (!wm_strncmp(msg,"0",1)){

		adl_atSendResponse ( ADL_AT_UNS, "Opa!!!\n\r");

		wm_strcpy(Saida,"G");

		ed_SendDataExt(Saida,1,TRUE,ED_ID_TCPSOCKET_1);

	}
	
}

void TCPHandler (s32 ResponseCode, TeDHandle id){

	char msg[20];

	adl_atSendResponse ( ADL_AT_UNS, "TCP Socket Handler: ");

	sprintf(msg,"%d",ResponseCode);

	adl_atSendResponse ( ADL_AT_UNS, msg);

	adl_atSendResponse ( ADL_AT_UNS, "\r\n");

	//Tratamento dos eventos de retorno
	switch(ResponseCode)
	{
	case ED_INFO_WAITING_FOR_DATA:
		adl_tmrSubscribe ( TRUE, 30, ADL_TMR_TYPE_100MS, HelloWorld_TimerHandler );
		break;
	}

}

void GPRSTimerHandler (u8 timerId){

	s8 sreturnCode = ERROR;

	ed_dialupGenInfos_t * Infos;

	adl_atSendResponse ( ADL_AT_UNS, "GPRS Link: ");

	ed_DialupGetGeneralInformations(&Infos);

	adl_atSendResponse ( ADL_AT_UNS, "gg" );

}

void DialupHandler(s32 ResponseCode){

	char msg[20];

	s8 sreturnCode = ERROR;

	switch (ResponseCode){

	case ED_OK_GPRS_SESSION_SET:  //GPRS conectado

		adl_atSendResponse ( ADL_AT_UNS, "ED_OK_GPRS_SESSION_SET\n\r");

		//adl_tmrSubscribe(TRUE,20,ADL_TMR_TYPE_100MS,GPRSTimerHandler);

		sreturnCode = ed_SocketSetConfig(ED_ID_TCPSOCKET_1,&ParamsSocket);

		if(sreturnCode==0){

			//TCP Socket configurado

			adl_atSendResponse ( ADL_AT_UNS, "TCP Socket configurado com sucesso\n\r");

			sreturnCode = ERROR;

			sreturnCode = ed_SocketTCPStart(ED_ID_TCPSOCKET_1,0,TCPHandler,DataRXHandler,DataTXHandler);

			if(sreturnCode == 0){

				adl_atSendResponse ( ADL_AT_UNS, "TCP Ok\r\n");

			}else{

				adl_atSendResponse ( ADL_AT_UNS, "Falha TCP: ");

				switch (sreturnCode){

				case ED_ERR_INVALID_ARG:
					adl_atSendResponse ( ADL_AT_UNS, "ED_ERR_INVALID_ARG\r\n");
				break;

				default:
					adl_atSendResponse ( ADL_AT_UNS, "Demais erros\r\n");
				break;

				}
					
			}

		}else{

			//Falha na configuracao do Socket TCP

			adl_atSendResponse ( ADL_AT_UNS, "Falha na configuracao do Socket TCP: ");

			sprintf(msg,"%d",sreturnCode);

			adl_atSendResponse ( ADL_AT_UNS, msg);
		}

	break;

	default:
		adl_atSendResponse ( ADL_AT_UNS, "Falha no link GPRS\n\r");
	break;

	}
}

void TimerHandler(u8 timerid){

	char msg[20];

	s8	sreturnCode;

	sreturnCode = ed_DialupConnectionStart(DialupHandler);

	if(sreturnCode == 0){

		//GPRS Conectado

		adl_atSendResponse ( ADL_AT_UNS, "GPRS Online\n\r");

	}else{

		//Falha na conexao GPRS

		adl_atSendResponse ( ADL_AT_UNS, "GPRS Error: ");

		sprintf(msg,"%d",sreturnCode);

		adl_atSendResponse ( ADL_AT_UNS, msg);

	}
}

bool GPRSHandler(adl_atResponse_t *paras){

	s8		sreturnCode = ERROR;
	char	msg[20];

	//adl_atSendResponse ( ADL_AT_UNS, paras->StrData);

	if( !wm_strncmp(paras->StrData,"\r\nOK",4)){

		//Comando AT+CGATT executado com sucesso

		sreturnCode = ed_GprsSetConfig(&ParamsGPRS);

		if (sreturnCode == 0){

			//Parametros OK
			adl_atSendResponse ( ADL_AT_UNS, "GPRS parameters are ok\n\r");

			adl_tmrSubscribe(FALSE,10,ADL_TMR_TYPE_100MS,TimerHandler);
			
		}else{

			//Parametros com erro
			adl_atSendResponse ( ADL_AT_UNS, "Errors trying to send GPRS parameters: ");

			sprintf(msg,"%d",sreturnCode);

			adl_atSendResponse ( ADL_AT_UNS, msg);
		}
	}else{

		//Erro ao tentar usar o comando AT+CGATT
		adl_atSendResponse ( ADL_AT_UNS, "Connection failed\n\r");
	}	

	return TRUE;
}

void GPRSConnection (){

	adl_atSendResponse ( ADL_AT_UNS, "Openning GPRS connection...");

	adl_atCmdCreate("AT+CGATT=1",TRUE,GPRSHandler,"*",NULL);
}


/***************************************************************************
	Function   : SimCard
----------------------------------------------------------------------------
	Description: Display the SimCard Events
----------------------------------------------------------------------------
	Variable Name     |IN |OUT|GLB|  Utilisation
----------------------+---+---+---+-----------------------------------------
	Event             |   |   |   | Sim Card Events
----------------------+---+---+---+-----------------------------------------
****************************************************************************/

void SimCard (u8 Event){

	switch(Event){

	//Normal Events
	case ADL_SIM_EVENT_PIN_OK:
			adl_atSendResponse ( ADL_AT_UNS, "PIN Code Ok\n\rConnecting...\n\r");
	break;
	case ADL_SIM_EVENT_REMOVED:
			adl_atSendResponse ( ADL_AT_UNS, "SimCard Removed\n\r");
	break;
	case ADL_SIM_EVENT_INSERTED:
			adl_atSendResponse ( ADL_AT_UNS, "SimCard Inserted\n\r");
	break;
	case ADL_SIM_EVENT_FULL_INIT:

			adl_atSendResponse ( ADL_AT_UNS, "Modem on-line !\n\r");

			GPRSConnection(); // Start the GPRS connection
	break;

	//Error Events
	case ADL_SIM_EVENT_PIN_ERROR:
			adl_atSendResponse ( ADL_AT_UNS, "PIN Code Incorreto\n\r");
	break;

	/*Erro quando se e necessario inserir o PIN Code.
	  Por comandos AT, insera o PIN Code usando
	  AT+CPIN="xxxx", onde xxxx e o codigo de 4 digitos*/
	case ADL_SIM_EVENT_PIN_WAIT:
			adl_atSendResponse ( ADL_AT_UNS, "Enter the PIN Code\n\r");
	break;
	}
}

/***************************************************************************
	Function   : adl_main
----------------------------------------------------------------------------
	Description: Main function
----------------------------------------------------------------------------
	Variable Name     |IN |OUT|GLB|  Utilisation
----------------------+---+---+---+-----------------------------------------
	InitType          |   |   |   | Application start mode reason
----------------------+---+---+---+-----------------------------------------
****************************************************************************/

void adl_main ( adl_InitType_e InitType )
{

	s8 sreturnCode;

	u8 Teste;

	char	msg[20];

	adl_atSendResponse ( ADL_AT_UNS, "TCP Socket Application\n\r");

    TRACE (( 1, "Embedded Application : Main" ));

	//Sim Card handler
	adl_simSubscribe(SimCard,NULL);

	sreturnCode = ed_Init(); //IP libraries inicialization


	Config(); // Config the GPRS/TCP socket parameters


}

#3

Hi gmnvh

Your sample will be very important to me


#4

Hi gmnvh,

I am trying your code, I have opened a new project with Open At Project Wizard and after I have copied in appli.c your code.

But when I compile I read this error:

c:\Ex2\src\appli.c(18): fatal error C1083: “ed_gprs.h”: No such file or directory

I think that this error depends on one bad setting of my Visual C++.Net

Can you help me on the relative setting please?

ThanKs


#5

Have you checked that the ed_gprs.h file is actually present on your hard drive?

Note that ed_gprs.h is the very first of the TCP/IP files included by the code:

/************************************************************************** 
   PI Componentes - Gustavo Nunes 
***************************************************************************/ 

/************************************************************************** 
   File         : Appli.c 
--------------------------------------------------------------------------- 
   Data Criacao : 20-Out-2005 

   Ult. Modif.  : 29-Out-2005 

   Descricao    : Abre conexao TCP 
***************************************************************************/ 

#include "adl_global.h" 

#include "ed_gprs.h" 
#include "ed_dialup.h" 
#include "ed_smtp.h" 
#include "ed_msgcodes.h" 

#include "ed_socket.h" // For "TCP Socket" functions 

#include "ed_common.h" // For "ed_Init" function

Do you actually have the TCP/IP add-on?

Are you sure that it’s installed properly?

On my installation, they are in the C:\OpenAT\OtherLibs\TCPIP\v3.03\inc folder


#6

Hi all,

I think to have resolved the problem.

I have controlled and after I have set up the property of my project in visual C++.Net.

I do not know if the procedure is corrected but I have executed the debug

Hi and thanks


#7

Hi all,

Anyway…here is the procedure to add the TCP/IP library on Open At 3.10.

On “Open At Project Wizard” there is a field called “wmnew script additional options:”. Put the following text there:

-otherlib TCPIP

If you are doing that in an existing project do not forget to use Build All to compile the project.

Rgds,
Gus


#8

Hi gmnvh,

A last question about "-otherlib TCPIP ":

It is valid for target mode too ?

Hi and thanks very much


#9

:smiley:

Yeah man…U can add it to your project and compile for Debug and Target mode…

As I said, just remeber to use Rebuild All (just at the first time) if you add the library in a existing project… :wink:

Gus