[SOLVED] Need help for serial, char array, or pointer

Hello, I need help in building the controller side for my project.

The code is this:

const byte numChars = 32;
char receivedChars[numChars];
char * sendChar;
boolean newData = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println(F("Ready"));
}

void loop() {
  // put your main code here, to run repeatedly:
  recvWithStartEndMarkers();
  if(newData == true){
    sendChar = receivedChars;
    Serial.println(sendChar);
  }
  newData = false;
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 1;
  char startMarker = '{';
  char endMarker = '}';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (recvInProgress == true) { // this one is the second
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else { //this is the last, notice the while loop loser on the newData=true
        receivedChars[0] = '<';
        receivedChars[ndx] = '>';
        ndx++;
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) { //this one gets executed first, recvInProgress = true, and then the while do the loop, resulting the if(recvInProgress -- true) started
      recvInProgress = true;
    }
  }
}

Marker codes from Robin2

What I expect is when I send {data}, it will send back . I really need the format so that the receiver can interpret the data using the < start marker and > end marker. Please do not suggest me to send {} instead, because the real marker for the data for the controller is <>, not {}. I changed the marker to {} to distinguish between program and sent data.

The data sent is {2}, three times.
The data received is <2>, <>, <>

The data sent is {2}, three times.
The data received is <2>, <>, <>

I’m not sure I understand your ask but in the above you are possibly seeing \r and \n being received…

WTF is that sendChar variable for?

sendChar = receivedChars;
Serial.println(sendChar);

why not just doSerial.println(receivedChars);

J-M-L:
I’m not sure I understand your ask but in the above you are possibly seeing \r and \n being received....

So, I send {2}
received <2>
send {2} again
received <>
send {2}
received <>

Also, note that I used the no line ending

J-M-L:
WTF is that sendChar variable for?

sendChar = receivedChars;

Serial.println(sendChar);



why not just do


Serial.println(receivedChars);

Did it, still same. Well, I forget if I can do that.

Sorry, after wandering for some time in the code, I found the mistake:

      else { //this is the last, notice the while loop loser on the newData=true
        receivedChars[0] = '<';
        receivedChars[ndx] = '>';
        ndx++;
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
ndx = 0;

Should be ndx = 1;

Tested, and worked

Thanks for the time, J-M-L

I think you’re slightly missing the point of the markers. Robin’s original code is intended (and will) give you the message without the markers.

After processing the message (not the incoming data) you send the processed message back; that is the point where you add the new markers.

So basically you’re not supposed to modify the recvWithStartEndMarkers() function, after all it’s the receive routine, not the transmit routine.

I suspect that what is wrong with your code is this block (specific line marked)

     else { //this is the last, notice the while loop loser on the newData=true
        receivedChars[0] = '<';
        receivedChars[ndx] = '>';
        ndx++;
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;    <<<<<<<<<<<<<<<<<<<<<<<<<<<<
        newData = true;
      }

When you send your second {2}, ndx starts with 0 and you happily store 2 in the buffer at the first position. Next you receive the endmarker and you even more happily overwrite that 2 with the new start marker.

Yeah I would second that:

  • get the message through Robin’s code so {2,3} becomes 2,3
  • print out a <
  • print out the message received 2,3
  • print out a >

==> whatever is on the receiving end will see <2,3>

There is no need to mess around with the buffer or the function. Keep it simple