I am working with a COM port, with the ultimate aim of streaming GPS data coming through one port and then passing it through a different port. Right now, I am working on the “passing it through a different port” part, trying to get a fake GPS sentence to be written through a COM port. However, after following several examples for setting up and sending data through a COM port, I seem to be having some trouble.
I set up my COM port as follows:
HANDLE serialHandleQGIS;
serialHandleQGIS = CreateFile(L"COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
I then go through the process of setting it up:
if (serialHandleQGIS == INVALID_HANDLE_VALUE) {
//handle error
}
DCB serialParamsQGIS = { 0 };
serialParamsQGIS.DCBlength = sizeof(serialParamsQGIS);
if (!GetCommState(serialHandleQGIS, &serialParamsQGIS)) {
//handle error
}
serialParamsQGIS.BaudRate = CBR_9600; //baud rate
serialParamsQGIS.ByteSize = 8; //data size
serialParamsQGIS.StopBits = ONESTOPBIT; //stop bit
serialParamsQGIS.Parity = NOPARITY; //parity
if (!SetCommState(serialHandleQGIS, &serialParamsQGIS)) {
//handle error
}
I create my “GPS data”:
string gpsString = "$GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M,,,,0000*18<CR><LF>";
string binaryString;
for (size_t i = 0; i < gpsString.size(); ++i) {
bitset<8> b(gpsString.c_str()[i]);
binaryString += b.to_string();
}
const char* gpsTx;
gpsTx = binaryString.data();
DWORD gpsBytesTx;
I use the following to send that data:
if (!WriteFile(serialHandleQGIS, gpsTx, sizeof(gpsTx), &gpsBytesTx, NULL)) {
//handle error
}
With no errors. But then the following does not produce anything:
else {
printf(gpsTx);
printf("n");
}
I have the following to “debug”:
printf("Before Send:n");
printf(gpsTx);
WriteFile(serialHandleQGIS, gpsTx, sizeof(gpsTx), &gpsBytesTx, NULL);
printf("nAfter Send:n");
printf(gpsTx);
The “Before Send” prints, but the “After Send” does not. This behavior leads me to think that there is some problem with the sending of the data with “WriteFile()”.
Everything compiles, and no errors are thrown (I have code in each of the “if” loops to “return 0” if an error occurs). I cannot find any information to explain what may be going on here – Thoughts?