Getting data from sensor

Hi,

I have a wheather station and i want get data from station with rs232.

My station response when i put comand “OPEN” and my app send this command but no receive nothing…

I read data of rs232 with FcmDataHandler and put this data in the buffer and send to ftp server… but rs232 no receive nothing…

My code of appli.c:

/***************************************************************************/
/*  Prototypes                                                             */
/***************************************************************************/
//void cfg_uart_ppp_client( void (*entry_point) (void));
//void cfg_uart_ppp_serv  ( void (*entry_point) (void));
//void cfg_gsm_ppp_client ( void (*entry_point) (void));
//void cfg_gsm_ppp_serv   ( void (*entry_point) (void));
void cfg_gprs           ( void (*entry_point) (void));

void appli_entry_point  ( void);

bool FcmDataHandler (u16 DataLen, u8 * Data);

/***************************************************************************/
/*  Mandatory variables                                                    */
/*-------------------------------------------------------------------------*/
/*  wm_apmCustomStackSize                                                  */
/*-------------------------------------------------------------------------*/
/***************************************************************************/
#if __OAT_API_VERSION__ >= 400
const u16 wm_apmCustomStackSize = 4096;
#else
u32 wm_apmCustomStack[1024];
const u16 wm_apmCustomStackSize = sizeof(wm_apmCustomStack);
#endif

s8 FcmHdl;
bool FcmCtrlHandler (adl_fcmEvent_e Event){
    switch (Event){
        case ADL_FCM_EVENT_FLOW_OPENNED:{
            adl_atSendResponse ( ADL_AT_UNS, "ADL_FCM_EVENT_FLOW_OPENNED\n\r");
            //adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
        }
    }
    return TRUE;
}

/***************************************************************************/
/*  Function   : adl_main                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : Customer application initialisation                       */
/*                                                                         */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  InitType          | x |   |   |  Application start mode reason         */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
void adl_main ( adl_InitType_e InitType )
{
  int r;
  adl_atCmdCreate( "AT+CBST=7,0,1", 1, NULL, NULL );
  //adl_atCmdCreate("AT+IPR=9600", ADL_AT_PORT_TYPE(ADL_AT_UART1, FALSE), NULL, "*", NULL);
  adl_tmrSubscribe ( TRUE, 300, ADL_TMR_TYPE_100MS, (adl_tmrHandler_t)appli_entry_point );
  FcmHdl = adl_fcmSubscribe(ADL_FCM_FLOW_V24_UART1,FcmCtrlHandler,FcmDataHandler);
  TRACE (( 1, "Embedded Application : Main" ));

  /* Initialize the stack */
  r = wip_netInitOpts( 
      WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART1, /* WIP traces on UART1 */
      //WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_UART2,
      //WIP_NET_OPT_DEBUG_PORT, WIP_NET_DEBUG_PORT_TRACE,
      WIP_NET_OPT_END);

  /* Depending on the activated flag, take the proper config */
#if defined( OVER_UART_PPP_SERV)
  /* Initialize PPP server over UART */
  cfg_uart_ppp_serv( appli_entry_point);

#elif defined( OVER_UART_PPP_CLIENT)
  /* Initialize PPP client over UART */
  cfg_uart_ppp_client( appli_entry_point);

#elif defined( OVER_GSM_PPP_SERV)
  /* Initialize PPP server over GSM  */
  cfg_gsm_ppp_serv( appli_entry_point);

#elif defined( OVER_GSM_PPP_CLIENT)
  /* Initialize PPP client over GSM  */
  cfg_gsm_ppp_client( appli_entry_point);

#elif defined( OVER_GPRS)
  /* Initialize GPRS connection      */
  cfg_gprs( appli_entry_point);

#else
  /* Don't initialize any bearer; only 
   * localhost=127.0.0.1 will be reachable. */
  appli_entry_point();

#endif
}

And my code of entry_point.c:

#define DST_SERVER    "81.129.192.240"     /* Address the FTP server */
#define DST_USER      "tester"       /* My FTP username */
#define DST_PASSWORD  "1234" /* My FTP password */
#define DST_FILENAME  "data.txt"        /* Name of the file to put */
#define DST_ISPASSIVE FALSE             /* Active or passive mode? */

#include "adl_global.h"
#include "wip.h"

/***************************************************************************/
/*  Globals                                                                */
/***************************************************************************/

extern s8 FcmHdl;
/* Whatever data I want to send in [DST_FILENAME]. */
static u8 buffer [300];

/* How many bytes of [buffer] have already been sent. */
static int offset = 0;

static wip_channel_t 
  dst_ftp_cx   = NULL, /* channel controlling the FTP connection. */
  dst_data     = NULL; /* channel used for file data transfer. */

/***************************************************************************/
/*  Function prototypes                                                    */
/***************************************************************************/

static void evh_cx( wip_event_t *ev, void *ctx);
static void evh_data( wip_event_t *ev, void *ctx);

//Vou buscar todos os dados que vierem da rs232 e meto no buffer
//do ficheiro que vai para o servidor
bool FcmDataHandler (u16 DataLen, u8 * Data){
    char a[2];
    sprintf(a,"%s",(char *)Data);
    a[1]=0;
    strcat((char *)buffer,a);
    return TRUE;
}

/***************************************************************************/
/*  Function   : appli_entry_point                                         */
/*-------------------------------------------------------------------------*/
/*  Object     : Called once the WIP IP stack is fully initialized.        */
/*               This is the starting point of user applications.          */
/*-------------------------------------------------------------------------*/
/***************************************************************************/
void appli_entry_point() {
  adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_AT);
  char comando[]="OPEN\n\r";
  adl_fcmSendData(FcmHdl,(u8 *)comando,strlen(comando));
  adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
  
  wip_debug( "Opening FTP connection...\n");
  
    dst_ftp_cx = wip_FTPCreateOpts (
    DST_SERVER,  evh_cx, NULL,
    WIP_COPT_USER,     DST_USER,
    WIP_COPT_PASSWORD, DST_PASSWORD,
    WIP_COPT_PASSIVE,  DST_ISPASSIVE,
    WIP_COPT_END);
   
  offset=0;
  if( ! dst_ftp_cx) { wip_debug( "Can't open dst FTP connection\n"); return; }
}

/***************************************************************************/
/*  Function   : evh_data                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : Handling events happenning on the FTP connection          */
/*               control channel.                                          */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  ev                | X |   |   |  WIP event                             */
/*--------------------+---+---+---+----------------------------------------*/
/*  ctx               |   |   |   |  unused                                */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void evh_cx( wip_event_t *ev, void *ctx) {
  
  switch( ev->kind) {

  case WIP_CEV_OPEN:
    /* FTP control initialized successfully -> open data dchannel */
    dst_data = wip_putFile( dst_ftp_cx, DST_FILENAME, evh_data, NULL);
    if( ! dst_data) { wip_debug( "Can't create data channel"); return; }
    break;

  case WIP_CEV_ERROR:
    wip_debug( "Error %i on cx channel\n", ev->content.error.errnum);
    /* fall through */
  
  case WIP_CEV_PEER_CLOSE:
    wip_debug( "Closing everything\n");
    if( dst_data) { wip_close( dst_data); dst_data = NULL; }
    wip_close( dst_ftp_cx); dst_ftp_cx = NULL;
    break;
  }
}

/***************************************************************************/
/*  Function   : evh_data                                                  */
/*-------------------------------------------------------------------------*/
/*  Object     : Handling events happenning on the data transfer channel   */
/*-------------------------------------------------------------------------*/
/*  Variable Name     |IN |OUT|GLB|  Utilisation                           */
/*--------------------+---+---+---+----------------------------------------*/
/*  ev                | X |   |   |  WIP event                             */
/*--------------------+---+---+---+----------------------------------------*/
/*  ctx               |   |   |   |  unused                                */
/*--------------------+---+---+---+----------------------------------------*/
/***************************************************************************/
static void evh_data( wip_event_t *ev, void *ctx) {
  
  switch( ev->kind) {

  case WIP_CEV_OPEN:
    /* Here, we could optionnally change the threshold which triggers
     * [WIP_CEV_WRITE] events with the following setting:
      
     wip_setOpts( ev->channel, WIP_COPT_SND_LOWAT, 3000, WIP_COPT_END);
      
     * (This way, we wouldn't receive WRITE events until at least 3KB
     *  of TCP send buffer are available).
     */
    break;

  /* There is some space available in TCP send buffer, which allows me
   * to write more data from [buffer]; let's write as much data as
   * possible, up to the end of [buffer]. [nwrite] is the number of bytes
   * I actually managed to put in the TCP buffer. */
  case WIP_CEV_WRITE: {
    int nwrite = wip_write( dst_data, buffer, sizeof( buffer) - offset);
    if( nwrite < 0) { /*error*/ return; }
    offset += nwrite; /* Update the number of bytes sent. */
    if( offset == sizeof( buffer)) { /* All of [buffer] has been sent */
      wip_close( dst_data);   dst_data   = NULL;
      wip_close( dst_ftp_cx); dst_ftp_cx = NULL;
    }
    break;
  }

  case WIP_CEV_ERROR:
    wip_debug( "Error %i on channel 0x%x\n", ev->content.error.errnum, 
               ev->channel);
    wip_close( dst_data); dst_data = NULL;
    if( dst_ftp_cx) { wip_close( dst_ftp_cx); dst_ftp_cx = NULL; }
    break;
  }
}

