I was attempting to Serial Read using readBytesUntil and parsing them by commas within a function. The parsed char array will be returned to the main function using pointers and individually converted to int using atoi for other variable manipulation.
But I'm just wondering why Serial Printing currentChannelValue cause the For-Loop to break halfway, as shown in the image (Print becomes corrupted on the 8th loop in this instance).
void setup() {
Serial.begin(9600);
}
void loop() {
char buffer[33];
char *slots[33];
int charsRead;
int channelLoop;
int sizeofdata;
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
buffer[charsRead] = '\0';
sizeofdata = DoBreakup(buffer, ",", slots);
Serial.println(sizeofdata);
Serial.println();
for(channelLoop = 0; channelLoop < sizeofdata ; channelLoop++) // Expected to loop 11 times
{
int channelRegister = 0;
int currentChannelValue = 0;
const char *bufferCharToInt[2];
bufferCharToInt[channelLoop] = slots[channelLoop];
bufferCharToInt[channelLoop + 1] = '\0';
currentChannelValue = atoi(bufferCharToInt[channelLoop]);
// Loop stops halfway whenever these three lines are added
Serial.println((String)"Slots Value: " + slots[channelLoop]);
Serial.println((String)"Conversion Buffer: " + bufferCharToInt[channelLoop]);
Serial.println((String)"Current Channel: " + currentChannelValue);
char binary[5] = {0};
channelRegister = channelLoop + 32;
itoa(channelRegister,binary,2);
char* string = binary + 1;
Serial.println(string);
Serial.println();
}
memset(slots, 0, sizeof(slots));
delay(1);
}
}
uint8_t DoBreakup(char s[], char delimit[], char *ptr[])
{
char *p;
int i = 0;
p = strtok(s, delimit);
while (p) {
ptr[i++] = p;
p = strtok(NULL, delimit);
}
return i;
}
The Serial.println((String)) will not be necessary for the final assembly as I'm just using it for troubleshooting now, but it is still a curious thing, the memory used is 11% of dynamic memory for now.
you only have room for 2 char pointers in bufferCharToInt yet you use channelLoop as an index which can (does) go way above 1...
➜ you write beyond the bounds of your pointer array (which has no use probably) and thus you corrupt your memory. From there all bets are off.
I would suggest to study Serial Input Basics to better handle Serial input and not rely on readBytesUntil which can timeout and give you an incomplete view of what was coming in depending on your circumstances.
Yup thanks! I am aware of the heap issue using String type, mainly using it now for labelling the things I'm printing for troubleshooting; it will be removed later on.
Could I just check why the Serial.Read sometimes do not register things that are printed on the Serial Monitor? i.e., I have to send the serial data twice before it recognizes the startMarker and runs the other blocks of code.
Hi @J-M-L, thanks for the revised code. Just for my understanding, both codes seems to still suffer from a lack of serial reading if the serial data is sent to the Serial.Monitor almost immediately (~1s) after Uploading.
But otherwise, it seems to be ok, so it was probably due to the inherent Bootloader/startup lead time of the Arduino Uno.
Anyways thanks so much for your guidance man, really appreciate it, sorry about the rookie mistakes .
When you open the serial monitor the arduino reboots. So Indeed needs a bit of time to get fully ready. If this is critical for you because for example there is no human intervention then either add a small pause in the program connecting to the arduino or establish a handshake (the caller waits for some OK message from the arduino before dumping data onto the Serial line)