Can't open COM1 from application launched at startup

Posted by n0rd on Stack Overflow See other posts from Stack Overflow or by n0rd
Published on 2011-11-20T09:32:31Z Indexed on 2011/11/20 9:50 UTC
Read the original article Hit count: 188

Filed under:
|
|

I'm using WinLIRC with IR receiver connected to serial port COM1 on Windows 7 x64. WinLIRC is added to Startup folder (Start->All applications->Startup) so it starts every time I log in. Very often (but not all the time) I see initialization error messages from WinLIRC, which continue for some time (couple of minutes) if I retry initialization, and after some retries it initializes correctly and works fine. If I remove it from Startup and start manually at any other moment it starts without errors.

I've downloaded WinLIRC sources and added MessageBox calls here and there so I can see what happens during initialization and found out that CreateFile call fails:

if((hPort=CreateFile(
    settings.port,GENERIC_READ | GENERIC_WRITE,
    0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0))==INVALID_HANDLE_VALUE)
{
    char buffer[256];
    sprintf_s(buffer, "CreateFile(%s) failed with %d", settings.port, GetLastError());
    MessageBox(NULL, buffer, "debug", MB_OK);
    hPort=NULL;
    return false;
}

I see message box saying "CreateFile(COM1) failed with 5", and 5 is an error code for "Access denied" error according to this link.

So the question is why opening COM-port can fail with such error right after booting Windows and proceed normally few seconds or minutes later?

© Stack Overflow or respective owner

Related posts about winapi

Related posts about serial-port