Receiving characters and organizing them

Hi all, I am using 1 Mega, 2 Xbee, and XCTU via PC. Currently I am able to send data from Xctu to Mega serial. I am trying to send long character via Xctu, it is of the following format a19282b13123 The idea is to separate each group of numbers when a unique character appears. I have put together some lines, but the codes doesn't seem to work as desired. please help.

 char c;
 char valuea; 
 char valueb; 

void loop()
{
if(Serial3.available() > 0 ) { 
    // If data comes in from XBee, send it out to serial monitor
  
   //Serial.write(Serial3.read());
  // Serial.println();

   char c = Serial3.read();
    switch (c) {
    case 'a':
     {
      valuea = (Serial3.read()); 
      valuea += (Serial3.read());
    
      }
      break;
    case 'b':
            {
      valueb = (Serial3.read());
      valueb += (Serial3.read());

      }
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
    break;
  }
  
Serial.print("a= ");
Serial.print(valuea);
Serial.print(", b= ");
Serial.print(valueb);
Serial.println();
  }

d20237: but the codes doesn't seem to work as desired.

What does actually happen? And what do you want it to do that is different?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. I would generally prefer to receive a complete message before trying to analyse any of it.

Have you control over the format of the message from the PC? If so I suggest you use the technique in my 3rd example.

...R

Robin2:
What does actually happen? And what do you want it to do that is different?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. I would generally prefer to receive a complete message before trying to analyse any of it.

Have you control over the format of the message from the PC? If so I suggest you use the technique in my 3rd example.

…R

Thanks for the reply.
Currently, the Xctu enables me to send one chart at a time. I am not sure if it can send all characters in one go.

so, on the serial monitor, I get to read/receive, every character I send. followed by a = ; b= < thats all.
valuea and valueb are not appearing ! , i guess the code skips them for some reason !

d20237:
Currently, the Xctu enables me to send one chart at a time. I am not sure if it can send all characters in one go.

What is a chart? If it is a string of characters can you post an example? Is there a terminating character?

so, on the serial monitor, I get to read/receive, every character I send. followed by a = ; b= < thats all.
valuea and valueb are not appearing ! ,

Can you copy and paste from the Serial Monitor so we can see exactly what you see.

Also it would help if you post the exact string of characters that was sent to the Arduino that gave rise to the output in the Serial Monitor.

You did not answer my question whether you can control the format of the output from the PC.

…R

chart = character. sorry.
Here is a snap shot for XCTU program and Serial monitor.
Exact string of characters that was sent is shown as well.

I don’t think there is formatting option from XCTU.

Images from Reply #4 so we don’t have to download it. See this Image Guide

9c2322ac6ea6c848bcaaa2a2f154ea85ba487d93.jpg

…R

You have not said if there is an end-marker at the end of the block of data?

Have you tried the examples in the link I gave you?

For the future please use copy and paste to show text rather than post a photo of it.

...R

Robin2:
Images from Reply #4 so we don’t have to download it. See this Image Guide

9c2322ac6ea6c848bcaaa2a2f154ea85ba487d93.jpg

…R

Thanks for pointing that out. I thought this option is not available.

Robin2: You have not said if there is an end-marker at the end of the block of data?

Have you tried the examples in the link I gave you?

For the future please use copy and paste to show text rather than post a photo of it.

...R

There is no end marker used. I have read the thread and examples, getting ready to implement it.

d20237:
There is no end marker used.

Then how do you know when a complete message has been received or when a new message starts?

(And just to be clear, that is a technical question, not a criticism)

…R

Robin2: Then how do you know when a complete message has been received or when a new message starts?

(And just to be clear, that is a technical question, not a criticism)

...R

Robin2, First, I would like to thank you for participating and help. Hence, I am ok criticism :) The message sent via xCTU is a clear set of two letters and six numbers post each letter.

d20237: The message sent via xCTU is a clear set of two letters and six numbers post each letter.

That seems to mean that every message is 8 characters long.

If I was confident that that I knew which was the first character I could just count until I got to 8 and then I would know I have the full message. But how can I be confident of starting counting at the first character?

Maybe there is a definite interval between messages and you could reckon you had the last character if the interval after it was longer than X (whatever X might be). Do you think that might work?

And you are absolutely sure that there is no linefeed or carriage-return character at the end of the message?

What software is producing the messages? Can you post a link to its datasheet or user manual?

...R

Robin2:
That seems to mean that every message is 8 characters long.

If I was confident that that I knew which was the first character I could just count until I got to 8 and then I would know I have the full message. But how can I be confident of starting counting at the first character?

Maybe there is a definite interval between messages and you could reckon you had the last character if the interval after it was longer than X (whatever X might be). Do you think that might work?

And you are absolutely sure that there is no linefeed or carriage-return character at the end of the message?

What software is producing the messages? Can you post a link to its datasheet or user manual?

…R

Yes,
Counting is the obvious choice.
Here is how I did it. Thanks for the your example 3. :slight_smile:

The software is xCTU, https://www.digi.com/products/xbee-rf-solutions/xctu-software/xctu

  if (recvInProgress == true) {
     if (ndx < 8){
          receivedCharsX[ndx] = c;
          ndx++;
      }
      else if( 8 <= ndx && ndx < 16){
          receivedCharsY[ndx-8] = c;
          ndx++;
      }
      else {
       recvInProgress = false;
      }
   }

d20237: Counting is the obvious choice. Here is how I did it.

Thanks for the feedback.

I am a little concerned that the Arduino may unwittingly start counting at the wrong place.

If there is a long gap between messages you could measure the time to be sure that you were between messages.

...R