4

I am trying to open a com port for reading and writing using C++ but I can't seem to pass the first stage of actually opening it. I get an INVALID_HANDLE_VALUE on the handle with GetLastError FILE_NOT_FOUND. I have searched around the web for a couple of days I'm fresh out of ideas. I have searched through all the questions regarding COM on this website too.

I have scanned through the existing ports (or so I believe) to get the name of the port right.

I also tried combinations of _T("COM1") with the slashes, without the slashes, with colon, without colon and without the _T

I'm using windows 7 on 64 bit machine.

this is the code i got

I'll be glad for any input on 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++;
        }
    }
 }

}
6
  • 2
    _T has been redundant for 10 years now. Windows 95 is dead. Commented Nov 1, 2013 at 22:32
  • 2
    Pretty rare to run into COM1 these days, they are reserved for legacy hardware. Don't use EnumPorts(), that's for printers. Use the HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM registry key. Commented Nov 2, 2013 at 0:54
  • If CreateFile() fails with error 2 for both \\.\COM1 and COM1, then you really do not have a COM1 port installed. Go into Control Panel > Device Manager > Ports to verify. Commented Nov 2, 2013 at 1:08
  • Thanks for the input guys. 1. I don't have SERIALCOMM 2. I don't have ports entry under device manager Commented Nov 2, 2013 at 9:50
  • @RemyLebeau This doesn't appear to be the case. I get that error but the port is definitely listed in the device manager. No idea why. Commented Apr 10, 2014 at 10:35

4 Answers 4

4

The Solution is to use

The Problem is, if your port is Bigger then 9 then you have to use the Syntax LPCWSTR szPortName = L"\\\\.\\COM11";.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain a bit more?
0

If you are on Windows 10 - running all system updates might help !

I had the same issue that opening port "COM4" returned an error ERROR_FILE_NOT_FOUND. When running the program as "Administrator" it worked. Now after a updating to 1511 the program can open "COM4" even not running as "Administrator".

1 Comment

Well, it can't be windows 10 since this question was asked long before windows 10 came to be :) Thanks anyway
0

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

Comments

0

ERROR_FILE_NOT_FOUND can be produced from CreateFile(L"\\\\.\\COM1", ...) and CreateFile(L"COM1:", ...) after using the Device Manager to change the assigned COM Port number. Disabling and re-enabling the device, or unplugging and reconnecting the USB adapter resolves the issue.

A useful test to confirm whether it is your program or the system is to send data to the port in command prompt. A successful test will show an empty line. A failed test will show an error message.

C:\drop>echo > \\.\COM1
The system cannot find the file specified.

C:\drop>echo > \\.\COM1

C:\drop>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.