Using serial communication between PC & a device, I am sending & Receiving pre-defined serial data frame. As part of this I need to program a binary file into a flash,

From PC, In request command sending
- a binary file size in bytes,
- chunk size [ex:65535 ] - How can calculate chunk size here?

Based on response from the device,
- response = success/failure
- chunk size

On the successful response, I would send next request file data write command to the device as below from PC,

I want to do this following command execution until all data is written or error
===============================
request command
- chunk id
- chunk size
- actual binary file data

response
- result = success/failure
- chunk id (that has (not) been written)
===============================


How can I implement this loop mechanism in the serial sender part? How to handle file operation I mean read()/write API's or also saw QDataStream to perform file operation?
Should I read complete binary file first and store it in big buffer or directly from the file?
I am running my GUI on Windows.

Best Regards,
Anita