PDA

View Full Version : Binary file data read/write using serial communication



anita.niharika@gmail.com
15th February 2021, 05:51
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

d_stranz
15th February 2021, 16:17
Don't double-post. You asked the identical question last week in the Newbie section.

Not only that, but you have been given answers and advice on how to handle these serial communications several times already in your previous posts. Go back and re-read those posts and you will see that in every case, you need to design a system that queues up a set of commands, executes one of them, waits for a successful reply, then executes the next until the queue is empty.

It does not matter if you read the complete file into a buffer or read and send the file in chunks. The point is that however you decide to do it, you need to implement the handshaking protocol between your PC and your device that allows you to send the information in the sizes the device wants, wait for it to tell you it is ready for the next chunk, send it, and repeat until there are no more chunks. You can't do this in a single function with a loop; you have to implement an event-driven system where you send, wait for a reply, and send again, based on the signals issued by the serial port.

Qt's file I/O mechanisms have all of the signals and slots needed to accomplish this. Qt has many example projects (https://doc.qt.io/qt-5/qtserialport-examples.html) for serial port communication. This one (https://doc.qt.io/qt-5/qtserialport-cwritersync-example.html) looks like it is applicable to your problem.