#include
#include
int main()
{
HANDLE hComm;
hComm = CreateFile(“\\\\.\\COM24”, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING,// Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
printf(“Error in opening serial port”);
else
printf(“opening serial port successful”);
CloseHandle(hComm);//Closing the Serial Port
return 0;
}
//正确操作方法
hComm = CreateFile(“COM1”, // for COM1—COM9 only
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL);