Can't open COM1 from application launched at startup
Asked Answered
P

2

8

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?

Update: the COM-port is the real one.

Update2: regarding the other application that opens serial port before WinLIRC. I did the following: I put Process Explorer to the Startup folder so it start on log in also, then rebooted. As soon as process explorer started I ran "Find Handle or Dll" dialog, put "Serial0" to the input and hit "Search". By that moment WinLIRC had already shown message box saying "CreateFile(COM1) failed with 5". Then I waited till the process explorer search ends, seen that it found nothing, then tried to reinitialize WinLIRC and it failed again. So I suggest that it is not the case of serial port being opened by other application. If anyone can suggest a better way to check it, I'll happily recheck.

When I search for "Serial0" in process explorer while WinLIRC is running it finds the winlirc.exe process, so it looks like it is correct term to search.

Update3: regarding the serial mouse driver. It is not listed in device manager, so I wasn't able to disable it there, however I have found this instructions on how to disable sermouse service and it didn't help.

Update4: one more thing I forgot to mention. It happens only if I log in soon after booting PC. If I leave windows on log in screen for several minutes and log in later, then WinLIRC initializes without any problems always.

Update5: Unfortunately, I don't have access to the computer that had this problem reproducing, so I can't experiment anymore.

Pave answered 20/11, 2011 at 9:32 Comment(8)
There's another process that opens the port first. Probably a service. Disable it.Invite
is your COM port a real physical COM port, or is it an USB-serial adapter ?Compose
@HansPassant, that's a nice idea, I'm looking into it.Pave
@HansPassant, it seems that it is not the case, see my update.Pave
Maybe Windows is probing the port looking for a serial mouse. Try disabling the "Serial Mouse" driver from the Control Panel / Devices.Soper
@Pave are you running any type of 3rd party "rights management" software like Novell or something similar? Maybe the rights management software isn't loading all your rights before you gain access to the COM ports.Chamblee
@klut, nope, nothing like that.Pave
@Pave okay, thanks. If I were you, I'd debug by changing the parameters in CreateFile. In particular I would mess around with the dwDesiredAccess parameter as it seems like it could be stemming from there. My first try would be to change it to 0. If that doesn't work, and all you're doing is reading on the port, change it to GENERIC_READ. Other than that, I would say its an OS issue =/Chamblee
H
1

It takes time to initialize the port. Your application will run absolutely fine on windows XP. Windows7's serial ports are virtual.

You can run a small code and check it out using System.IO.Ports;

    private void Form1_Load(object sender, EventArgs e)
    {

        string[] ports = System.IO.Ports.SerialPort.GetPortNames();
        comboBox1.Items.Add("None");
        foreach (string port in ports)
            comboBox1.Items.Add(port);
        comboBox1.SelectedIndex = 0;

    }

This will return you the list of serial port. Check the status of it and display it on message box. Make this code and run at startup. You'll get the root cause.

Headon answered 23/6, 2013 at 7:59 Comment(0)
L
1

Here some links one has to visit before plunging into the magic world of serial programming in Windows :)

A detailed explanation of serial programming in Windows:

http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c5425/Serial-Communication-in-Windows.htm

a little bit outdated (the site states 1999-2003 so yes, it's outdated) but absolutely useful:

http://www.flounder.com/serial.htm

Loosestrife answered 24/6, 2013 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.