FILE_NOT_FOUND when trying to open C ++ COM port

I am trying to open a COM port for reading and writing using C ++, but I cannot pass the first step of actually opening it. I get INVALID_HANDLE_VALUEon pen with GetLastError FILE_NOT_FOUND. I searched the Internet a couple of days when I'm out of ideas. I looked through all the questions regarding COM on this site.

I looked at existing ports (or, as I suppose) to get the port name correctly.

I also tried combinations _T("COM1")with a slash without a slash with a colon without a colon and without _T

I am using Windows 7 on a 64 bit machine.

this is the code i got

I will be happy for any input of this

void SendToCom(char* data, int len)
{

DWORD cbNeeded = 0;
DWORD dwPorts = 0;
EnumPorts(NULL, 1, NULL, 0, &cbNeeded, &dwPorts);

//What will be the return value
BOOL bSuccess = FALSE;

LPCSTR COM1 ;

BYTE* pPorts = static_cast<BYTE*>(malloc(cbNeeded));
bSuccess = EnumPorts(NULL, 1, pPorts, cbNeeded, &cbNeeded, &dwPorts);
if (bSuccess){
    PORT_INFO_1* pPortInfo = reinterpret_cast<PORT_INFO_1*>(pPorts);
    for (DWORD i=0; i<dwPorts; i++)
    {
        //If it looks like "COMX" then          
        size_t nLen = _tcslen(pPortInfo->pName);
        if (nLen > 3)
        {
            if ((_tcsnicmp(pPortInfo->pName, _T("COM"), 3) == 0) ){
                COM1 =pPortInfo->pName;
                //COM1 ="\\\\.\\COM1";
                HANDLE m_hCommPort = CreateFile( COM1 ,
                    GENERIC_READ|GENERIC_WRITE,  // access ( read and write)
                    0,                           // (share) 0:cannot share the COM port
                    NULL,                           // security  (None)
                    OPEN_EXISTING,               // creation : open_existing
                    FILE_FLAG_OVERLAPPED,        // we want overlapped operation
                    NULL                            // no templates file for COM port...
                    );
                if (m_hCommPort==INVALID_HANDLE_VALUE)
                {
                    DWORD err = GetLastError();
                    if (err == ERROR_FILE_NOT_FOUND) {
                        MessageBox(hWnd,"ERROR_FILE_NOT_FOUND",NULL,MB_ABORTRETRYIGNORE);
                    }
                    else
                        if(err == ERROR_INVALID_NAME) {
                            MessageBox(hWnd,"ERROR_INVALID_NAME",NULL,MB_ABORTRETRYIGNORE);
                        }
                        else
                        {
                            MessageBox(hWnd,"unkown error",NULL,MB_ABORTRETRYIGNORE);
                        }
                }
                else{
                    WriteAndReadPort(m_hCommPort,data);
                }
            }
            pPortInfo++;
        }
    }
 }

}
+4
source share
3 answers

, , 9, LPCWSTR szPortName = L"\\\\.\\COM11";.

0

Windows 10, , !

, "COM4" ERROR_FILE_NOT_FOUND. "" . 1511 "COM4", "".

0

http://www.cplusplus.com/forum/windows/163855/ Use CreateFileA (...) instead of CreateFile (...)

0
source

All Articles