I open the port to communicate with the device and control the device, but the function CreateFile()returns INVALID_HANDLE_VALUE.
GetLastError()returns 2, which means that it cannot find the specified file.
My code is shown below:
wsprintf( szPort, "COM%d", nPort );
m_hIDComDev = CreateFile(szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (m_hIDComDev == INVALID_HANDLE_VALUE)
{
DWORD err=GetLastError();
}
Why is this problem occurring?
source
share