Hello! I've just come across this problem today, and I have absolutely no idea what's causing it because, logically, there is no reason for it to be doing this.
void serialEvent() {
int availableBytes = Serial.available();
if (availableBytes) {
Serial.readBytes(readData, availableBytes);
DWORD theResult = *(DWORD*)readData;
memset(data, 0, 32);
DWORD* dwData = (DWORD*)data;
dwData[0] = 2;
dwData[1] = theResult;
Serial.write(data, 32);
Serial.flush();
memset(data, 0, 32);
}
delay(1000);
}
In short, what my program is doing is reading some data sent from my computer and sending it back to it to print on an on-screen console. It works the first time, but for some reason, it's being sent twice with the first four bytes exactly the same. Here are the results:
[Packet 1] 0 0 0 0 1 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[Packet 2] 2 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[Packet 3] 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
What really matters is the third packet. In all the packets, the first four bytes describe the operation to be run on from my computer. The rest is additional data.
I've confirmed this to be the result of serialEvent() returning because, with delay(1000), the third and unwanted packet gets sent to my pc after exactly 1 second after the second packet gets sent.
Anyone got a clue on how to fix this?