serially transmitted data is also copied to received data

hello,
i am transmitting some data to the GSM modem using the Software Serial port. I have configured pin 8 as Rx pin and pin 9 as the Tx pin. i send some AT commands using the .println method and then wait for response from the GSM modem. The code is as below

int SendCommand(char *cmd, char *PosResp, int RxDelay )
{
char RxBuf[50] ;
int len ;
long int startTime ;

//send the command
GSM_PORT.println(cmd);

Diag.print(cmd) ;

delay(200);

startTime = millis() ;
do
{
len = 0 ;
while( GSM_PORT.available() )
{
RxBuf[len++] = GSM_PORT.read() ;
//Diag.print( RxBuf[len-1] ) ;
}
}while( !strstr( RxBuf, PosResp) && !strstr( RxBuf, "ERROR") && ((millis()-startTime)<RxDelay) ) ;

if( strstr( RxBuf, PosResp) )
{
Diag.println(" ---- + resp");
//Diag.print( "RxBuf=" );
//Diag.println( RxBuf );

return 1;
}
else
{
Diag.println(" ---- - resp");
Diag.print( "RxBuf=" );
Diag.println( RxBuf );

return 0;
}
}

whenever i print the RxBuf i see that the Transmitted data is also copied to the RxBuf. Any idea why this is happening?

You need to post a complete program. The problem often lies in the other parts.

And please use the code button </> so your code looks like this and is easy to copy to a text editor

...R

here is the code. i am trying to post the analog voltage to a thinkspeak channel using GPRS.

#include <SoftwareSerial.h>

#define HTTP_DOWNLOAD_LATENCY  5000

#define Diag Serial

SoftwareSerial GSM_PORT(8, 9); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Diag.begin(57600);

  do
  {
    Diag.println("Waiting for GSM modem to initialise" ) ;
  }while( !IsModemAlive );

  // set the data rate for the SoftwareSerial port
  GSM_PORT.begin(9600);
  
  if( IsSIMRegstrd() )
    DiagMsg( "SIM registered to n/w", 0 );
  else
    DiagMsg( "Pls insert working SIM", 1 );
}

int IsModemAlive( void )
{
  if( SendCommand("AT", "OK", 200) )
    return 1 ;
  
  return 0 ;
}

int IsSIMRegstrd( void )
{
  int count = 5 ;
  do
  {
    if( SendCommand("AT+CPIN?", "+CPIN: READY", 200) )
      if( SendCommand("AT+CREG?", "+CREG: 0,1", 200) )
        return 1 ;
      else
        count-- ;
    else
      count-- ; 
  }while( count>0 ) ;
  
  return 0 ;
}

int SendCommand(char *cmd, char *PosResp, int RxDelay )
{
  char RxBuf[50] ;
  int len ;
  long int startTime ;
 
  //send the command
  GSM_PORT.println(cmd);
    
  Diag.print(cmd) ;
  
  delay(200);
  
  startTime = millis() ;
  do
  {
    len = 0 ;
    while( GSM_PORT.available() )
    {
      RxBuf[len++] = GSM_PORT.read() ;
      //Diag.print( RxBuf[len-1] ) ;
    }
  }while( !strstr( RxBuf, PosResp) && !strstr( RxBuf, "ERROR") && ((millis()-startTime)<RxDelay) ) ;

  
  if( strstr( RxBuf, PosResp) )
  {
    Diag.println(" ---- + resp");
    //Diag.print( "RxBuf=" );
    //Diag.println( RxBuf );
    
    return 1;
  }
  else
  {
    Diag.println(" ---- - resp");
    //Diag.print( "RxBuf=" );
    //Diag.println( RxBuf );
    
    return 0;
  }
}

int updateTSField(char * key, int field_idx, int value)
{
  char httpdata[40]="key=", buf[5] ;
  
  strcat(httpdata, key);
  strcat(httpdata,"&field");
  strcat(httpdata,itoa(field_idx, buf, 10));
  strcat(httpdata,"=");
  strcat(httpdata,itoa(value, buf, 10));
  
  if( httpPOST( httpdata ) )
   return 1 ;
  else
    return 0 ;
}

int httpPOST( char *data )
{
  int state, timeout ;
  char httpdata_cmd[50]="AT+HTTPDATA=" ;
  char buf[5] ;
  
  //setup bearer settings for GPRS 
  if( !setupGPRS() )
  {
    DiagMsg( "IP address not assigned", 0 );
    
    return 0;
  }

  //construct the AT+HTTPDATA=<no_of_bytes_for_download>,<latency_time>
  strcat(httpdata_cmd, itoa(strlen(data), buf, 10));
  strcat(httpdata_cmd, ",");
  strcat(httpdata_cmd, itoa(HTTP_DOWNLOAD_LATENCY, buf, 10));
  
  Diag.print("httpdata_cmd= ");
  Diag.println(httpdata_cmd);
  Diag.print("data= ");
  Diag.println(data);

  state = 0 ; 
  //continue with HTTP POST 
  if( SendCommand( "AT+HTTPINIT", "OK", 200) )
    if( SendCommand( "AT+HTTPPARA=\"CID\",1", "OK", 200) )
      if( SendCommand( "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update\"", "OK", 500) )
          if( SendCommand( httpdata_cmd, "DOWNLOAD", 800) )
            if( SendCommand( data, "OK", 200) )
              if( SendCommand( "AT+HTTPACTION=1", "+HTTPACTION:1,200", 7000) )
                state = 1 ;
              else
                SendCommand( "AT+HTTPREAD", "OK", 3000);

  SendCommand( "AT+HTTPTERM", "OK", 200);
  return(state) ; 
}

