Hi, Even I face the same problem when i try to close the handle. I haven't used any thread to create or close the handle. I have literally used CreateFileW() to create a handle for serial port communication.
hComm = CreateFileW (pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Where pcCommPort is "COM_2" CreateFileW call succeeds even if device is not connected. I have send few commands through WriteFile() and read ReadFile() api's and tried to close the handle using
CloseHandle(hComm)
But my application hangs infinitely. What would be the exact root cause for this issue? Sample:
While(1)
{
i = 0;
hComm = CreateFileW (pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
while(i < 10)
{
i++;
WriteFile(hComm , &txBuffer[0], numBytesToTx, (DWORD*)&numBytesTx, NULL);
ReadFile(hComm , &get_data[i], 1, (DWORD*)&numBytesRx, NULL);
}
CloseHandle(hComm);
hComm = NULL;
}