1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
//close if open
if (m_hPort != INVALID_HANDLE_VALUE)
{
if (hCommWatchThread)
{
// Clear all event notification masks
SetCommMask( m_hPort, 0 );
}
CloseHandle(m_hPort);
m_hPort = INVALID_HANDLE_VALUE;
m_PortNum = 0;
}
//open
m_PortNum = 1;
m_BaudRate = 38400;
//************************************************
// Open the port and get the handle...
CString strPort ;
strPort.Format("COM1:");
//strPort.SetAt(3, '0' + '1');
m_hPort = CreateFile( strPort, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, // overlapped I/O
NULL );
if (m_hPort == INVALID_HANDLE_VALUE)
{
/*TRACE(_T("Unable to open '%s'\n"), strPort);
return FALSE;*/
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
CloseHandle(m_hPort);
OnButton1();
//PostQuitMessage(0);
}
//TRACE(_T("Opened '%s'\n"), strPort);
AfxMessageBox("COM1 opened",MB_OK); |
Partager