int setupGPRS( void )
{
  int timeout = 5 ;
  
  do
  {
      if( SendCommand( "AT+SAPBR=2,1", "+SAPBR: 1,1", 200) )
        break ;

      if( !SendCommand("AT+CGATT?","+CGATT: 1", 200) )
        DiagMsg( "GPRS not attached", 0 );
  
      if( !SendCommand( "AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 200) )
        DiagMsg( "SAPBR=3,1,contype,gprs failed", 0 );
    
      if( !SendCommand( "AT+SAPBR=1,1", "OK", 2000) )
        DiagMsg( "SABPR=1,1 failed", 0 );
  
       if( SendCommand( "AT+SAPBR=2,1", "+SAPBR: 1,1", 200) )
         break ;
       else
         timeout = timeout - 1 ;
  }while( timeout ) ;

  if( timeout>0 )
    return 1 ;
  else
    return 0 ;
}

void DiagMsg( char *msg, int loop)
{
  Diag.println( msg ) ;
  
  if( loop )
    while( 1 ) ;
}


void loop() // run over and over
{
  int postfail_count = 5 ;
  
  do
  {
    int adcValue = analogRead( A0 ) ;
    
    int voltage = adcValue * (5000.0/1023.0) ; 
    
    Diag.print("voltage= ");
    Diag.print(voltage);
    Diag.println("mV");
    
    if( !updateTSField( "D9R6K3Y1NNMNZPCD", 1, voltage ) )
    {
      DiagMsg( "Channel post - failed", 0 );
      postfail_count--;
    }
    else
       DiagMsg( "Channel post - success", 0 );

    delay(3000);
  }while( postfail_count>0 );
  
  DiagMsg( "Posting stopped due to repeated failure in posting", 1 ) ;
}

Can you post one or two examples of the output you are getting so I can relate it to specific lines in your program.

...R

This is what i see on the Serial monitor.

AT+CPIN? is the Tx data to the GSM modem from the Arduino(using GSM_PORT.println) and the expected response is +CPIN: READY and OK. But the RxBuf which contains the received data(check the code) which i am printing to the serial monitor after reception contains AT+CPIN? as well as +CPIN: READY and OK.

My code works since i use strstr to compare the received data with the expected response but i would like to know why the transmitted data is also copied to the receive buffer RxBuf.

i have a terminal program connected to the GSM modem and it displays
AT+CPIN?
+CPIN: READY
OK

for the moment you can forget the rest of the code.

Waiting for GSM modem to initialise
AT+CPIN? ---- + resp
RxBuf=AT+CPIN? //this shouldnt be appearing in RxBuf
+CPIN: READY
OK
¬¸
AT+CREG? ---- + resp
RxBuf=AT+CREG? //this shouldnt be appearing in RxBuf
+CREG: 0,1
OK
¬¸
SIM registered to n/w
voltage= 356mV
AT+SAPBR=2,1 ---- + resp
RxBuf=AT+SAPBR=2,1
+SAPBR: 1,1,"100.113.176.162"
OK
dÇd6
httpdata_cmd = AT+HTTPDATA=31,5000
data = key=D9R6K3Y1NNMNZPCD&field1=356
AT+HTTPINIT ---- + resp
RxBuf=AT+HTTPINIT
OK
1,1 ¬ ¬¬67
AT+HTTPPARA="CID",1 ---- + resp
RxBuf=AT+HTTPPARA="CID",1
OK
¬¬67
AT+HTTPPARA="URL","http://api.thingspeak.com/update" ---- + resp
RxBuf=AT+HTTPPARA="URL","http://api.thingspeak.com/update"
OK
6
AT+HTTPDATA=31,5000 ---- + resp
RxBuf=AT+HTTPDATA=31,5000
DOWNLOAD
ngspeak.com/update"
OK
6
key=D9R6K3Y1NNMNZPCD&field2=100 ---- + resp
RxBuf=
OK
PDATA=31,5000
DOWNLOAD
ngspeak.com/update"
OK
6
AT+HTTPACTION=1 ---- - resp
RxBuf=
,+HTTPACTION=1
OK
DOWNLOAD
ngspeak.com/update"
OK
6
AT+HTTPREAD ---- + resp
RxBuf=AT+HTTPREAD

veeru:
for the moment you can forget the rest of the code.

I don't know which code you want me to ignore.
If you have a minimal program that illustrates the problem please post that.

And I don't understand the stuff that followed the line I quoted.

You have a great many string literals. I wonder are you suffering memory corruption. Try using the F macro.

What happens if you make RxBuf[] either static or global?

...R