Hello all!
Had a quick question that my colleague and I are curious about.
To set the scene (unfortunately cannot go into extreme detail), we have two IrDA2 Click Transceiver boards, one connected to the laptop through UART, and the second connected to the Tx and Rx of the Arduino Uno.
We have a string sent manually from software that is as follows:
5A 5A 5A 5A FB 4D 00 01 AB AB 10 00 C0 DE BE EF
Where the 5A bytes are sync words, FB 4D is the received checksum, 00 01 is the message ID, AB AB is the message status, 10 00 is the byte length, and C0 DE BE EF is the message data.
The code that I have attached is essentially just loopback code, where the arduino is supposed to read the serial data from the IR boards, store it in the buffer (in order), check if the data received matches (if not modify the status bytes), and then send the data back after setting the message ID to 2.
The code itself works, however we have noticed in testing it that after 96 bytes received and sent (6 messages), the arduino seems to freeze up on the Serial.write functions, and requires us to manually reset the board to have the system to continue working. We are trying to figure out the reasoning behind this, as the reset button in application will not be able to be manually touched, and just for sanity's sake. We figured it may be a memory issue (however that still for some reason doesn't seem like the cause) with memcpy function and struct layout, however we are not entirely sure, and were curious if anyone had any ideas.
The code for the loop (with struct declaration & brief setup for checksum) as is:
struct incomingMsg
{
uint8_t sync_word1;
uint8_t sync_word2;
uint8_t sync_word3;
uint8_t sync_word4;
uint16_t checksum;
uint16_t msgID;
uint16_t msgStatus;
uint16_t msgLeng;
uint32_t msgData;
} iM;
void setup() {
Serial.begin(115200);
checksum = checksum_gen((uint8_t*)(&(iM.msgID)), iM.msgLeng - 6);
}
void loop() {
byte counter = 0; //set counter for storing in buffer, cannot use msgLeng b/c it is the equivalent of the buffer
byte buff[sizeof(incomingMsg)]; //buffer for storing message as counter increments and data is read
if (Serial.available() > 0) {
buff[counter++] = Serial.read();
} //after parsing out data, need to: (1) set msgID to 2 (2) if data is incorrect, set msgStatus to 0xEAEA (3) send data back out
memcpy(&iM, buff, sizeof(incomingMsg));
if (iM.checksum != checksum) { //issue with original struct value, keep it with iM struct and value for checksum that is expected, checksum matches, set msgID to 2 and leave msgStatus at 0xABAB
iM.msgStatus = 0xEAEA; //set msgStatus to EAEA to signify bad data
}
iM.msgID = 0x0002; //set msgID to 2 before sending back
iM.checksum = checksum_gen(((uint8_t *)&iM) + 6, (iM.msgLeng - 6));
Serial.write(iM.sync_word1);
Serial.write(iM.sync_word2);
Serial.write(iM.sync_word3);
Serial.write(iM.sync_word4);
Serial.write((uint8_t*)(&(iM.checksum)), 2);
Serial.write((uint8_t*)(&(iM.msgID)), 2);
Serial.write((uint8_t*)(&(iM.msgStatus)), 2);
Serial.write((uint8_t*)(&(iM.msgLeng)), 2);
Serial.write((uint8_t*)(&(iM.msgData)), 4);
}
}
Thank you in advance for your help! I have the rest of this code handy as needed, but wanted to simplify it down to where the problem resides. We redid the code (modifying the structs and swapped static bytes to bytes) so we have solved this problem, however we're curious as to why this set of code doesn't work, so if anyone has any clue it would be much appreciated!