What is wrong on my code?

Please help…

Why have you commented-out the switch to Data mode?

You need to switch to data mode!

viewtopic.php?f=5&t=3270&p=12364&hilit=+data+mode#p12364

thanks!!

In appli_entry_point i have a ADL_FCM_V24_STATE_AT before command and ADL_FCM_V24_STATE_DATA after command:

void appli_entry_point() {
  adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_AT);
  char comando[]="OPEN\n\r";
  adl_fcmSendData(FcmHdl,(u8 *)comando,strlen(comando));
  adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);

This is correct?

Please help…

Please read the link that I gave earlier!

I was read, but what you want i read? I was not informed…

I use the adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA); in my code but my station not response…

And what does that link say about waiting for events…?

If you look to my code you see i wait to event:

s8 FcmHdl;
bool FcmCtrlHandler (adl_fcmEvent_e Event){
    switch (Event){
        case ADL_FCM_EVENT_FLOW_OPENNED:{
            adl_atSendResponse ( ADL_AT_UNS, "ADL_FCM_EVENT_FLOW_OPENNED\n\r");
            adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
        }
    }
    return TRUE;
}

This is correct?

Why are you calling adl_atSendResponse after the UART has been opened by FCM?

Where do you wait for the event that tells you that the flow has switched to data mode?

Look again: viewtopic.php?f=5&t=3270&p=12364&hilit=+wait+for+the+event#p12364

In:

switch (Event){
        case ADL_FCM_EVENT_FLOW_OPENNED:{
            adl_atSendResponse ( ADL_AT_UNS, "ADL_FCM_EVENT_FLOW_OPENNED\n\r");
            adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
        }
    }

If Evento is ADL_FCM_EVENT_FLOW_OPENNED i fcmSwitchV24State to DATA its not correct?

OK, maybe there’s a language barrier here?

Look at the list of possible ADL_FCM_EVENT_… events - which one of them is the one that indicates that the flow has switched to data mode :question:

Note that the flow has to be opened first, and then switched to data mode; there is an event which indicates that the flow has opened, and there is another event which indicates that the flow has switched to data mode

Ok sorry, but I did not understand well, I read the documentation and soon put my code here, thank you for the help!

Thus I am waiting for the flow to pass to data mode and now i can send commands to my sensor, this code is correct?

switch (Event){
	case ADL_FCM_EVENT_FLOW_OPENNED:{
		adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
		adl_atSendResponse ( ADL_AT_UNS, "ADL_FCM_EVENT_FLOW_OPENNED\n\r");
		break;
	}
	case ADL_FCM_EVENT_V24_DATA_MODE:{
		adl_atSendResponse (ADL_AT_RSP, "DATA MODE\r\n" )
		break;
	}
}

thanks very much for your help!!

But it it’s in Data mode, you can’t send AT responses, can you?! :unamused:

It’s correct? I try this code but im not recieve anything of the station…

bool FcmCtrlHandler (adl_fcmEvent_e Event){    
    switch (Event){
       case ADL_FCM_EVENT_FLOW_OPENNED:{
          adl_fcmSwitchV24State(FcmHdl,ADL_FCM_V24_STATE_DATA);
          //adl_atSendResponse (ADL_AT_RSP, "ADL_FCM_EVENT_FLOW_OPENNED\n\r");
          break;
       }
       case ADL_FCM_EVENT_V24_DATA_MODE:{
          //adl_atSendResponse (ADL_AT_RSP, "DATA MODE\r\n" );
          adl_tmrSubscribe ( TRUE, 1000, ADL_TMR_TYPE_100MS, (adl_tmrHandler_t)appli_entry_point );
          break;
       }
    }
    return TRUE; 
}

Here i send the command OPEN to my station but i don’t know how make the correct [enter] to rs232 \n? \r?

void appli_entry_point() {
  char comando[]="OPEN\r";
  adl_fcmSendData(FcmHdl,(u8 *)comando,strlen(comando));
...

Please help…

someone can give a help?

:slight_smile:

FCM does no translation:

  • \n gives a single LF character;
  • \r gives a single CR character.

you suggest?

char comando[]="OPEN<LF><CR>";
adl_fcmSendData(FcmHdl,(u8 *)comando,strlen(comando));

or

char comando[]="OPENLFCR";
adl_fcmSendData(FcmHdl,(u8 *)comando,strlen(comando));

This is standard ‘C’ textbook stuff - how to use the backslash “escape sequences”.
Look it up!

The only implementation-specific detail is whether or not the system does any translation of CR to CRLF, etc - and I’ve already answered that one!

And here’s an ASCII Code table for you: asciitable.com

Thanks but you did not answer my question…

What is your suggestion?

char comando[]="OPEN<LF><CR>";

or

char comando[]="OPEN\n";

?