Share via

How to avoid false positive where modem lines go High upon serial port opening using win32?

sirjuicebox-9237 0 Reputation points
2026-03-12T19:47:49.0033333+00:00

I'm using the Win32 library to interact with serial ports. Using pretty standard code, when I call createFile(), I notice that all modem lines suddenly send back a HIGH signal once the call completes. This is causing false positives in my application. Without rewriting core code to filter out false positives, is there way to open the port without asserting these modem lines?

Here is a sanitized example of my code:

HANDLE hSerial; 
hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 
if(hSerial==INVALID_HANDLE_VALUE)
{ 
	if(GetLastError()==ERROR_FILE_NOT_FOUND)
	{ 
		//serial port does not exist. Inform user. 
	} 
	
	//some other error occurred. Inform user. 
}
Windows development | Windows API - Win32
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 150 Reputation points Microsoft External Staff Moderator
    2026-03-13T04:05:30.15+00:00

    Hello @sirjuicebox-9237 ,

    Thank you for reaching out.

    The behavior you see happens when you open a serial port with CreateFile(). This is normal. When the port opens, the driver often turns on some modem lines (like DTR or RTS) by default. Your application can see a HIGH signal right away. This can cause false positives.

    You can try these ways to fix it:

    1. Set modem lines after opening the port Use GetCommState to get the current settings. Change DTR and RTS as you want. Then apply the settings with SetCommState.
    2. Clear modem lines manually Use EscapeCommFunction with CLRDTR or CLRRTS after opening the port.

    Hope this helps. If you found my response helpful or informative in any way, I would greatly appreciate it if you could follow this guidance to provide feedback.

    Thank you.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.