I am trying to inject a timestamp in each line of the GPS NMEA sentences being captured from the port in this case COM3
.
My data currently looks like this:
#Example 1
EST: 2024-07-10 16:14:10
$GPSS ,,,,,,,,,,,
So on .....
......
...........
...........
EST: 2024-07-10 HH:MM:SS
$GPSS ,,,,,,,,,,,
So on .....
......
...........
...........
HH:MM:SS is arbitrary it means the next time stamp collected from the system.
However, I want the data to look like this:
EST: 2024-07-10 16:14:10 $GPSS ,,,,,,,,,,,
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
EST: 2024-07-10 HH:MM:SS So on .....
The code below produces example 1.
These are my imports as well:
#include <iostream>
#include <windows.h>
#include <chrono>
#include <stdlib.h>
#include <iomanip>
void readDataAndWriteToFile(HANDLE hSerial, const char* filePath) {
DWORD bytesRead;
char buffer[1024];
DWORD dwBytesWritten = 0;
HANDLE hFile;
hFile = CreateFile(filePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Error opening file for writing" << std::endl;
return;
}
auto startTime = std::chrono::steady_clock::now();
while (true) {
auto currentTime = std::chrono::steady_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::minutes>(currentTime - startTime);
if (elapsedTime.count() >= 2) {
break;
}
std::chrono::time_point<std::chrono::_V2::system_clock> input = std::chrono::system_clock::now();
ReadFile(hSerial, buffer, sizeof(buffer), &bytesRead, NULL);
if (bytesRead > 0) {
std::string timestamp = serial_TimePoint(input, "nEST: %Y-%m-%d %H:%M:%Sn");
DWORD timestampLength = static_cast<DWORD>(timestamp.size());
WriteFile(hFile, timestamp.c_str(),timestampLength,&dwBytesWritten, NULL);
WriteFile(hFile, buffer, bytesRead, &dwBytesWritten, NULL);
}
}
CloseHandle(hFile);
}
I am thinking whether if it were possible to concatenate timestamp.c_str()
, and buffer
. However, I feel this would still be a problem because buffer is a chunk of data read from the COM3
at a CBR_9600
, which is 9600 bits per second. I don’t understand if the problem is that it’s too fast.