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>, <>, <>
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;
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.