Unable to open COM1 from an application launched at startup

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

I downloaded WinLIRC sources and added MessageBox calls here and there so that I can see what happens during initialization and found out that the CreateFile call is not being executed:

 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 a message box with the message "CreateFile (COM1) with 5", and 5 is the error code for the error "Access is denied" according to this link .

So, the question is why opening a COM port may fail with such an error immediately after loading Windows and continue normally a few seconds or minutes later?

Update : COM port is real.

Update2 : regarding another application that opens a serial port prior to WinLIRC . I did the following: I put Process Explorer in the Startup folder so that it also starts when you log in, and then reboots. As soon as the process explorer starts, I launched the “Find Handle or Dll” dialog, placed “Serial0” on the input and clicked “Search”. By that time, WinLIRC had already shown a window saying that "CreateFile (COM1) failed with 5". Then I waited until the search for the process explorer was over, saw that he had not found anything, and then tried to reinitialize WinLIRC, and it again failed. Therefore, I propose that this is not the case when the serial port was opened by another application. If anyone can suggest a better way to test this, I will gladly double-check.

When I search for “Serial0” in the process explorer while WinLIRC is running, it detects the winlirc.exe process, so it looks like the right term to search for.

Update3 : regarding a serial mouse driver. It is not listed in the device manager, so I could not turn it off, however I found this instructions for disabling sermouse , and it did not help.

Update4 : one more thing that I forgot to mention. This only happens if I log in shortly after booting the PC. If I leave the windows on the login screen for several minutes and log in later, WinLIRC initializes without any problems.

Update5 . Unfortunately, I do not have access to a computer that had this playback problem, so I can no longer experiment.

+8
winapi serial-port createfile
source share
2 answers

It takes time to initialize the port. Your application will work fine in Windows XP. The serial ports of Windows7 are virtual.

You can run a little code and test it 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 a list of serial ports. Check the status and display it in the message box. Make this code and run at startup. You will get the root cause.

+1
source share

Here are some links to visit before diving into the magical world of serial programming in Windows :)

A detailed description of sequential programming in Windows:

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

a bit outdated (website 1999-2003 so yes, it is outdated), but absolutely useful:

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

+1
source share

All Articles