I am working on building a serial command receiver. The idea is came from the boot loader. However, I got a very strange problem that I have no idea what could cause it.
As the first screenshot show, the print isn't finished because "===a=" is stuck, like this.

In the code, it should print out and the others.
char* receiveCmd(Stream &_serial, char &cmd_ptr, int &l_ptr) {
Stream* serial = &_serial;
if (serial->available() && (uint8_t)serialRead(_serial) == CMD_START) {
int buf_count = 0; //start at data
cmd_ptr = serialRead(_serial); //cmd_byte
char low_byte = serialRead(_serial);
char high_byte = serialRead(_serial);
uint16_t length = low_byte; //data_length
length = length | (high_byte << 8);
Serial.println("\n\n===a===");
Serial.println(length);
Serial.println("===b===");
l_ptr = (int)length;
char* _buf = (char*)malloc(length * sizeof(char));
while (buf_count != length) {
if (serial->available()) {
_buf[buf_count] = serial->read();
buf_count++;
}
}
char checksum = serialRead(_serial); //checksum, don't bother it yett
char stop_byte = serialRead(_serial);
if (stop_byte != CMD_EOF) {
free(_buf);
return NULL; //error
}
return _buf;
}
return NULL;
}
Anyway, the problem is fixed when I print out other variables first, then the length var..
char* receiveCmd(Stream &_serial, char &cmd_ptr, int &l_ptr) {
Stream* serial = &_serial;
if (serial->available() && (uint8_t)serialRead(_serial) == CMD_START) {
int buf_count = 0; //start at data
cmd_ptr = serialRead(_serial); //cmd_byte
char low_byte = serialRead(_serial);
char high_byte = serialRead(_serial);
uint16_t length = low_byte; //data_length
length = length | (high_byte << 8);
Serial.println("\n\n===a===");
Serial.println(low_byte, HEX);
Serial.println(high_byte, HEX);
Serial.println(length);
Serial.println("===b===");
l_ptr = (int)length;
char* _buf = (char*)malloc(length * sizeof(char));
while (buf_count != length) {
if (serial->available()) {
_buf[buf_count] = serial->read();
buf_count++;
}
}
char checksum = serialRead(_serial); //checksum, don't bother it yett
char stop_byte = serialRead(_serial);
if (stop_byte != CMD_EOF) {
free(_buf);
return NULL; //error
}
return _buf;
}
return NULL;
}
Other code is exactly the same. Does anyone know what may cause it? I can't print out those thing during production because it is only for debugging. If I remove those print, the code is also stuck(because of the length variable again).
For additional information, the serial signal is generated from other Arduino and AltSerial Library. The structure is like this.
void sendCmd(Stream &_serial, char cmd, char* payload, int length) {
Stream* serial = &_serial;
char buf[6 + length]; //start, data_length, data, checksum, stop
buf[0] = CMD_START; //start_byte
buf[1] = cmd; //cmd_byte
buf[2] = length & 0xff; //data_length - low byte
buf[3] = (length >> 8) & 0xff; //data_length - high byte
buf[4 + length] = (char)calcCRC(payload); //checksum
buf[5 + length] = CMD_EOF; //stop_byte
for (int i = 0; i < length; i++) //load buf
buf[4 + i] = payload[i];
for (int i = 0; i < sizeof(buf); i++)
serial->print(buf[i]);
}
I have used Serial.print(altSerial.read(), HEX) to check whether the byte is received correctly. They are all correct. That's why it is confusing me.QAQ
===== UPDATED INFORMATION =====
I have narrowed the problem. It seems like the below code is the main cause regard the issue. But no solution for this still.
uint16_t length = low_byte; //data_length
length = length | (high_byte << 8);
Thank you.