Using CreateFile to open serial port using VC++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Using CreateFile to open serial port using VC++

Post by Saman » Fri Dec 02, 2011 12:55 am

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

Return to “C/C++ Programming”