Page 1 of 1

Using CreateFile to open serial port using VC++

Posted: Fri Dec 02, 2011 12:55 am
by Saman
Here is the way to open serial port for non-overlapped (synchronous) communication in Visual C++.

Code: Select all

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.
}