Problem sending serial data with xbee (duplicate value?)

Hello, I tried searching in here and couldnt find what I was looking for. Story short, my pc send to the arduino a command (keyword) through xbee and the arduino, based on the keyword will do something and send a response back. I need to send strings through xbee because I attach the module ID to the response so the pc knows which module replied back.

The problem I have is that the first command works fine. When I send a second command, the serial string sent still includes the first response.

Here is the serial monitor results:

CM <---- command from PC
AA16| <--- response sent o pc from arduino (Good!)
TEST <---- command from PC
AA16|CMCOK| <--- response sent o pc from arduino (BAD!).. should only be "CMCOK|"

I am using xbee.print so I am able to send the string. Before I added the Module ID to it, I was using xbee.write because it was not using a string variable and everything was working fine.

here is my code: (MODID is a string variable)

#define buzzerPin 8 
#define XBee Serial3

String MODID = "AA"; 
String MODQ = "16";  

const byte numChars = 32;   // XBEE variable
char receivedChars[numChars]; // XBEE variable
boolean newData = false;    // XBEE variable

void setup() {
  XBee.begin(9600);
  Serial.begin(9600);

}

void loop() {
  recvWithStartEndMarkers();
  showNewData();

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (XBee.available() > 0 && newData == false) {
        rc = XBee.read();
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
 if (newData == true) {
 Serial.println(receivedChars); //Uncomment to see string in serial monitor
 String RecData; RecData=String(receivedChars);  RecData.trim();
  if(RecData == "CM"){
        String MODCHK = MODID += MODQ += "|";
        Serial.println(MODCHK);
        XBee.print(MODCHK);
        delay(1);
      }
if(RecData == "TEST"){
      String TestStatus = "";
      TestStatus += MODID;
      digitalWrite(buzzerPin,HIGH); delay(100);
      digitalWrite(buzzerPin,LOW); delay(100);
      digitalWrite(buzzerPin,HIGH); delay(100);
      digitalWrite(buzzerPin,LOW);     
      TestStatus += "CMCOK|";
      Serial.println(TestStatus);
      XBee.print(TestStatus);
      delay(1);
  }
        newData = false;
 }
}

Any help is appreciated... starting to bang my head lol

Don't post snippets (Snippets R Us!)

your bug is likely here with all the += you modify the Strings that we don't know about

      String MODCHK = MODID += MODQ += "|";

MODCHK is technically "empty"? Thats where its declared and I just feed it the value

Should it be

String MODCHK = "";
MODCHK = MODID += MODQ += "|";

To me thats pretty much the same thing but in Arduino I could be wrong :slight_smile:

I also editied the code to add the missing portion... not much was missing.

I think you are confused between += and +

when you do MODCHK = MODID += MODQ += "|";
you modify the 3 strings... may be you meant
MODCHK = MODID + MODQ + "|";

I think you are correct :wink:
For some reason i thought += was for concatenation... i was actually thinking about that... i will try + only in a few and report back...

Well that was it :slight_smile:
OMG when coding for hours sometimes the obvious is not.. obvious anymore.

Thanks again kind sir!

It's often like this. You can't see the obvious. When it happens, it means it's time to go away from the keyboard and take a walk.

+= means concatenate and assign the result to the variable
+ means concatenate only

so basically MODCHK = MODID += MODQ += "|";
was the same as

MODQ =  MODQ + "|";
MODID = MODID + MODQ;
MODCHK = MODID;

which was not what you wanted

note that doing multiple concatenation with Strings might lead to memory challenges. Do you really need the concatenation at all?

well i need to send the module id and the response. my app on pc reads the data and separate the id from the answer so it knows which module the response is attached to.

i will have multiple modules so the way i understand it, with xbee, all the modules will receive the command. unless there is a way to target a specific xbee? Havent really searched for that. If there is a way, is there a possibility for arduino to get me that identification?

what I meant is do you need the answer in a variable. Can't you just send back the message in 3 steps?

xxx.print(MODID);
xxx.print(MODQ);
xxx.print("|");

yeah i could. since i am using the | as the end of the message to action it on pc, it shouls receive all of it. that could work.

thanks again

That would save memory and make your code faster. And you can get rid of the String class as well (MODID and MODQ could be in PROGMEM or just const char*)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.