Below (over two posts) example code for Arduino and example code for a C# console application that demonstrates the idea of a protocol. I've mostly concentrated on the C# side of things; the Arduino code was quickly hacked together.
Arduino
byte buffer[4096];
byte chunkSize = 32;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(57600);
}
void loop()
{
receiveConfirmEcho();
}
/*
receive data from PC
send a message back every chunkSize (32) bytes
when all data is received, echo the data back to PC
*/
void receiveConfirmEcho()
{
static uint16_t index;
static uint32_t lastUpdateTime;
// read data if available
while (Serial.available() > 0)
{
// update the time that a byte was received
lastUpdateTime = millis();
// save in buffer
buffer[index++] = Serial.read();
// confirm
if (index % chunkSize == 0)
{
Serial.println(index);
}
}
// on time out, echo the received data back
if (index != 0 && (millis() - lastUpdateTime > 5000))
{
for (uint16_t cnt = 0; cnt < 4096; cnt += chunkSize)
{
Serial.write(&buffer[cnt], chunkSize);
delay(200);
}
// reset variables
index = 0;
memset(buffer, 0, sizeof(buffer));
}
}