Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open COM1 from application launched at startup

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.

like image 753
n0rd Avatar asked Nov 20 '11 09:11

n0rd


2 Answers

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.

like image 179
Akshay Kekre Avatar answered Sep 30 '22 06:09

Akshay Kekre


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

like image 39
Gianluca Ghettini Avatar answered Sep 30 '22 06:09

Gianluca Ghettini